Thursday, 15 August 2013

.net - Maximum / minimum value of type in F# -



.net - Maximum / minimum value of type in F# -

unchecked.defaultof<'t> generates default value type. there such generic function generate maximum / minimum value type type having maximum / minimum value makes sense?

edit

to reply john palmer's question of think useful: want create "mutable" version of below function:

let internalarrdiffmax (s : 't []) (diff : 't -> 't -> 'c) = s |> array.mapi (fun e -> [| j in + 1 .. s.length - 1 -> diff e s.[j] |] |> array.maxby (fun n -> n)) |> array.maxby (fun e -> e)

since can't declare mutable variable without assigning value it, don't think there other way than:

let internalarrdiffmax (s : 't []) (difffun : 't -> 't -> 'c) = allow mutable max : 'c = // generic max of 'c if makes sense in 0 .. s.length - 1 j in + 1 .. s.length - 1 allow diff = difffun s.[i] s.[j] if (i = 0 && j = 1) || max < diff max <- diff max

which why think need generic max.

if must proceed on route, there's reflection. however, advise against utilize of maxvalue out-of-band or special value.

let inline trygetmaxvalue< ^a> () = match typeof< ^a>.getfield("maxvalue") | null -> none | fieldinfo -> fieldinfo.getvalue() |> unbox< ^a> |> allow maxvi = trygetmaxvalue<int>() // val maxvi : int alternative = 2147483647 allow maxvf : float alternative = trygetmaxvalue() // val maxvf : float alternative = 1.797693135e+308 allow maxvs : string alternative = trygetmaxvalue() // val maxvs : string alternative = none

.net f# max minimum unchecked

No comments:

Post a Comment