f#

Why I can not use disposable objects in object members?

荒凉一梦 提交于 2019-12-10 22:38:57
问题 I don't want to add StreamWriter parameter to Write-To-File procedure but when I'm trying to work with disposable StreamWriter there I'm getting: An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. code: let fileLogger = { new IFlog with member i.FLog level format = use file = LogFile() Printf.kfprintf (fun f -> fprintfn file "[%s][%A] " <| level.ToString() <| DateTime.Now ) file (format) so when

Unquote: What am I missing?

社会主义新天地 提交于 2019-12-10 22:38:22
问题 I'm trying to get the Unquote library working with xUnit in .NET 4, and I'm not having any luck at all getting the tests to run. So, I broke it down to the simplest reproduction steps I could. Does anybody know what I might be missing? Create a new F# Library project. Use NuGet to add xUnit and Unquote references. Paste in the following code... I got the actual test directly from the Unquote home page, but the content doesn't matter, because it doesn't get run. module Tests open Xunit open

Constructing a record with a power of Applicative in F#

风流意气都作罢 提交于 2019-12-10 22:35:37
问题 Suppose there is a type r = {A : int; B : string; C : int; D : string} and a some values: let aOptional : int option = ... let bOptional : string option = ... let cOptional : int option = ... let dOptional : string option = ... How can r optional could be constructed from them eleganlty (without nested case-stuff, etc.)? Btw, here is how it could be done in haskell with Control.Applicative: data R = R { a :: Integer, b :: String, c :: Integer, d :: String} R <$> aOptional <*> bOptional <*>

F#: How do I declare and use ByRef semantics on parameters?

时间秒杀一切 提交于 2019-12-10 22:28:24
问题 I am wraping some SQL Stored Procs with in/out parameters. This of course means I have to do ugly stuff like declare my parameters as by reference and use mutables. How would I do this in F#? 回答1: F# does indeed have a byref parameter. Here's an example from the MSDN page: type Incrementor(z) = member this.Increment(i : int byref) = i <- i + z Mutable variables also exist (though there's an important difference between using ref and mutable variables, either of which can be used for many of

F#: In which memory area is the continuation stored: stack or heap?

元气小坏坏 提交于 2019-12-10 22:28:20
问题 Question It is possible to make every recursive function tail recursive by follow the continuation-passing style (CPS). As I understand it you put everything after the first recursive call into a function and hand it over to the same call. Therefore the recursive call is the last statement in the function and the compiler is able to do the tail call optimization. This means that the recursion is replaced by a loop. There are no additional stack frames consumed. The continuation is a function

Order of fields in a type for FileHelpers

帅比萌擦擦* 提交于 2019-12-10 22:25:20
问题 I'm reading a simple CSV file with Filehelpers - the file is just a key, value pair. (string, int64) The f# type I've written for this is: type MapVal (key:string, value:int64) = new()= MapVal("",0L) member x.Key = key member x.Value = value I'm missing something elementary here, but FileHelpers always assumes the order of fields to be the reverse of what I've specified - as in Value, Key. let dfe = new DelimitedFileEngine(typeof<MapVal>) let recs = dfe.ReadFile(@"D:\e.dat") recs |> Seq

F#: Attempt to memoize member function resets cache on each call?

六眼飞鱼酱① 提交于 2019-12-10 22:00:12
问题 I'm trying to memoize a member function of a class, but every time the member is called (by another member) it makes a whole new cache and 'memoized' function. member x.internal_dec_rates = let cache = new Dictionary< Basis*(DateTime option), float*float>() fun (basis:Basis) (tl:DateTime option) -> match cache.TryGetValue((basis,tl)) with | true, (sgl_mux, sgl_lps) -> (sgl_mux, sgl_lps) | _ -> let (sgl_mux, sgl_lps) = (* Bunch of stuff *) cache.Add((basis,tl),(sgl_mux,sgl_lps)) sgl_mux,sgl

How to show labels on x axis for each element?

戏子无情 提交于 2019-12-10 21:57:51
问题 I have sequence of tuples containing filename and number. I want to draw column graph where on X axis I have filenames. My problem is that now only 3 labels (filenames) are shown under X axis. That's probably because more can't fit on screen. Or maybe X axis interval is wrong? How to make chart display all filenames? Maybe there is a way to rotate those labels 90 degrees counterclockwise to make room for more labels? 回答1: You should be able to use: |> Chart.WithXAxis (LabelStyle = ChartTypes

fparsec to parse sequence of string

随声附和 提交于 2019-12-10 21:45:18
问题 I have an user input text like "abc,def,ghi". I want to parse it to get list of string as ["abc", "def"]. I tried let str : Parser<_> = many1Chars (noneOf ",") let listParser : Parser<_> = many (str);; but it always give me the first item only ["abc"]. "Def" and others are not coming in result list 回答1: You're parsing up to the first comma, but not parsing the comma itself. To parse a list of things separated by other things, use sepBy: let comma = pstring "," let listParser = sepBy str comma

WebSharper F# - How to run a template project created with VS Code and Ionide?

谁说我不能喝 提交于 2019-12-10 21:44:09
问题 I have never worked with .NET before and I would like to know how to run a WebSharper F# project without any IDE. Context I'm running Linux with Mono 4.4.2 The project was created with VS Code and Ionide, using the template websharperserverclient I'm able to compile the code using the automatically generated file build.sh or by executing xbuild , but only .dll files are generated, I couldn't see any .exe I thank in advance for any help! Updates Using websharperserverclient I get weird results