Last active
January 10, 2017 14:28
-
-
Save et1975/7db86e1310667423007f to your computer and use it in GitHub Desktop.
F# record selection translated into C# properties init expression tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Reflection | |
open System | |
open System.Linq | |
open System.Linq.Expressions | |
let memoize f = | |
let dict = System.Collections.Concurrent.ConcurrentDictionary<_,_>() | |
fun x -> dict.GetOrAdd(x, lazy (f x)).Force() | |
let getProperties = memoize(fun (t:Type) -> t.GetProperties(BindingFlags.Instance ||| BindingFlags.Public ||| BindingFlags.SetProperty).ToDictionary(fun p -> p.Name.ToLower())) | |
/// translate f# record init syntax tree into class property init tree: | |
/// r -> v1 -> v2 ... invoke(v2).invoke(v1) | |
/// into: | |
/// new { prop1 = v1, prop2 = v2 } | |
let translate x:Expression<Func<'T,'T>> = | |
let rec extract (args:Expression seq) (ps:ParameterExpression seq) sx = | |
match box sx with | |
| :? LambdaExpression as lambda -> | |
match lambda.Body with | |
| :? NewExpression as n -> | |
let props = getProperties typeof<'T> | |
let vals = lambda.Parameters |> Seq.append ps |> Seq.skip 1 // skip $ | |
let bindings = | |
vals.Select(fun p -> props.TryGetValue(p.Name.ToLower())).Where(fun (f,_) -> f) | |
|> Seq.zip args | |
|> Seq.map (fun (a, (_,p)) -> Expression.Bind(p, a)) | |
|> Seq.append | |
(n.Constructor.GetParameters() | |
.Select(fun p i -> (p, n.Arguments.[i])) | |
.Where(fun (_, a) -> a.NodeType = ExpressionType.Constant) | |
.Select(fun (p, a) -> Expression.Bind(props.[p.Name.ToLower()], a))) | |
|> Seq.map (fun b -> b :> MemberBinding) | |
|> Array.ofSeq | |
Expression.Lambda<Func<'T,'T>>(Expression.MemberInit(Expression.New(typeof<'T>),bindings), lambda.Parameters |> Seq.append ps |> Seq.take 1) // take $ | |
| :? MethodCallExpression as call -> | |
extract (Seq.append args call.Arguments) (Seq.append ps lambda.Parameters) call.Object | |
| _ -> failwith "Don't know how to translate the expression" | |
| _ -> x | |
extract Seq.empty Seq.empty x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
then using a table from Cassandra.Data.Linq:
Given a type, mapped to the table:
we can call the update, something like this:
resulting in efficient partial update Cassandra instruction.
Note that conversion of F# quotations to expression trees is automatic in some cases and with a bit of creative design update calls can be made a lot less verbose.