f#

How to consume HttpClient from F#?

匆匆过客 提交于 2020-01-02 01:05:10
问题 I'm new to F# and stuck in understanding async in F# from the perspective of a C# developer. Say having the following snippet in C#: var httpClient = new HttpClient(); var response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); How to write the same in F#? 回答1: Here is a function that should do what you're looking for (note that you'll have to wrap the code in an asynchronous computation expression in order to

When to use interfaces, and when to use higher order functions?

我是研究僧i 提交于 2020-01-02 00:59:09
问题 Given a ASP.NET MVC application with the following layers: UI (Views, CSS, Javascript, etc.) Controllers Services (Contains business logic, and data access) The reason for no separate data access layer, is that I'm using SQL type provider. (The following code may not be working, as it's only a raw draft). Now imagine a service named UserService defined like: module UserService = let getAll memoize f = memoize(fun _ -> f) let tryGetByID id f memoize = memoize(fun _ -> f id) let add evict f

F# how to write an empty statement

∥☆過路亽.° 提交于 2020-01-02 00:54:31
问题 How can I write a no-op statement in F#? Specifically, how can I improve the second clause of the following match statement: match list with | [] -> printfn "Empty!" | _ -> ignore 0 回答1: Use unit for empty side effect: match list with | [] -> printfn "Empty!" | _ -> () 回答2: The answer from Stringer is, of course, correct. I thought it may be useful to clarify how this works, because "()" insn't really an empty statement or empty side effect... In F#, every valid piece of code is an expression

how does [<Literal>] differ from other constants in F#

隐身守侯 提交于 2020-01-02 00:47:10
问题 I am a bit confused by the Literal keyword and why it is necessary in F#. Reading the docs, it sounds to me that [<Literal>] is used to define a constant, however I am a bit confused how this constant differs from all other constants in F#.. Values that are intended to be constants can be marked with the Literal attribute. This attribute has the effect of causing a value to be compiled as a constant. When I think of a constant, I think of something which is immutable.... let x = "a" + "b" /

F# naming convention

人盡茶涼 提交于 2020-01-02 00:19:10
问题 Is there an "official" naming / casing convention for F#? I'm always in doubt of using C# style or not: Class.MyFunctionName or Module.my_function_name In F# you're meant to mix BCL classes and F# library ones: they have different casing and the code looks very ugly. 回答1: Yes, there is confusion, because F# has morphed from OCaml to .Net over the years. Basically, the naming conventions are a "breaking change" - old code is inconsistent with new code. However, the May 2009 CTP has settled the

How to implement an interface member that returns void in F#

我怕爱的太早我们不能终老 提交于 2020-01-01 23:59:08
问题 Imagine the following interface in C#: interface IFoo { void Bar(); } How can I implement this in F#? All the examples I've found during 30 minutes of searching online show only examples that have return types which I suppose is more common in a functional style, but something I can't avoid in this instance. Here's what I have so far: type Bar() = interface IFoo with member this.Bar() = void Fails with _FS0010: Unexpected keyword 'void' in expression_. 回答1: The equivalent is unit which is

F# and Excel integration for .NET 4.0 (Visual Studio 2010 Beta 1)

倾然丶 夕夏残阳落幕 提交于 2020-01-01 16:50:11
问题 Can anyone give a web link to or demonstrate here how to do F# and Excel integration using .NET 4.0 (Visual Studio 2010 Beta 1)? I know how to do this in the CTP release but as far as I know it should be simpler in .NET 4.0 (Visual Studio 2010 Beta 1). 回答1: There is no 'secret sauce' added to the latest F# CTP release (Visual Studio 2010 Beta1) to improve Office interop. Perhaps you have F# confused with C#'s new support for Dynamic. However, Office interop in F# is the same as C# - you can

“Merging” Discriminated Unions in F#?

半城伤御伤魂 提交于 2020-01-01 16:23:32
问题 Following on from this question, I am having an issue combining differently typed Result types together. (what follows is a contrived example, not real code) Suppose I have a function that reads a file: type ReadFileError = | FileNotFound of string let readFile (path : string) : Result<string, ReadFileError> = // --- 8< --- And a function that parses it somehow: type JsonParseError = | InvalidStructure of string let parseJson (content : string) : Result<Json, JsonParseError> = // --- 8< ---

How would you approach this problem in F#? (high frequency sensor data)

非 Y 不嫁゛ 提交于 2020-01-01 12:02:34
问题 I'm a mechanical engineering grad student and my adviser has just asked me to write a data visualization utility for one of our sensor projects. As it's summer and he wants me to have some fun with it, I thought this would be a great time to learn a language adept at scientific computing, so I went ahead and plowed right into F#. As I'm new to the functional programming paradigm I'm having a little difficulty structuring my program properly, especially given the possibility of easily

Is there somewhere to learn about the possible future of F#? [closed]

喜欢而已 提交于 2020-01-01 09:17:12
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Are there any websites/blogs (perhaps by F# team members?) where thoughts about the future of F# are regularly discussed/revealed? I