f#

F# function calling syntax confusion

百般思念 提交于 2019-12-17 16:16:26
问题 I have a piece of code: links |> Seq.map (fun x -> x.GetAttributeValue ("href", "no url")) Which I wanted to rewrite to: links |> Seq.map (fun x -> (x.GetAttributeValue "href" "no url")) But the F# compiler doesn't seem to like that. I was under the impression that these two function calls were interchangeable: f (a, b) (f a b) The error that I get is: The member or object constructor 'GetAttributeValue' taking 2 arguments are not accessible from this code location. All accessible versions of

How do I create an F# Type Provider that can be used from C#?

≯℡__Kan透↙ 提交于 2019-12-17 15:38:56
问题 If I use the F# Type Providers from the assembly FSharp.Data.TypeProviders 4.3.0.0, I am able to create types in a very simple F# library. I am then able to use those types without any dependency on the assembly FSharp.Data.TypeProviders. That is pretty sweet! Here is an example: I created an F# library project called TryTypeProviders. I put this in the .fs: module TryTypeProviders type Northwind = Microsoft.FSharp.Data.TypeProviders.ODataService I then am able to use the F# library from a C#

Pretty print a tree

空扰寡人 提交于 2019-12-17 15:25:46
问题 Let's say I have a binary tree data structure defined as follows type 'a tree = | Node of 'a tree * 'a * 'a tree | Nil I have an instance of a tree as follows: let x = Node (Node (Node (Nil,35,Node (Nil,40,Nil)),48,Node (Nil,52,Node (Nil,53,Nil))), 80,Node (Node (Nil,82,Node (Nil,83,Nil)),92,Node (Nil,98,Nil))) I'm trying to pretty-print the tree into something easy to interpret. Preferably, I'd like to print the tree in a console window like this: _______ 80 _______ / \ _ 48 _ _ 92 _ / \ / \

Overloaded inline operators in F#: ( |+| )

霸气de小男生 提交于 2019-12-17 14:53:52
问题 I'm trying to define an overloaded operator, e.g. |+| , as the following: let inline ( |+| ) (m1 : #IMeasurable) (m2 : #IMeasurable) = m1.Measure + m2.Measure The problem is, I can't do something like: let three = m1 |+| m2 |+| m3 Because the operator |+| isn't defined for the case (m1 : int) (m2 : #IMeasurable) . Is there a way to overload this operator or use static type constraints to make the above expression possible? Is there a way to modify IMeasurable (which I can edit) so that this

Concisely creating an IDictionary<_,obj>

走远了吗. 提交于 2019-12-17 14:05:38
问题 Is there a shorter way of creating an IDictionary<_,obj> , possibly without boxing every value? This is what I have. let values = [ "a", box 1 "b", box "foo" "c", box true ] |> dict Dictionary<_,obj>.Add can be called without boxing, but I couldn't figure out a way to use it that's shorter than what I have. I'm hoping for something other than defining a boxing operator. EDIT Based on Brian's suggestion, here's one way to do it, but it has its own problems. let values = Seq.zip ["a"; "b"; "c"]

Concisely creating an IDictionary<_,obj>

*爱你&永不变心* 提交于 2019-12-17 14:05:34
问题 Is there a shorter way of creating an IDictionary<_,obj> , possibly without boxing every value? This is what I have. let values = [ "a", box 1 "b", box "foo" "c", box true ] |> dict Dictionary<_,obj>.Add can be called without boxing, but I couldn't figure out a way to use it that's shorter than what I have. I'm hoping for something other than defining a boxing operator. EDIT Based on Brian's suggestion, here's one way to do it, but it has its own problems. let values = Seq.zip ["a"; "b"; "c"]

How can i convert between F# List and F# Tuple?

不羁的心 提交于 2019-12-17 12:16:16
问题 Is there some way to convert between F# List and F# Tuple? For example: [1;2;3] -> (1,2,3) (1,2,3,4) -> [1;2;3;4] I need two Functions to do that: let listToTuple list = ... let tupleToList tuple = ... Thank you in advance. 回答1: Besides listToTuple then pblasucci has the right answer. But you wont be happy with the result unless you know something about type types involved, or if you wan't to do a lot of boxing and unboxing. let tupleToList t = if Microsoft.FSharp.Reflection.FSharpType

How can i convert between F# List and F# Tuple?

故事扮演 提交于 2019-12-17 12:16:09
问题 Is there some way to convert between F# List and F# Tuple? For example: [1;2;3] -> (1,2,3) (1,2,3,4) -> [1;2;3;4] I need two Functions to do that: let listToTuple list = ... let tupleToList tuple = ... Thank you in advance. 回答1: Besides listToTuple then pblasucci has the right answer. But you wont be happy with the result unless you know something about type types involved, or if you wan't to do a lot of boxing and unboxing. let tupleToList t = if Microsoft.FSharp.Reflection.FSharpType

Projecting a list of lists efficiently in F#

跟風遠走 提交于 2019-12-17 11:53:10
问题 I have to do projection of a list of lists which returns all combinations with each element from each list. For example: projection([[1]; [2; 3]]) = [[1; 2]; [1; 3]]. projection([[1]; [2; 3]; [4; 5]]) = [[1; 2; 4]; [1; 2; 5]; [1; 3; 4]; [1; 3; 5]]. I come up with a function: let projection lss0 = let rec projectionUtil lss accs = match lss with | [] -> accs | ls::lss' -> projectionUtil lss' (List.fold (fun accs' l -> accs' @ List.map (fun acc -> acc @ [l]) accs) [] ls) match lss0 with | [] ->

How do I customize output of a custom type using printf?

我们两清 提交于 2019-12-17 11:23:12
问题 I've read through a good chunk of Expert F# and am working on building an actual application. While debugging, I've grown accustomed to passing fsi commands like this to make things legible in the repl window: fsi.AddPrinter(fun (x : myType) -> myType.ToString()) I would like to extend this to work with the printf formatter, so I could type e.g. printf "%A" instanceOfMyType and control the output for a custom type. The book implies that this can be done (p 93, "Generic structural formatting