Skip to content

Instantly share code, notes, and snippets.

@ctaggart
Created October 16, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctaggart/7835987f91bbfa481832c6bf6da299c3 to your computer and use it in GitHub Desktop.
Save ctaggart/7835987f91bbfa481832c6bf6da299c3 to your computer and use it in GitHub Desktop.
module FableApp
open Fable.Core
open Fable.Core.JsInterop
open Fable.Import
open Fable.Import.Node
open Fable.Import.ts
let makeFactorialFunction() =
let functionName = ts.createIdentifier "factorial"
let paramName = ts.createIdentifier "n"
let parameter =
ts.createParameter(
None, // decorators
None, // modifiers
None, // dotDotDotToken
paramName |> BindingName.Case1 |> U2.Case2);
let condition =
ts.createBinary(
paramName,
SyntaxKind.LessThanEqualsToken,
ts.createLiteral(1))
let ifBody =
ts.createBlock(
[| ts.createReturn(ts.createLiteral(1)) |],
true) // multiline
let decrementedArg = ts.createBinary(paramName, SyntaxKind.MinusToken, ts.createLiteral(1))
let recurse =
ts.createBinary(
paramName,
SyntaxKind.AsteriskToken,
ts.createCall(functionName, None, [| decrementedArg |])); // typeArgs
let statements: Statement array =
[|
ts.createIf(condition, ifBody)
ts.createReturn(recurse)
|]
ts.createFunctionDeclaration(
None, // decorators
Some [| ts.createToken(SyntaxKind.ExportKeyword) |], // modifiers
None, // asteriskToken
functionName |> U2.Case2 |> Some,
None, // typeParameters
[| parameter |],
ts.createKeywordTypeNode(SyntaxKind.NumberKeyword) :> TypeNode |> Some, // returnType
ts.createBlock(statements, true) |> Some // multiline
)
let resultFile =
ts.createSourceFile("someFileName.ts", "", ScriptTarget.Latest, false, ScriptKind.TS); // setParentNodes
let printerOptions = createEmpty<PrinterOptions>
printerOptions.newLine <- Some NewLineKind.LineFeed
let printer = ts.createPrinter(printerOptions);
let result = printer.printNode(EmitHint.Unspecified, makeFactorialFunction(), resultFile);
printfn "%s" result
C:\Users\camer\fs\ts2fable\src [(blog_2017-10-16)]> dotnet fable webpack
Fable (1.2.3) daemon started on port 61225
CWD: C:\Users\camer\fs\ts2fable
cmd /C node "C:\Users\camer\fs\ts2fable\node_modules\webpack\bin\webpack.js"
Bundling for development...
Parsing ./FableApp.fsproj...
fable: Compiled src\FableApp.fsproj
fable: Compiled src\App.fs
Hash: b5936d27100724ba5734
Version: webpack 3.6.0
Time: 11920ms
Asset Size Chunks Chunk Names
bundle.js 5.59 MB 0 [emitted] [big] main
bundle.js.map 6.24 MB 0 [emitted] main
[13] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/Date.js 7.49 kB {0} [built]
[18] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/Util.js 20.4 kB {0} [built]
[75] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/Symbol.js 324 bytes {0} [built]
[76] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/Long.js 37.3 kB {0} [built]
[77] ./src/FableApp.fsproj 25 bytes {0} [built]
[78] ./src/App.fs 1.56 kB {0} [built]
[145] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/String.js 21.8 kB {0} [built]
[146] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/Int32.js 1.12 kB {0} [built]
[150] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/TimeSpan.js 2.27 kB {0} [built]
[151] C:/Users/camer/.nuget/packages/fable.core/1.2.3/fable-core/RegExp.js 3.56 kB {0} [built]
+ 142 hidden modules
Closing Fable daemon...
C:\Users\camer\fs\ts2fable\src [(blog_2017-10-16)]> node ..\public\bundle.js
export function factorial(n): number {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment