f#

Difference between higher order and curried functions

你说的曾经没有我的故事 提交于 2019-12-12 10:54:11
问题 I'm reading a book, Functional Programming Using F#, which says (page 33), in the section Declaration of higher-order functions We have seen higher-order built-in functions like (+) and (<<) and at the end of the section Higher-order functions may alternatively be defined by supplying the arguments as follows in the let-declaration: let weight ro s = ro * s ** 3.0;; However there were some helpful comments at the bottom of a question that I asked earlier today (which was originally titled

F# pipe first parameter

一世执手 提交于 2019-12-12 10:47:33
问题 is that possible to pipe the first parameter into a multiple parameter function? for example date = "20160301" Is that possible to pipe date into DateTime.ParseExact( , "yyyyMMDD", CultureInfo.InvariantCulture) 回答1: As @yuyoyuppe explains in his/her answer, you can't pipe directly into ParseExact because it's a method, and thereby not curried. It's often a good option to define a curried Adapter function, but you can also pipe into an anonymous function if you only need it locally: let res =

Download one file from remote (git show) using libgit2sharp

本小妞迷上赌 提交于 2019-12-12 10:44:11
问题 Using git show , I can fetch the contents of a particular file from a particular commit, without changing the state of my local clone : $ git show <file> $ git show <commit>:<file> How can I achieve this programatically using libgit2sharp? 回答1: According to the documentation: $ git show 807736c691865a8f03c6f433d90db16d2ac7a005:a.txt Is equivalent to the code below: using System; using System.IO; using System.Linq; using System.Text; using LibGit2Sharp; namespace ConsoleApp2 { class Program {

Classes without constructor in F#

江枫思渺然 提交于 2019-12-12 10:43:33
问题 I'm not sure why F# seems to allow the definition of a class without any constructors. I mean, it would be impossible to instantiate an object of the class. Shouldn't the language spec treat this as illegal behavior? For example, I can define the class type myClass = class member this.x = 0 end myClass seems to have the type type myClass = member x: int But it would not be instantiable. 回答1: In my experience, the object-oriented features of F# can sometimes be less elegant than what C#

Generic type definition syntax on F#

非 Y 不嫁゛ 提交于 2019-12-12 10:37:26
问题 This is not a big deal, but is there any way in F# to get a generic type definition without calling GetGenericTypeDefinition() ? IComparable<_> is IComparable<object> (or whatever type is inferred) and IComparable<> is a syntax error. VB.NET GetType(IComparable(Of )) C# typeof(IComparable<>) F# typeof<IComparable<_>>.GetGenericTypeDefinition() 回答1: You want "typedefof" printfn "%s" (typedefof<list<int>>).Name 来源: https://stackoverflow.com/questions/1652050/generic-type-definition-syntax-on-f

F#: Destructuring bind with a discriminated union

你离开我真会死。 提交于 2019-12-12 10:36:51
问题 open System let x = (1, 2) let (p, q) = x printfn "A %A" x printfn "B %A %A" p q let y = Some(1, 2) try let None = y () with | ex -> printfn "C %A" ex let Some(r, s) = y printfn "D %A" y // printfn "E %A %A" r s http://ideone.com/cS9bK0 When I uncomment the last line, the compiler rejects the code complaining /home/rRiy1O/prog.fs(16,19): error FS0039: The value or constructor 'r' is not defined /home/rRiy1O/prog.fs(16,21): error FS0039: The value or constructor 's' is not defined Is it not

this construct causes code to be less generic… when applied to phantom type

十年热恋 提交于 2019-12-12 10:35:37
问题 I am trying to implement a DSL in F# for a small language. Unfortunately the compiler stops me dead in my tracks when I attempt to constrain nodes to hold additional type information (via phantom types). The following lines of code illustrate the issue: type Expr<'a> = | Int of int | Eq of Expr<int> * Expr<int> | Not of Expr<bool> let rec to_string (expr: Expr<'a>) = match expr with | Int(n) -> string n | Eq(x, y) -> sprintf "%s == %s" (to_string x) (to_string y) | Not(b) -> sprintf "!%s" (to

Is this usage of Option idiomatic in F#?

安稳与你 提交于 2019-12-12 10:34:02
问题 I have the following function that checks the existance of a customer in a data source and returns the id. Is this the right/idiomatic way of using the Option type? let findCustomerId fname lname email = let (==) (a:string) (b:string) = a.ToLower() = b.ToLower() let validFName name (cus:customer) = name == cus.firstname let validLName name (cus:customer) = name == cus.lastname let validEmail email (cus:customer) = email == cus.email let allCustomers = Data.Customers() let tryFind pred =

Undocumented `when` keyword usage in FSharp.Core

心已入冬 提交于 2019-12-12 10:28:50
问题 Looking for information about statically resolved type parameters for inline functions I stumbled upon the definitions of various primitive operators in FSharp.Core: let inline (+) (x: ^T) (y: ^U) : ^V = CheckedAdditionDynamic<(^T),(^U),(^V)> x y when ^T : int32 and ^U : int32 = (# "add.ovf" x y : int32 #) when ^T : float and ^U : float = (# "add" x y : float #) // <snip> when ^T : ^T = ((^T or ^U): (static member (+) : ^T * ^U -> ^V) (x,y)) As can be seen in the snippet above the when

How to create new type at runtime in F#? [closed]

孤人 提交于 2019-12-12 10:28:02
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Please give an example of how to create new type (say, two types Cartesian product) in F# at runtime with reflection? UPDATE I am looking for a language with first class types. I was told F# can this. I tried