f#

F# type provider use case

℡╲_俬逩灬. 提交于 2019-12-05 21:16:27
I have a bit of hard time to get a grip on type providers. I would like to get some feedback on the usefulness of type provider approach for following use case. Quite unfortunately our telemetry API returns objects as List<Dictionary<string, object>> . Dictionary has strings for keys (meaning column names) and value can be any object (though, usually this is some value type in System namespace). Also, there is an method that returns Dictionary<string, Type> collection that represents the schema (column name -> type binding). We usually use this data for ad-hoc/exploratory analysis and

How to print formatted date in F#

橙三吉。 提交于 2019-12-05 21:15:16
I have this date in F# let myDate = new DateTime(2015, 06, 02) And want to output it like "2015/06/02" in the console window. I tried: Console.WriteLine(sprintf "%s" myDate.ToString("yyyy/MM/dd")) But this does not compile (compiler says, " Successive arguments should be separated by spaces or tupled, and arguments involving function or method applications should be parenthesized ") How would I output the date as "2015/06/02"? UPDATE: As commented by Panagiotis Kanavos, this will work: Console.WriteLine("{0:yyyy/MM/dd}", myDate) You easily can call the ToString overload that takes a format

Using F# JsonProvider within a portable class library fails

断了今生、忘了曾经 提交于 2019-12-05 20:11:40
I'm trying to use the JsonProvider and I'm getting the following error when I call a function on it: System.TypeInitializationException was unhandled Message: An unhandled exception of type 'System.TypeInitializationException' occurred in PortableLibrary1.dll Additional information: The type initializer for '<StartupCode$PortableLibrary1>.$PortableLibrary1' threw an exception. I have a basic console application as follows: module Pinit = open FSharp.Data type JsonT = JsonProvider<"""..\myFile.json"""> let doc = JsonT.Load("""..\nyFile.json""") let result = doc.GeneratedAt [<EntryPoint>] let

Implementating functional languages for the CLR (Or, papers on the implementation of F#)

不羁岁月 提交于 2019-12-05 20:01:19
Does anyone know of any good papers on the implementation of the F# compiler? I'm trying to generate CIL code for a simple functional language targeting the CLR, but I am struggling with a few aspects. The differences between functional languages and CIL are making it hard to generate well-typed CIL code. I have solutions that work via type erasure, but I'd much rather find a way to generate CIL code that reflects (to at least some extend) the Hindley-Milner type system of my source language (e.g., by generating generic classes). Judging by the generated code from the MS and Mono F# compilers,

how to outer join in F# using FLinq?

五迷三道 提交于 2019-12-05 19:45:45
问题 question pretty much says it all. I have a big flinq query of the following form: for alias1 in table1 do for alias2 in table2 do if alias1.Id = alias2.foreignId using this form, how can I do a left outer join between these two tables? 回答1: I think you can use the groupJoin function available in the Query module. Here is an example using Northwind with Products as the primary table and Categories as the table with foreign key: open System.Linq <@ Query.groupJoin db.Products db.Categories (fun

Is it possible to do function overloading in F#?

我怕爱的太早我们不能终老 提交于 2019-12-05 19:42:04
问题 Something like let f x = log(x) and later I can apply f to matrix, vector or a float. I guess it is not possible since F# is strictly static typed. Any other patters to overcome this problem? Thanks! 回答1: You can use operator overloading for types/classes: type Fraction = { n : int; d : int; } static member (+) (f1 : Fraction, f2 : Fraction) = { n = f1.n * f2.d + f2.n * f1.d; d = f1.d * f2.d } or inlined functions: > let inline fi a b = a+b;; val inline fi : ^a -> ^b -> ^c when ( ^a or ^b) :

Merge/join seq of seqs

南楼画角 提交于 2019-12-05 19:34:44
Slowly getting the hang of List matching and tail recursion, I needed a function which 'stitches' a list of lists together leaving off intermediate values (easier to show than explain): merge [[1;2;3];[3;4;5];[5;6;7]] //-> [1;2;3;4;5;6;7] The code for the List.merge function looks like this: ///Like concat, but removes first value of each inner list except the first one let merge lst = let rec loop acc lst = match lst with | [] -> acc | h::t -> match acc with | [] -> loop (acc @ h) t | _ -> loop (acc @ (List.tl h)) t //first time omit first value loop [] lst (OK, it's not quite like concat,

Retrieving items from an F# List passed to C#

丶灬走出姿态 提交于 2019-12-05 19:09:29
问题 I have a function in C# that is being called in F#, passing its parameters in a Microsoft.FSharp.Collections.List<object> . How am I able to get the items from the F# List in the C# function? EDIT I have found a 'functional' style way to loop through them, and can pass them to a function as below to return C# System.Collection.List: private static List<object> GetParams(Microsoft.FSharp.Collections.List<object> inparams) { List<object> parameters = new List<object>(); while (inparams != null)

how to XML-Serialize array of an array in FSharp

微笑、不失礼 提交于 2019-12-05 19:02:38
here's what I'm looking for: <reports> <parameters> <parameter name="srid" type="java.lang.Integer">16533</parameter> <parameter name="pmid" type="java.lang.Integer">17018</parameter> <parameter name="Start" type="java.text.SimpleDateFormat">1/1/2011 12:00:00 AM</parameter> <parameter name="End" type="java.text.SimpleDateFormat">1/31/2011 12:00:00 AM</parameter> </parameters> <parameters> <parameter name="srid" type="java.lang.Integer">16099</parameter> <parameter name="pmid" type="java.lang.Integer">17018</parameter> <parameter name="Start" type="java.text.SimpleDateFormat">1/1/2011 12:00:00

Why does F# Set need IComparable?

被刻印的时光 ゝ 提交于 2019-12-05 18:48:20
问题 So I am trying to use the F# Set as a hash table. But my element type doesn't implement the IComparable interface (although it implements IEquatable ). I got an error saying the construction is not allowed because of comparison constraint. And through some further read, I discovered that F# Set is implemented using binary tree, which makes insertion causes O(log(n)) . This looks weird to me, why is the Set structure designed this way? Edit: So I learned that Set in F# is actually a SortedSet