Skip to content

Instantly share code, notes, and snippets.

@wraikny
Last active January 16, 2021 04:02
Show Gist options
  • Save wraikny/6337675e12be52ed15697d6c496ef127 to your computer and use it in GitHub Desktop.
Save wraikny/6337675e12be52ed15697d6c496ef127 to your computer and use it in GitHub Desktop.
#r "nuget: FSharp.Quotations.Evaluator"
open FSharp.Quotations
open FSharp.Quotations.Evaluator
let makeSequential (xs: #seq<Expr<unit>>): Expr<unit -> unit> =
let body =
(<@ () @>)
|> Seq.foldBack(fun x s -> <@ %x; %s @>) xs
<@ fun () -> %body @>
let ef =
[ 1..3 ]
|> List.map(fun x -> <@ stdout.WriteLine x @>)
|> makeSequential
let f = ef.Evaluate()
printfn "%A" ef
f ()
(*
Lambda (unitVar0,
Sequential (Call (Some (Call (None, ConsoleOut, [])), WriteLine,
[ValueWithName (1, x)]),
Sequential (Call (Some (Call (None, ConsoleOut, [])),
WriteLine, [ValueWithName (2, x)]),
Sequential (Call (Some (Call (None, ConsoleOut,
[])), WriteLine,
[ValueWithName (3, x)]),
Value (<null>)))))
1
2
3
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment