f#

Is is possible to pattern match on the underlying shape of a discriminated union?

末鹿安然 提交于 2019-12-06 11:17:13
问题 Does F# support pattern matching of a discriminated union member instance by criteria other than the Identifier pattern? For example, imagine that I want to match on the underlying shape of the data and I want to consider anything with an int * int shape, regardless of how the DU classifies the value. Is Here's how I'd do it now: type ExampleDU = | BinaryCase1 of x:int * y:int | BinaryCase2 of x:int * y:int | UnaryCase1 of x:int let underlyingValue = (1,2) let asCase1 = BinaryCase1

Extending the Indexer for an existing class

回眸只為那壹抹淺笑 提交于 2019-12-06 10:53:12
Suppose I have type A with indexer implemented, e.g. type A is a library. Now I want to extend the indexer of it, e.g. here I want to add float number into the indexer. I worked out the following code: type A(a:int array) = member this.Item with get(x) = a.[x] and set(x) value = a.[x] <- value type A with member m.Item with get(x:float) = m.[x |> int] and set(x:float) v = m.[x |> int] <- v But it seems not working: let a = A([| 1;2;3 |]) a.[1] a.[1] <- 10 a.[1.0] For the last line, I get: Script1.fsx(243,4): error FS0001: This expression was expected to have type int but here has type float Is

Creating insert statements with F#

别等时光非礼了梦想. 提交于 2019-12-06 10:44:25
问题 I use F# once every few months or so and in between it seems I forget everything, so I hope you'll excuse my ignorance. My code below is pulling data from Yahoo. It's a good example that represents what I need to do. The first row returned has the column headings. I need to take the data (the tail of the list) and insert it into the database. What's the best way to generate an insert statement based on the column headings returned (the column headings match the database column names)? In the

F# functional style approach much slower

折月煮酒 提交于 2019-12-06 10:25:15
Trying to learn F#, by solving some programming puzzles. I don't want to add too many details about the problem as I don't want to spoil the fun for others. Basically, the issue is to find all 4-uples { (i,j,k,l) | i ^ j ^ k ^ l != 0 } with no repetition (eg., (1,1,1,2) and (1,1,2,1) are the same and should be counted just once). I have found a O(n^3) approach which works, please see countImperative(a,b,c,d) below. But I also tried to refactor the code as to get rid of the nested for loops. However, I could not do so without a significant performance penalty. It was my impression that F#'s

Unexpected result from call to Newtonsoft.Json from F#

浪子不回头ぞ 提交于 2019-12-06 10:15:01
I am not getting the expected result from this F# code. I would expect t to contain values as a result of the call to JsonSchema.Parse(json) but instead it is empty. What am I doing wrong? open Newtonsoft.Json open Newtonsoft.Json.Schema let json = """{ "Name": "Bad Boys", "ReleaseDate": "1995-4-7T00:00:00", "Genres": [ "Action", "Comedy" ] }""" [<EntryPoint>] let main argv = let t = JsonSchema.Parse(json) 0 // return an integer exit code Mark Seemann As John Palmer points out, JsonSchema.Parse parses a JSON schema , but from your question, it looks as though you want to parse a normal JSON

What am I doing wrong in chunking a list in F#

橙三吉。 提交于 2019-12-06 09:32:35
F# doesn't come easy to me. The following piece of code is supposed to chunk a list. I have no idea what the problem is. Please help. let chunk items chunkSize = let folder = fun state x -> match state with (reversedResult, reversedChunk) -> if reversedChunk.Length < chunkSize then (reversedResult, x::reversedChunk) else ((reversedChunk |> List.rev)::reversedResult, [x]) let alsmostDone = items |> List.fold folder ([], []) match alsmostDone with | (reversedResult, []) -> reversedResult |> List.rev | (reversedResult, lastReversedChunk) -> (lastReversedChunk |> List.rev)::reversedResult |> List

Using F# Datatypes in C#

北战南征 提交于 2019-12-06 08:57:29
More particularly, I really want an immutable/shared linked list, and I think having immutable maps and sets would be nice too. As long as I don't have to worry about the core implementation, I can easily add extension methods/subclass/wrap it to provide a reasonably slick external interface for myself to use. Is there any reason I shouldn't do this? Performance, incompatibility, etc.? The types in the F# library (such as Set , Map and list ) were not designed to be used from C#, so I wouldn't generally recommend using them directly. It can be done and some basic operations will work well (e.g

F# Code Optimization for Left Leaning Red Black Tree

前提是你 提交于 2019-12-06 07:49:42
问题 I've been working on porting a C# implementation of a LLRBT to F# and I now have it running correctly. My question is how would I go about optimizing this? Some ideas I have Using a Discriminated Union for Node to remove the use of null Remove getters and setters you cant have a null attribute and a struct at the same time Full source can be found here. C# code taken from Delay's Blog. Current performance F# Elapsed = 00:00:01.1379927 Height: 26, Count: 487837 C# Elapsed = 00:00:00.7975849

F# operator overloading riddle 2

佐手、 提交于 2019-12-06 07:05:42
问题 In F# operator overloading seems powerful but also tricky to get right. I have following class: type Value<'T> = with static member inline (+) (a : Value<'U>, b: Value<'U>) : Value<'U> = do stuff If i define another overload for + with : static member inline (+) (a : Value<'U>, b: 'U) : Value<'U> = do stuff It works. But if i want a symmetric operator: static member inline (+) (b: 'U, a : Value<'U>) : Value<'U> = do stuff The compiler complains: let a = Value<int>(2); let b = a + 3 // ok let

Replay Recorded Data Stream in F#

做~自己de王妃 提交于 2019-12-06 06:48:20
问题 I have recorded real-time stock quotes in an SQL database with fields Id , Last , and TimeStamp . Last being the current stock price (as a double), and TimeStamp is the DateTime when the price change was recorded. I would like to replay this stream in the same way it came in, meaning if a price change was originally 12 seconds apart then the price change event firing (or something similar) should be 12 seconds apart. In C# I might create a collection, sort it by the DateTime then fire an