f# - Generalizing a new operator over many types -
i using unquote , did not see approximate comprison. decided write one.
let inline (=~=) x y = abs x-y < 1.e-10 however operator not mapped onto, lists
let test = [1;2] =~= [1;2] //---> error is possible declare operator flow (=) ?
or require define new traits 'structuralequality-ishness"?
is better define new operator with, say, http://code.google.com/p/fsharp-typeclasses/ ?
i don't know unquote, regarding approximate function/operator i'm not sure if there way implement structural comparison.
if want "by hand", using technique (or trick) similar 1 used f# typeclasses project, here example:
type approximate = approximate static member inline ($) (approximate, x:^n ) = fun (y:^n) -> float (abs (x-y)) < 1.e-10 static member inline ($) (approximate, x:list< ^n>) = fun (y:list< ^n>) -> x.length = y.length && (list.zip x y |> list.forall ( fun (a,b) -> (approximate $ a) b)) // more overloads let inline (=~=) x y = (approximate $ x) y
Comments
Post a Comment