f#

Execute Code on Main Thread from Async F#

廉价感情. 提交于 2019-12-22 12:18:41
问题 I am implementing the following Swift method in F#: func downloadCachedImage(url : URL) { if let cachedImage = imageCache.object(forKey: url.absoluteString as AnyObject) { self.image = cachedImage as! UIImage } URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in if (error != nil) { print(error) return } DispatchQueue.main.async { if let downloadedim = UIImage(data: data!) { imageCache.setObject(downloadedim, forKey: url.absoluteString as AnyObject) self.image

How to print formatted date in F#

亡梦爱人 提交于 2019-12-22 10:46:52
问题 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:

Tail-recursion in FParsec

谁说胖子不能爱 提交于 2019-12-22 10:36:24
问题 I have encounter a problem with parsers having two branches of recursion. To demonstrate the problem easier, I use a simple grammar of a lambda calculus from the article written by Luca Bolognese as the example: <expression> ::= <name> | <function> | <application> <name> ::= non­blank character sequence <function> ::= \ <name> . <body> <body> ::= <expression> <application> ::= ( <function expression> <argument expression> ) <function expression> ::= <expression> <argument expression> ::=

What would be an idiomatic F# way to scale a list of (n-tuples or list) with another list, also arrays?

跟風遠走 提交于 2019-12-22 09:50:02
问题 Given: let weights = [0.5;0.4;0.3] let X = [[2;3;4];[7;3;2];[5;3;6]] what I want is: wX = [(0.5)*[2;3;4];(0.4)*[7;3;2];(0.3)*[5;3;6]] would like to know an elegant way to do this with lists as well as with arrays. Additional optimization information is welcome 回答1: You write about a list of lists, but your code shows a list of tuples. Taking the liberty to adjust for that, a solution would be let weights = [0.5;0.4;0.3] let X = [[2;3;4];[7;3;2];[5;3;6]] X |> List.map2 (fun w x -> x |> List

How to check if a function's type parameters are statically resolved?

白昼怎懂夜的黑 提交于 2019-12-22 09:48:19
问题 very simple examples: let myfun x = x Here in the intellisense it says "x: 'a -> 'a". In the FSI it says "x: 'a -> 'a" let inline myfun x = x Here in the intellisense it says "x: 'a -> 'a". In the FSI it says "x: 'a -> 'a" <<<< why not ^a ? let inline myfun (x: 'b) = x Here in the intellisense it says "x: 'b -> 'b". In the FSI it says "x: 'b -> 'b" let inline myfun (x: ^b) = x Here in the intellisense it says "x: 'b -> 'b". In the FSI it says "x: ^b -> ^b" <<<< different Since the

F#: shortest way to convert a string option to a string

穿精又带淫゛_ 提交于 2019-12-22 09:31:45
问题 The objective is to convert a string option that comes out of some nicely typed computation to a plain string that can then be passed to the UI/ printf /URL/other things that just want a string and know nothing of option types. None should just become the empty string. The obvious way is to do a match or an if on the input: input |> fun s -> fun s -> match s with | Some v -> v | _ -> "" or input |> fun s -> if s.IsSome then s.Value else "" but while still being one-liners, these still take up

How to properly overload global (+) and (*) without breaking support for other types

匆匆过客 提交于 2019-12-22 09:29:31
问题 I have imported a vector math library, and would like to add my own (*) and (+) operators while preserving the existing operators for basic int and float. I have tried the following: let inline (*) (x : float) (y : Vector) = y.Multiply(x) let inline (*) (x : Vector) (y : float) = x.Multiply(y) let inline (+) (x : Vector) (y : Vector) = x.Add(y) Which has two problems: It seems to remove int + int and int * int , and The 2nd line (which is intended to complete commutativity) does not compile

F# - how to group previous, this and next elements in circular Seq

你离开我真会死。 提交于 2019-12-22 09:11:55
问题 In F# How to best convert a finite Sequence-like seq [0; 1; 2; 3; 4] into a Sequence of tuples like seq [4,0,1 ; 0,1,2 ; 1,2,3 ; 2,3,4 ; 3,4,0] ? Addition: My Seq represents circular data. In this case the vertices of a closed polyline. I need the neighboring elements to compute the angle of each corner. 回答1: I would do it like this: let neighbors xs = match Array.ofSeq xs with | [||] -> [||] | [|x|] -> [|x, x, x|] | xs -> let n = xs.Length [|yield xs.[n-1], xs.[0], xs.[1] for i=1 to n-2 do

How to make a function to return really different types in fsharp?

邮差的信 提交于 2019-12-22 08:48:23
问题 Assume that there is a third-party library written in FSharp, it contains several generic classes, for example as follows: type FirstType<'a> has method DoWork , that accepts: first param of type FirstType<'a> , second param is a function of type ('a -> 'b) DoWork method return type is FirstType<'b> type SecondType<'a> has method DoWork , that accepts: first param of type SecondType<'a> , second param is a function of type ('a -> 'b) DoWork method return type is SecondType<'b> type ThirdType

Right associative operator in F# [duplicate]

心不动则不痛 提交于 2019-12-22 08:27:27
问题 This question already has an answer here : Function Application Operator ($) in F#? (1 answer) Closed 5 years ago . Sometimes I have to write: myList |> List.iter (fun x -> x) I would really like to avoid the parentheses. In Haskell there is an operator for this ($) It would look like this myList |> List.iter $ fun x -> x I created a custom operator let inline (^!) f a = f a and now I can write it like this myList |> List.iter ^! fun x -> x Is there something like this in F#? 回答1: There is no