f#

Create a byte array with dynamic size in F#

跟風遠走 提交于 2019-12-05 16:44:42
问题 In C# and Java a byte array can be created like this byte[] b = new byte[x]; where x denotes the size of the array. What I want to do is to do the same thing in F#. I have searched for how to do it and looked for it in the documentation. I think that I'm probably using the wrong search terms because I can't find out how. What I've found so far is that Array.create can be used like this: let b = Array.create x ( new Byte() ) Is there another way to do it which is more similiar to the way it

How do I test for exactly 2 characters with fparsec?

假装没事ソ 提交于 2019-12-05 16:12:11
I have the following program that runs. It takes a line of text and splits it into two parts, the first is an identifier and the second is the remainder of the line. My parser for the identifier (factID) takes any string of characters as the identifier, which is not (quite) what I want. What I want is a parser that only succeeds when it encounters two consecutive upper case letters. So for example "AA" should succeed while "A", "A1" or "AAA" should not. What I can't figure out is how construct a parser that looks for a fixed length token. I thought perhaps CharParsers.next2CharsSatisfy might

How do you return multiple values and assign them to mutable variables?

橙三吉。 提交于 2019-12-05 16:04:13
问题 This is what I have so far. let Swap (left : int , right : int ) = (right, left) let mutable x = 5 let mutable y = 10 let (newX, newY) = Swap(x, y) //<--this works //none of these seem to work //x, y <- Swap(x, y) //(x, y) <- Swap(x, y) //(x, y) <- Swap(x, y) //do (x, y) = Swap(x, y) //let (x, y) = Swap(x, y) //do (x, y) <- Swap(x, y) //let (x, y) <- Swap(x, y) 回答1: You can't; there's no syntax to update 'more than one mutable variable' with a single assignment. Of course you can do let newX,

Parsing method arguments with FParsec

浪尽此生 提交于 2019-12-05 15:58:23
I am trying to implement a method arguments parser with FParsec. I was wondering if there is some already implemented feature in FParsec itself that'd aid me on this purpose? I ask this as FParsec provides tooling when dealing with operator precedence, so there might be something for this too. Parsing the opening and closing braces is pretty straight-forward. The headache lies in dealing with the following 3 cases that can happen: Method arguments can consist of: no arguments, one argument, several arguments (all comma separated). keep in mind the last argument cannot be preceded by a comma! I

Compile with standalone flag gives compilation errors in client code

匆匆过客 提交于 2019-12-05 15:52:57
问题 I'm attempting to compile Zero29 with the --standalone compiler flag. The project itself compiles fine, but I have a unit test project that exercises some code in the Zero29 project, even though it's an executable program (.exe). Everything works fine without the --standalone compilation flag. However, when I add the --standalone compilation flag to the Zero29 project, the Zero29 project compiles fine, but in the unit test project, the compiler complains about this Discriminated Union defined

F# Custom Operators Precedence

痴心易碎 提交于 2019-12-05 14:54:55
问题 In F#, you can define custom operators like let (=~) input pattern = Regex.IsMatch(input, pattern) Unlike Haskell, custom operators are defined without precedences. What are the operator precedences of the custom operators in F#? 回答1: Operator precedence is determined by the first character(s), according to a predefined table. 来源: https://stackoverflow.com/questions/3347972/f-custom-operators-precedence

F# convert Array2 into a list

和自甴很熟 提交于 2019-12-05 14:42:49
I'm still new to functional programming so if I can't figure out how to do something I revert back to procedural style. I found a way to get around having to convert to a list but I'd still like to know how. Here is my attempt to convert a two dimensional array to a list. let board = Array2.init 10 20 (fun i j -> pull(i, j)) let mutable pieces = [] board |> Array2.mapi (fun i j a -> transform(i, j, a)) |> Array2.iter (fun a -> (pieces <- a :: pieces)) Brian Apparently in .Net, multi-dimensional arrays are IEnumerable (non-generic), and thus this works: let a2 = Array2.init 2 3 (fun x y -> (x+1

F# type providers with Portable Library

情到浓时终转凉″ 提交于 2019-12-05 14:42:18
I'm building a Windows 8 application and am finding myself wanting to include a list of ISO-4217 codes, which I have in up-to-date form in XML. Naturally, there are a couple of these codes for all countries. I figured a Type Provider would be an excellent fit. However, since they emit code, I can't use them with Portable Libraries. How do I compile the type provider so that it doesn't use emit, in order to use the XML I've got ? Tomas Petricek I don't have experience with creating a Type Provider that is useable from Portable Libraries, but you seem to be assuming that type providers emit code

Type inheritance in F#

谁说胖子不能爱 提交于 2019-12-05 14:38:26
I can't find the proper syntax for coding a type D that inherits a base class B (written in C#) and his constructors other than the base class implicit constructor: C# code: public class B { private int _i; private float _f; public B() { _i = 0; _f = 0.0f; } public B(int i) { _i = 0; _f = 0.0f; } public B(int i, float f) { _i = i; _f = f; } } F# code: type D() = inherit B() //how to inherit from other constructors ? Thanks I found a way to do it thanks this blog ! type D = class inherit B new () = { inherit B() } new (i : int) = { inherit B(i) } new ((i,f) : int*single) = { inherit B(i, f) }

F#: shortest way to convert a string option to a string

為{幸葍}努か 提交于 2019-12-05 14:29:07
The objective is to convert a string option that comes out of some nicely typed computation to a plain string that can then be passed to the UI/ printf /URL/other things that just want a string and know nothing of option types. None should just become the empty string. The obvious way is to do a match or an if on the input: input |> fun s -> fun s -> match s with | Some v -> v | _ -> "" or input |> fun s -> if s.IsSome then s.Value else "" but while still being one-liners, these still take up quite a lot of line space. I was hoping to find the shortest possible method for doing this. You can