F#: how to elegantly select and group discriminated unions? -
I have a list of shapes:
type = size = | Float Board | Float Rectangle * Float A = [Circle 5.0; Rectangle (4.0, 6.0)
How can I test a Circle present in one? I can create a function for each shape.
Let the headline s = match with S. Division - & gt; True | _ - & gt; The wrong list.exists have a
, but I think there should be a more beautiful way in F # except for defining such a function for each shape type. What
a | Gt> Seq.groupBy (& lt; size type? & Gt;)
You can combine the F # image quote quotation to get a general solution
type size = | Float Board | Float Rectangle * Float Let Union Sees (C: Xp. & Lt; _ - & gt; 'T & gt;) = Match C. Lambda (_, Newlykese (UC, _)) - & gt; Tagrider = Microsoft.FSharp.Reflection.FSharpValue.PreComputeUnionTagReader (uci.DeclaringType) Enjoy (v: 'T) - & gt; (Tagreder v) = uci.Tag | _ - & gt; Failed "Invalid expression" a = [Circle 5.0; Rectangle (4.0, 6.0)] | & Gt; List.filter (isUnionCase & lt; @ Rectangle @ & gt; printf "% A" a
Comments
Post a Comment