f#

Why do I need to use the unit type in F# if it supports the void type?

一笑奈何 提交于 2019-12-12 07:06:59
问题 I read this MSDN article: Unit Type (F#) ...The unit type is a type that indicates the absence of a specific value; the unit type has only a single value, which acts as a placeholder when no other value exists or is needed ... The unit type resembles the void type in languages such as C# and C++... So... Alright, I understand, that the unit type is such a type, which has only a single value () . But I have some questions: Why is it needed? When is it needed? I don't understand why not to use

F# take subsequence of a sequence

半腔热情 提交于 2019-12-12 04:53:00
问题 I need to define a function, which takes a sequence and integers i and n as parameters and returns sub list or sub sequence of this seq, defined as followed: . 回答1: You can write: let subSeq i n = Seq.skip i >> Seq.take n Which is used, e.g.: let result = subSeq 5 10 [0..20] 来源: https://stackoverflow.com/questions/34093543/f-take-subsequence-of-a-sequence

FsUnit: Unable to test portable library due to it and test project having different F#.Core versions

我们两清 提交于 2019-12-12 04:27:17
问题 I have a Portable Library, for which the FSharp.Core version is 3.7.4.0 . Installing (in the Unit Test project) FsUnit installs, as a dependency, FSharp.Core version 3.1.2.5 . Due to this, using the portable library's functions in my Unit Test project, for example: module StammaTests.PieceTests open Stamma open NUnit.Framework open FsUnitTyped [<Test>] let ``Testing a Basic function`` () = Piece.toChar Black King |> shouldEqual 'k' yields error: Result Message: System.IO.FileLoadException :

how to use c# struct from f#

送分小仙女□ 提交于 2019-12-12 04:21:58
问题 what's the syntax for using a c# struct in f#? how do i assign it's fields values? thanks! update: looks like i can declare the variable itself mutable and then set it's fields using the <- operator...is there another way? 回答1: looks like i can declare the variable itself mutable and then set it's fields using the <- operator...is there another way? This is the correct thing to do. let mutable someStruct = CallSomething() someStruct.Field1 <- 42 // etc. 回答2: Do you require something like that

F# Multiple Attributes CLIMutable DataContract

血红的双手。 提交于 2019-12-12 03:59:13
问题 I am running into an issue with combining attributes when using ServiceStack.Redis with f#. Maybe I am thinking about this wrong but right now I'd like my type to be seralized to JSON but also passable to ServicvStack. The issue with f# types is that there is no default constructor and this the data added to my Redis instance is emtpy, well the record is there but none of the data inside it is there. Here is an example: open System open ServiceStack.Redis [<CLIMutable>] [<DataContract>] type

FParsec - How to parse from standard input stream

强颜欢笑 提交于 2019-12-12 03:56:29
问题 I can't seem to successfully parse from standard input stream with FParsec. I reduced my case to this very simple code : match (runParserOnStream (pstring "test" .>> FParsec.CharParsers.newline) () "stdin" (Console.OpenStandardInput ()) Console.InputEncoding) with | Success(result, _, _) -> printfn "Success: %A" result | Failure(errorMsg, perr, _) -> printfn "Failure: %s" errorMsg But when i run the program, enter the string test, and then press Enter, it hangs there, and i can't seem to

jQuery Ajax call to F#?

北城以北 提交于 2019-12-12 03:56:15
问题 Is there any way to make a call to an f# library using jQuery without having to wrap it into a web service method? 回答1: As mentioned in comments, you could translate F# to JavaScript. This is actually pretty doable, because it can use quotations (a high-level representation of F# code). There are two projects that do this. One is a rather sophisticated commercial project named WebSharper (which has other features too) and the other one is open source project FSharp.Javascript. If you want to

F# generics / function overloading syntax

旧时模样 提交于 2019-12-12 03:34:08
问题 I'm confused on how to label a function as generic without an explicit type declaration like ('a -> 'a) let add a b = a + b This gives us val add : a:int -> b:int -> int However we can then immediately call add "Hello " "World!" and now the value of add is val add : a:string -> b:string -> string val it : string = "Hello World!" If we then call add 2 3 // then we get error: This expression was expected to have type string but here has type int How do I ensure that a function works on all

Forward type declaration with two files

戏子无情 提交于 2019-12-12 03:33:53
问题 I have two files. File 1: namespace A type A1() = // .... File 2: namespace A type A2() = // .... I can use A1 and A2 types in File 2, but I can see only A1 in File 1. I think it is because compiler don't know about A2 when it is parsing File 1. Is there any way to make something like forward type declaration in this case ? Or to change the order of parsing the source files by compiler. It will be enough for me to have A1 and A2 in File1, and only A2 in File1. Update: I believe it is not

Set.union F# trouble

微笑、不失礼 提交于 2019-12-12 03:32:27
问题 I have a Set<String*String> , and I'm trying to write a function that takes all the elements of that Set and return a Set<String> . My idea is to use Set.fold, and have an empty set accumulator and take the union of the two sets, but I'm running into problems. Here is the code: type Chart = Set<Country*Country>;; let countriesInChart (chart : Chart) = Set.fold(fun (x,y) set -> Set.union [x;y] set ) chart [] But I get this error Set.fold(fun (x,y) set -> Set.union [x;y] set ) chart [];; ------