c#-to-f#

Euler 23 in C#: 0.2 seconds, in F#: 30.5 seconds. Why?

会有一股神秘感。 提交于 2019-12-20 03:11:33
问题 I am not really satisfied with my F# solution to this problem because I can't find a beautiful & fast solution, but that's not the issue here. The issue is, I translated the solution to C# for the heck of it, and it is fast. Like, really fast, comparatively. I can't figure out why. Yes, I've been to Reflector, C# code looks very similar, can't say I really understand IL but it looks kinda like the same. The only thing I can think of is performance of F# int[] against C# List. So here it goes:

XML documentation for interface methods and record properties

不羁的心 提交于 2019-12-20 02:26:08
问题 It seems the XML documentation works fine for the most cases, but not always. I wanted to make the Intellisense fully available for the parts that are designed for interoperating with C#. So, here's a small (and maybe a bit contrived) example: ///<summary>Well, it's a summary</summary> type Summary = { ///<summary>Gets a short name</summary> Name : string; ///<summary>Gets whether the action was successful or not</summary> IsSuccessful : bool; } ///<summary>Represents path filtering action<

C# object initialization syntax in F#

你离开我真会死。 提交于 2019-12-19 12:38:11
问题 Please note: this question is not the same as this question. I recently came across some C# syntax I hadn't previously encountered: Is there any way to do this in F#? class Two { public string Test { get; set; } } class One { public One() { TwoProperty = new Two(); } public Two TwoProperty { get; private set; } } var test = new One(){ TwoProperty = { Test = "Test String" }}; (note the initialization of TwoProperty in the initializer when the setter is private - it is setting a property on the

C# async / await method to F#?

若如初见. 提交于 2019-12-18 19:42:29
问题 I am trying to learn F# and am in the process of converting some C# code to F#. I have the following C# method: public async Task<Foo> GetFooAsync(byte[] content) { using (var stream = new MemoryStream(content)) { return await bar.GetFooAsync(stream); } } Where bar is some private field and GetFooAsync returns a Task<Foo> . How does this translate to F#? Here is what I currently have: member public this.GetFooAsync (content : byte[]) = use stream = new MemoryStream(content) this.bar

Using c# delegates with f# functions

北城以北 提交于 2019-12-17 20:21:29
问题 I am trying to call a c# function from f# where the c# function takes a function (delegate?) as a parameter and I need this argument to be a f# function. Eg: Sample c# public static void function_1(double x, ref double y) { y = Math.Exp(x); } main() { state s; call_func(s, function_1) } So, call_func has a parameter of type void fn(double, ref double) In f# I tried: let function_1 (x:double) (y:double byref) = let y = 6.0 () let test = let s = new state let ret = call_func(s, function_1) But

Method Chaining vs |> Pipe Operator

隐身守侯 提交于 2019-12-17 19:07:24
问题 So I have the following code: // Learn more about F# at http://fsharp.net open System open System.Linq open Microsoft.FSharp.Collections let a = [1; 2; 3; 4; 54; 9] let c = a |> List.map(fun(x) -> x*3) |> List.filter(fun(x) -> x > 10) let d = a.Select(fun(x) -> x*3).Where(fun(x) -> x > 10) for i in c do Console.WriteLine(i) for i in d do Console.WriteLine(i) Both seem to do the same thing, but most F# examples I see use the |> pipe operator, while I'm more used to method chaining (a.l.a. C#

F# Equivalent of Destructor

余生长醉 提交于 2019-12-14 03:40:08
问题 I am translating a C# class that wraps an unmanaged library to F#. I have run into the seemingly simple problem of rewriting the destructor that follows. class Wrapper { // P/Invoke ellided private SomeType x; public Wrapper() { x = new SomeType(); Begin(); } public ~Wrapper() { End(); } The simplified F# code I have at this point is as follows: type Wrapper() = [<Literal>] static let wrappedDll = "Library.dll" [<DllImport(wrappedDll , EntryPoint = "Begin")>] static extern void Begin() [

Using Ninject with mocks in F#

♀尐吖头ヾ 提交于 2019-12-13 18:36:03
问题 This question is part of larger question that can be found here As in out production code we use Ninject and constructor injection our services tend to look like this public class Service : IService { private readonly IRepository _repository; public Service(IRepository repository) { _repository = repository; } public Task<IEnumerable<SelectOption>> GetAlLogicOptions() { return _repository.GetOptionsAsync(); } } how ever list of constructor parameters may and will change over time. This is the

How to use FSharpx TaskBuilder with functions taking parameters

假装没事ソ 提交于 2019-12-13 07:14:27
问题 I have been lately programming with the FSharpx library and especially its TaskBuilder. Now I wonder if it should be possible to define a function which takes parameters and takes a result. Such as let doTask(parameter:int) = let task = TaskBuilder(scheduler = TaskScheduler.Current) task { return! Task.Factory.StartNew(fun() -> parameter + 1) } match FSharpx.Task.run doTask(1) with | _ -> () Looking at the source code I see run expects a function taking no parameters and returning a Task<'a>

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