Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "FSharp.Quotations.Evaluator.dll"
- open FSharp.Quotations
- open FSharp.Reflection
- open FSharp.Quotations.Evaluator
- module NTuple =
- let init n elements =
- printf "%d\n" n
- if n = 1 then List.head elements
- else Expr.NewTuple elements
- let makeType n =
- if n = 1 then typeof<int>
- else FSharpType.MakeTupleType(typeof<int> |> Array.replicate n)
- let get n ind t =
- if n = 1 then t
- else Expr.TupleGet (t, ind)
- let head n t = get n 0 t
- let tail n t =
- let elements = [for i in 1..n-1 -> get n i t]
- init (n - 1) elements
- let rec eachindex n size =
- if n = 1
- then
- let x = NTuple.head n size
- <@@ seq {for i in 0..%%(x)-1 -> [i]} @@>
- else
- let x = NTuple.head n size
- let xs = NTuple.tail n size
- let ex = eachindex (n - 1) xs
- <@@
- %%(ex)
- |> Seq.map (fun xs -> seq {for i in 0..%%(x)-1 -> i::xs})
- |> Seq.concat
- @@>
- let size = <@@ (2, 2) @@>
- let ex = eachindex 2 size
- let (ind:seq<int list>) = QuotationEvaluator.Evaluate (ex |> Expr.Cast)
- ind |> Seq.toList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement