f#

F# Records: Dangerous, only for limited use, or well used functionality?

末鹿安然 提交于 2020-01-22 19:52:08
问题 So have gotten to record in my F# journey and at first they seem rather dangerous. At first this seemed clever: type Card = { Name : string; Phone : string; Ok : bool } let cardA = { Name = "Alf" ; Phone = "(206) 555-0157" ; Ok = false } The idea that the cardA is patten matched with Card. Not to mention the simplified pattern matching here: let withTrueOk = list |> Seq.filter (function | { Ok = true} -> true | _ -> false ) Problem is: type Card = { Name : string; Phone : string; Ok : bool }

How to read F# type signatures?

血红的双手。 提交于 2020-01-22 13:50:06
问题 I'm struggling with the F# type signature notation. For example let's say you have a Fold function: let rec Fold combine acc l = ... that may have this type signature: ('a -> 'b -> 'a) -> 'a -> list<'b> -> 'a which I would read as a function that has three arguments: a function that takes an 'a, a 'b and returns an a' an 'a a list of 'b and returns an 'a. But then it would make more sense for my cavemen brain to express it as ('a, 'b -> 'a), 'a, list<'b> -> 'a I'm sure there is a semantic

The F# equivalent of C#'s 'out'

一世执手 提交于 2020-01-22 13:49:12
问题 I am rewriting a C# library to F# and I need to translate the following code bool success; instance.GetValue(0x10, out success); what is the equivalent of the out keyword in F#? 回答1: Neither wasatz's answer nor Max Malook's is complete. There are three ways of calling methods with out parameters. The second and third ways also work with ref parameters. For the examples, assume the following type: open System.Runtime.InteropServices //for OutAttribute type SomeType() = member this.GetValue

Scalability of the .NET 4 garbage collector

大兔子大兔子 提交于 2020-01-22 05:48:24
问题 I recently benchmarked the .NET 4 garbage collector, allocating intensively from several threads. When the allocated values were recorded in an array, I observed no scalability just as I had expected (because the system contends for synchronized access to a shared old generation). However, when the allocated values were immediately discarded, I was horrified to observe no scalability then either! I had expected the temporary case to scale almost linearly because each thread should simply wipe

F# return ICollection

梦想的初衷 提交于 2020-01-21 08:49:52
问题 I'm working with a library created in C#. I've been working on porting some code to F# but must use quite a few underlying types from the C# lib. One piece of code needs to calculate a list of values and assign it to a public field/property in the class. The field is a C# class that contains two ICollection. My F# code works fine and needs to return an F# Seq/List. I tried the following code snippets which each produce errors. Return type of F# member is a type called recoveryList with type

F# return ICollection

萝らか妹 提交于 2020-01-21 08:48:29
问题 I'm working with a library created in C#. I've been working on porting some code to F# but must use quite a few underlying types from the C# lib. One piece of code needs to calculate a list of values and assign it to a public field/property in the class. The field is a C# class that contains two ICollection. My F# code works fine and needs to return an F# Seq/List. I tried the following code snippets which each produce errors. Return type of F# member is a type called recoveryList with type

F# passing an operator with arguments to a function

ⅰ亾dé卋堺 提交于 2020-01-21 04:17:37
问题 Can you pass in an operation like "divide by 2" or "subtract 1" using just a partially applied operator, where "add 1" looks like this: List.map ((+) 1) [1..5];; //equals [2..6] // instead of having to write: List.map (fun x-> x+1) [1..5] What's happening is 1 is being applied to (+) as it's first argument, and the list item is being applied as the second argument. For addition and multiplication, this argument ordering doesn't matter. Suppose I want to subtract 1 from every element (this

How to display an FSharp.Charting graph in an existing form?

白昼怎懂夜的黑 提交于 2020-01-21 03:40:27
问题 I don't understand how to create a chart control and place the chart in an existing form. All the examples I found on the web show the chart in a new form but I would like to add the chart to one of my existing forms. I'm thinking of something like this: let form = new Form(Text="My form") let lbl = new Label(Text="my label") let chart = Chart.Area ["a", 10; "b", 20] form.Controls.Add lbl form.Controls.Add chart // ---> The type 'ChartTypes.GenericChart' is not compatible with the type

How to define optional parameters in F# modules?

家住魔仙堡 提交于 2020-01-16 08:38:09
问题 I'm trying to re-write a tiny C# lib in F# and I've encountered an error. I'm trying to define optional parameters for a method in a module but the compiler says "Optional arguments are only permitted on type members". I've checked why you can't use them in loose functions but when typing static member or member I get another error instead. module Kingdom = let Rule (?years : int) = () I thought this was going to workas it's how I understood you type it, after reading the Microsoft Docs

Using COM DLLs with FSI

為{幸葍}努か 提交于 2020-01-16 08:16:14
问题 Is there a way within FSI that I can reference and use registered COM components? In a normal .fs compiled program I can simply reference the component in question and then open the relevant generated namespace(s). In a .fsx file, however, I can't seem to replicate this behaviour. I have tried using #r to reference the .dll directly, and I have tried using #I to point to the directory followed #r both with the library's "friendly" name and the file name, but nothing seems to work. Are you