f#

Ranges A to B where A > B in F#

拟墨画扇 提交于 2019-12-23 07:44:20
问题 I've just found something I'd call a quirk in F# and would like to know whether it's by design or by mistake and if it's by design, why is it so... If you write any range expression where the first term is greater than the second term the returned sequence is empty. A look at reflector suggests this is by design, but I can't really find a reason why it would have to be so. An example to reproduce it is: [1..10] |> List.length [10..1] |> List.length The first will print out 10 while the second

How can I define an extension member on an F# unit of measure?

别等时光非礼了梦想. 提交于 2019-12-23 07:35:28
问题 Leaving aside whether we should use units of measure for unitless concepts like angles, suppose I have define degree and radian units in F# type [<Measure>] degree = static member ToRadians (d:float<degree>) : float<radian> = d * (Math.PI * 1.<radian>) / 180.0<degree> and [<Measure>] radian = static member ToDegrees (r:float<radian>) : float<degree> = r * 180.0<degree> / (Math.PI * 1.<radian>) I can use them relatively easily like 4.0<degree> |> degree.ToRadians It seems like extension

What's the difference between `ImmutableSortedSet` and fsharp `Set`?

偶尔善良 提交于 2019-12-23 07:29:18
问题 BCL has introduced a group of Immutable Collections I am wondering what's the difference between ImmutableSortedSet and the native FSharp Set ? It seems that the performance signatures of both are similar. Also I saw somewhere that SortedSet is implemented as a Red Black Tree, so I guess ImmutableSortedSet does the same. What is the internal implementation of fsharp map ? Is is Red Black Tree as claimed here or AVL tree as found out here? In addition, why MSDN documents don't state clear what

Idioms/Practices for Implementing Constrained Numeric Types in F#?

守給你的承諾、 提交于 2019-12-23 07:29:12
问题 Suppose one needs a numeric data type whose allowed values fall within a specified range. More concretely, suppose one wants to define an integral type whose min value is 0 and maximum value is 5000. This type of scenario arises in many situations, such as when modeling a database data type, an XSD data type and so on. What is the best way to model such a type in F#? In C#, one way to do this would be to define a struct that implemented the range checking overloaded operators, formatting and

What's the difference between `ImmutableSortedSet` and fsharp `Set`?

。_饼干妹妹 提交于 2019-12-23 07:28:44
问题 BCL has introduced a group of Immutable Collections I am wondering what's the difference between ImmutableSortedSet and the native FSharp Set ? It seems that the performance signatures of both are similar. Also I saw somewhere that SortedSet is implemented as a Red Black Tree, so I guess ImmutableSortedSet does the same. What is the internal implementation of fsharp map ? Is is Red Black Tree as claimed here or AVL tree as found out here? In addition, why MSDN documents don't state clear what

Fully qualified name, unqualified name with import declaration resolve differently

落花浮王杯 提交于 2019-12-23 07:15:07
问题 This works open System let f = Action(fun () -> Unchecked.defaultof<_>) But this let f = System.Action(fun () -> Unchecked.defaultof<_>) produces the compilation error Multiple types exist called 'Action', taking different numbers of generic parameters. Provide a type instantiation to disambiguate the type resolution, e.g. 'Action< , ,_, , ,_, , ,_>'. I know I can fix it by adding a type parameter placeholder ( System.Action<_>(...) ), but any idea why they behave differently? EDIT Found this

Fully qualified name, unqualified name with import declaration resolve differently

随声附和 提交于 2019-12-23 07:14:07
问题 This works open System let f = Action(fun () -> Unchecked.defaultof<_>) But this let f = System.Action(fun () -> Unchecked.defaultof<_>) produces the compilation error Multiple types exist called 'Action', taking different numbers of generic parameters. Provide a type instantiation to disambiguate the type resolution, e.g. 'Action< , ,_, , ,_, , ,_>'. I know I can fix it by adding a type parameter placeholder ( System.Action<_>(...) ), but any idea why they behave differently? EDIT Found this

Result vs raise in F# async?

情到浓时终转凉″ 提交于 2019-12-23 07:08:29
问题 It seems like there are two ways to return errors in an async workflow: raise and Result . let willFailRaise = async { return raise <| new Exception("oh no!") } let willFailResult = async { return Result.Error "oh no!" } For the caller, the handling is a bit different: async { try let! x = willFailRaise // ... with error -> System.Console.WriteLine(error) } async { let! maybeX = willFailResult match maybeX with | Result.Ok x -> // ... | Result.Error error -> System.Console.WriteLine(error) }

Why can't non-partial active patterns be parameterized in F#?

与世无争的帅哥 提交于 2019-12-23 06:58:59
问题 The following F# code works as I expected, printing `Matched as 'A': let (|Char|_|) convf = function | LazyList.Nil -> None | LazyList.Cons (x, _) -> Some (convf x) let test = function | Char System.Char.ToUpper x -> printfn "Matched as %A" x | _ -> printfn "Didn't match" test (LazyList.of_list ['a']) However, if I change Char from a partial active pattern to a complete active pattern as follows: let (|Char|NoChar|) convf = function | LazyList.Nil -> NoChar | LazyList.Cons (x, _) -> Char x

What's the purpose of `id` function in the FSharp.Core?

☆樱花仙子☆ 提交于 2019-12-23 06:47:19
问题 From Operators.id<'T> Function (F#): The identity function. Parameters: x Type: 'T (The input value) Return Value: The same value F# Core Library Versions, supported in: 2.0, 4.0, Portable Why is there a function that returns its input? 回答1: When working with higher-order functions (i.e. functions that return other functions and/or take other functions as parameters), you always have to provide something as parameter, but there isn't always an actual data transformation that you'd want to