f#

Inconsistent processing of generic ParamArray argument in F#

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 06:41:46
问题 Being spotted in context of this question this seemingly inconsistent behavior can be reproduced as following both in F#2.0 and F#3.0 RC: type Heterogeneous = static member Echo([<ParamArray>] args: Object[]) = args type Generic = static member Echo<'T>([<ParamArray>] args: 'T[]) = args Usage: Returns: Heterogeneous.Echo 0 // [|0|] OK Generic.Echo 0 // [|0|] OK Heterogeneous.Echo (0,1) // [|0; 1|] OK Generic.Echo (0,1) // [|0; 1|] OK Heterogeneous.Echo [|0|] // [|[|0|]|] OK? Generic.Echo [|0|

Is it possible to make the following example fully polymorphic in F#?

家住魔仙堡 提交于 2020-01-02 06:37:10
问题 type Mul = Mul with member inline __.Op(a: ^a,b: ^a) = a*b type Div = Div with member inline __.Op(a: ^a,b: ^a) = a/b type Add = Add with member inline __.Op(a: ^a,b: ^a) = a+b type Sub = Sub with member inline __.Op(a: ^a,b: ^a) = a-b let inline op x a b = (^a: (member Op: ^b * ^b -> ^b) x,a,b) let inline tup2 a b c d = op Mul a b, op Mul c d let inline tup2' f a b c d = op f a b, op f c d let a = tup2 1 2 3.0f 4.0f //let b = tup2' Mul 1 2 3.0f 4.0f //Gives a type error. I am wondering if

Extension methods for F# tuples

自古美人都是妖i 提交于 2020-01-02 05:41:10
问题 Is it possible to write extension methods for F# tuples? For example, to add instance methods .Item1 and .Item2 (like System.Tuple) which are equivalent to calling fst and snd for 2-tuples? 回答1: Not perfect but I'm using this. (I borrowed original code from http://www.fssnip.net/6V and added small modification.) [<AutoOpen>] module TupleExtensions = type System.Tuple with static member Item1(t) = let (x,_) = t in x static member Item1(t) = let (x,_,_) = t in x static member Item1(t) = let (x,

AWS API Gateway: Regex for error is not picked up

情到浓时终转凉″ 提交于 2020-01-02 05:41:08
问题 I have a Lambda function tied to the AWS API gateway. When I call it with something like /foo/bar and bar does not exist, my function returns (correctly) something like: { "code": 404, "message": "bar not found" } I now want the status code of this to be 404, too, but following the tutorials I could find on the net, I had no success so far. What I did: I created a method response for 404 that looks like this: And in the integration response, I set the following: To my understanding, this

How do I test for exactly 2 characters with fparsec?

北城余情 提交于 2020-01-02 05:38:09
问题 I have the following program that runs. It takes a line of text and splits it into two parts, the first is an identifier and the second is the remainder of the line. My parser for the identifier (factID) takes any string of characters as the identifier, which is not (quite) what I want. What I want is a parser that only succeeds when it encounters two consecutive upper case letters. So for example "AA" should succeed while "A", "A1" or "AAA" should not. What I can't figure out is how

F# Discriminated Union - “downcasting” to subtype

◇◆丶佛笑我妖孽 提交于 2020-01-02 05:27:28
问题 I don't really know what the proper title for this question should be, but: I have a discriminated union called MyDiscriminatedUnion in F# : type MyDiscriminatedUnion = | Foo of Foo | Bar of Bar where Foo and Bar are record types: type Foo = { ... } type Bar = { ... } I created a value of union type Foo : let foo = Foo { ... } The compiler tells me that foo is of type MyDiscriminatedUnion . Then I want to pass foo into a function that expects type Foo, not MyDiscriminatedUnion . Therefore the

Redistributable version of FSharp.Core.dll?

最后都变了- 提交于 2020-01-02 04:42:06
问题 I have developed a commercial extension for the Unity3D game engine in F#, it's a piece of code which extends the editor with advanced node/graph editing features. F# is perfect for this due to the nature of immutability and DU's. But, to my question: I realized that the license for the FSharp.Core.dll which comes with Microsoft .NET/Visual Studio probably doesn't allow you to re-distribute it with a commercial project? How would I go about finding out if this is the case or not, and if it's

Converting C# code to F# (if statement)

…衆ロ難τιáo~ 提交于 2020-01-02 03:50:27
问题 I'd like to know how to convert this code line by line from C# to F#. I am not looking to use any kind of F#'s idioms or something of the like. I am trying to understand how to map directly C#'s constructs to F#. Here is the C# code: //requires l.Length > 0 int GetMinimumValue(List<int> l) { int minVal = l[0]; for (int i = 0; i < l.Length; ++i) { if (l[i] > minValue) { minVal = l[i]; } } return minVal; } And here is my F# attempt: let getMinValue (l : int list) = let minVal = l.Head for i = 0

building Either (or Result) on top of Choice in F#

若如初见. 提交于 2020-01-02 02:46:09
问题 I built a monad for success/failure based on information in Scott Wlaschin's blog with extra help from this stack overflow posting. I ended up with a type type Result<'a> = | Success of 'a | Error of string I now realize that this is equivalent of Choice in F#, and Either in haskell. I'd like to reuse code instead of keeping my own, but would like to just change the implementation and not have to change my existing code. I'd like to use my existing names along with the implementation of

F# comparison vs C# IComparable

微笑、不失礼 提交于 2020-01-02 02:35:08
问题 My problem, in a nutshell, is this: What can I do about storing a tuple (or any type with a constraint of 'comparison') in a C# container that requires an IComparable ? This works: > let x (y : 'a when 'a : comparison) = y ;; val x : y:'a -> 'a when 'a : comparison > x (1,2) ;; val it : int * int = (1, 2) I would have thought this would work: > let x (y : IComparable<int>) = y ;; val x : y:IComparable<int> -> IComparable<int> > x (1,2) ;; x (1,2) ;; ---^^^ stdin(28,4): error FS0001: The type