f#

MissingMethodException when running a unit test that uses FSharp.Data

≯℡__Kan透↙ 提交于 2019-12-08 19:18:38
问题 I have a NUnit unit test that is written in a normal F# library but targets F# code in a Portable Class Library. When I run this test (in Visual Studio 2013), I get the following exception: Result Message: System.MissingMethodException : Method not found: 'Microsoft.FSharp.Control.FSharpAsync`1<System.IO.TextReader> FSharp.Data.Runtime.IO.asyncReadTextAtRuntime(System.Boolean, System.String, System.String, System.String, System.String)'. This is what I have in my app.config in the Portable

The non-generic type Type does not expect type arguments

旧城冷巷雨未停 提交于 2019-12-08 19:13:33
I am creating a simple test type provider. I want to provide a string, and return a type with the type name equal to that provided string. But the result doesn't work, saying that BasicProvider is a non-generic type. Error: The non-generic type 'SimpleStringProvider.BasicProvider' does not expect any type arguments, but here is given 1 type argument(s) module SimpleStringProvider open ProviderImplementation open ProviderImplementation.ProvidedTypes open Microsoft.FSharp.Core.CompilerServices open System.Reflection [<TypeProvider>] type BasicProvider (config : TypeProviderConfig) as this =

How to create a type that implement IDictionary<'K, 'V> and IEnumerable<'V>

守給你的承諾、 提交于 2019-12-08 19:03:13
问题 I want to create a read-only keyed collection that implements IDictionary<'K, 'V> and IEnumerable<'V>. Taking the obvious approach I get the following error: This type implements or inherits the same interface at different generic instantiations 'IEnumerable<'V>' and 'IEnumerable<KeyValuePair<'K,'V>>'. This is not permitted in this version of F#. Is there a different way of achieving this? EDIT - Since this seems to be an insurmountable limitation of F#, what would be an idiomatic way of

F# structural tuples versus BCL Tuple types

微笑、不失礼 提交于 2019-12-08 18:35:08
问题 In F# you can define a first function as follows: let first (x, y) = x You can call it like this: first (1, 2) You can also define the same function in terms of the BCL Tuple type: let first (t:Tuple<_, _ >) = t.Item1 However, you cannot call it using the prior syntax, or you will get the following error: error FS0001: The type ''c * 'd' is not compatible with the type 'Tuple<'a,'b>' Instead, you have to do the following: first (Tuple<_,_>(1, 2)) This is strange, since compiled F# code does

Functional programming and dependency inversion: how to abstract storage?

社会主义新天地 提交于 2019-12-08 18:33:42
问题 I'm trying to create a solution that has a lower-level library that will know that it needs to save and load data when certain commands are called, but the implementation of the save and load functions will be provided in a platform-specific project which references the lower-level library. I have some models, such as: type User = { UserID: UserID Situations: SituationID list } type Situation = { SituationID: SituationID } And what I want to do is be able to define and call functions such as:

Creating Delegates With Lambda Expressions in F#

↘锁芯ラ 提交于 2019-12-08 18:12:49
问题 Why does... type IntDelegate = delegate of int -> unit type ListHelper = static member ApplyDelegate (l : int list) (d : IntDelegate) = l |> List.iter (fun x -> d.Invoke x) ListHelper.ApplyDelegate [1..10] (fun x -> printfn "%d" x) not compile, when: type IntDelegate = delegate of int -> unit type ListHelper = static member ApplyDelegate (l : int list, d : IntDelegate) = l |> List.iter (fun x -> d.Invoke x) ListHelper.ApplyDelegate ([1..10], (fun x -> printfn "%d" x)) does? The only

Are the double forward/backward pipe operators documented?

不羁岁月 提交于 2019-12-08 18:03:35
问题 I remember reading about the double pipe operators -- ||> and <|| -- somewhere and now I can't remember where. I can't find them on MSDN or in the language spec. Are they documented anywhere? Example let print a b = sprintf "%O %O" a b (1, 2) ||> print // val it : string = "1 2" 回答1: Double (forward/backward) pipe operators are documented in the list of F# operators on MSDN and are also documented as a function exported from the Core.Operators module. This is probably automatically generated

Converting a list of strings into floats/ints in F#

时光毁灭记忆、已成空白 提交于 2019-12-08 17:38:22
问题 Is there a quick and simple way to convert an entire list of strings into floats or integers and add them together similar to this in F#? foreach(string s in list) { sum += int.Parse(s); } 回答1: Something like this should have the same effect: let sum = list |> Seq.map System.Int32.Parse |> Seq.sum F# doesn't seem to support referring to the method on int so I had to use System.Int32 instead. In F# the type seq is an alias for the .NET IEnumerable , so this code works on arrays, lists etc.

F#: how to evaluate a “seq” to get all its values eagerly?

淺唱寂寞╮ 提交于 2019-12-08 17:35:58
问题 We know that in F#, seq is lazy evaluated. My question is, if I have a seq with limited number of values, how to convert it into some data type that contains all its value evaluated? > seq { for i in 1 .. 10 do yield i * i };; val it : seq<int> = seq [1; 4; 9; 16; ...] Thanks a lot. 回答1: The answer from @Carsten is correct: you can use Seq.toArray or Seq.toList if you wish to convert lazily evaluated sequences to lists or arrays. Don't use these function to force evaluation, though. The most

Why is `unit` treated differently by the F# type system when used as a generic interface argument?

﹥>﹥吖頭↗ 提交于 2019-12-08 17:30:10
问题 Consider this interface: type A<'a> = abstract X : 'a Let's try to implement it with int as a generic argument: { new A<int> with member this.X = 5 } // all is well Now, let's try unit for an argument: // Compiler error: The member 'get_X : unit -> unit' does not have the correct type to override the corresponding abstract method. { new A<unit> with member this.X = () } Now, if we define a non-generic interface, everything also works well: type A_int = abstract X : int { new A_int with member