f#

How do I create an F# function with a printf style logging argument?

旧街凉风 提交于 2019-12-18 05:48:11
问题 I'm trying to create a framework to do some processing of files and data. The one area I'm struggling with is how to provide a logging function to the framework, allowing the framework to report messages without having any knowledge of the logging in use. let testLogger (source:seq<'a>) logger = logger "Testing..." let length = source |> Seq.length logger "Got a length of %d" length let logger format = Printf.kprintf (printfn "%A: %s" System.DateTime.Now) format testLogger [1; 2; 3] logger

Is it possible to implement a recursive “SelectMany”?

[亡魂溺海] 提交于 2019-12-18 05:45:05
问题 As we all know, Enumerable.SelectMany flattens a sequence of sequences into a single sequence. What if we wanted a method that could flatten sequences of sequences of sequences, and so on recursively? I came up quickly with an implementation using an ICollection<T> , i.e. eagerly evaluated, but I'm still scratching my head as to how to make a lazily-evaluated one, say, using the yield keyword. static List<T> Flatten<T>(IEnumerable list) { var rv = new List<T>(); InnerFlatten(list, rv); return

Why does F#'s printfn work with literal strings, but not values of type string?

泄露秘密 提交于 2019-12-18 05:43:50
问题 In the following F# code; I would expect that the printfn is being called three times; each with a string. However, the bottom line does not compile ( The type 'string' is not compatible with the type 'Printf.TextWriterFormat<'a>' ). What is it about the first two lines that means this can work? Aren't they just strings too? open System printfn ("\r\n") // Works printfn ("DANNY") // Works printfn (DateTime.Now.ToLongTimeString()) // Doesn't compile 回答1: The F# compiler statically analyses the

Using the F# pipe symbol with an object constructor

为君一笑 提交于 2019-12-18 05:41:34
问题 I'm trying to figure out the correct syntax to use the pipe operator |> into the creation of an object. Currently I'm using a static member to create the object and just piping to that. Here is the simplified version. type Shape = val points : Vector[] new (points) = { points = points; } static member create(points) = Shape(points) static member concat(shapes : Shape list) = shapes |> List.map (fun shape -> shape.points) |> Array.concat |> Shape.create What I want to do ... static member

Using the F# pipe symbol with an object constructor

五迷三道 提交于 2019-12-18 05:41:31
问题 I'm trying to figure out the correct syntax to use the pipe operator |> into the creation of an object. Currently I'm using a static member to create the object and just piping to that. Here is the simplified version. type Shape = val points : Vector[] new (points) = { points = points; } static member create(points) = Shape(points) static member concat(shapes : Shape list) = shapes |> List.map (fun shape -> shape.points) |> Array.concat |> Shape.create What I want to do ... static member

F# - Function with no arguments?

笑着哭i 提交于 2019-12-18 05:27:25
问题 When thinking in a functional mindset, given that functions are supposed to be pure, one can conclude any function with no arguments is basically just a value. However, reallity gets in the way, and with different inputs, I might not need a certain function, and if that function is computationally expensive, I'd like to not evaluate it if it's not needed. I found a workaround, using let func _ = ... and calling it with func 1 or whatever, but that feels very non-idiomatic and confusing to the

xUnit v1 tests appear in xUnit GUI (xunit.gui.clr4.exe) but not VS 2012 Test Explorer

微笑、不失礼 提交于 2019-12-18 05:05:11
问题 I have an F# Class Library with the "xUnit.net" and "xUnit.net Runners" packages installed using NuGet. I have the following code: module XUnitTest open Xunit [<Fact>] let Test () = do Assert.True (1 = 2) () When I run the xUnit GUI (xunit.gui.clr4.exe, which NuGet adds to (projectdirectory)\packages\xunit.runners.1.9.1\tools), and load the assembly built by this project, the Test () method appears, and fails when I run it, as expected. However, I cannot get the test to appear in VS 2012's

xUnit v1 tests appear in xUnit GUI (xunit.gui.clr4.exe) but not VS 2012 Test Explorer

女生的网名这么多〃 提交于 2019-12-18 05:05:06
问题 I have an F# Class Library with the "xUnit.net" and "xUnit.net Runners" packages installed using NuGet. I have the following code: module XUnitTest open Xunit [<Fact>] let Test () = do Assert.True (1 = 2) () When I run the xUnit GUI (xunit.gui.clr4.exe, which NuGet adds to (projectdirectory)\packages\xunit.runners.1.9.1\tools), and load the assembly built by this project, the Test () method appears, and fails when I run it, as expected. However, I cannot get the test to appear in VS 2012's

Calculating the Cartesian product of a list of numbers with F#

我与影子孤独终老i 提交于 2019-12-18 04:55:21
问题 I am new to f# I am try to calculate the Cartesian products of a list of numbers. I "borrowed" this. let xs = [1..99] let ys = [1..99] seq {for x in xs do for y in ys do yield x * y} Is there a better or more elegant way? Gary 回答1: Another possibiltiy to tackle the problem based on functionality provided by the List module would be: let xs = [1..99] let ys = [1..99] let zs = xs |> List.collect (fun x -> ys |> List.map (fun y -> x*y)) which avoids the extra calls to .concat and should also do

Inline function and type extension

本小妞迷上赌 提交于 2019-12-18 04:53:02
问题 Consider I have two different library types: type Foo = { foo : string } type Bar = { bar : int32 } I want to implement generic function zoo that will work for either Foo or Bar instances. And I cannot change Foo and Bar because they are part of library code. Here's my first attempt using type extensions and inline function as explained here: // Library.fs module Library type Foo = { foo : string } type Bar = { bar : int32 } // Program.fs type Foo with static member zoo (f : Foo) = "foo" type