f#

Parsing the arrow type with FParsec

孤人 提交于 2019-12-11 16:58:01
问题 I'm trying to parse the arrow type with FParsec. That is, this: Int -> Int -> Int -> Float -> Char For example. I tried with this code, but it only works for one type of arrow ( Int -> Int ) and no more. I also want to avoid parentheses, because I already have a tuple type that uses them, and I don't want it to be too heavy in terms of syntax either. let ws = pspaces >>. many pspaces |>> (fun _ -> ()) let str_ws s = pstring s .>> ws type Type = ArrowType of Type * Type let arrowtype' = pipe2

How to write a sum function in F# using RProvider?

烈酒焚心 提交于 2019-12-11 16:26:20
问题 #r "RProvider.dll" open RProvider open RProvider.``base`` let add (x: float) (y: float) = let sum = R.sum(x,y) sum.Value VS gives me the error "The field, constructor or member 'Value' is not defined" I also tried passing a vector to R.sum from the little existing documentation (https://github.com/BlueMountainCapital/FSharpRProvider/wiki/How-To) I can't figure what to do 回答1: The R.sum function seems to be a bit uglier to use, because it takes variable number of parameters and sums all of

Extracting the text inside a docx file

自作多情 提交于 2019-12-11 15:35:45
问题 I am using the below code to read .docx file and it is successfully extracting the text from the file. But the problem here is, it is just extracting the text. For example if my document data is like below I am line 1 I am line 2 I am some other text Then it is returning me like I am line 1I am line 2I am some other text. I just want as it is. How can I do that. Below is the code I am using now. open System open System.IO open System.IO.Packaging open System.Xml let getDocxContent (path:

F# - Extract parameter from Expr

久未见 提交于 2019-12-11 15:24:52
问题 My questions will no end take... I've the function: let hasMany (expr:Expr<'a -> seq<'b>>) now I want to extract the seq<'b> from the Expr since I need to cast it to an ICollection<'b> and wrap it back into a new Expr - Why not just make it take an Expr that takes an ICollection<'b> in the first place you may ask - simple enough the user would need to first cast the seq<'b> to an ICollection<'b> , which I'm trying to avoid since I'm creating a library thats going to be used by others than me,

Proper way how to prepare data in async cancellable workflow with responsive UI

耗尽温柔 提交于 2019-12-11 14:40:41
问题 This question is based on Async.TryCancelled doesn't work with Async.RunSynchronously that looks complex, so I will cut a simple part that I try to solve. Suppose I have this functions: let prepareModel () = async { // this might take a lot of time (1-50seconds) let! a = ... let! b = ... let! res = combine a b return res } let updateUI model = runOnUIThread model prepareModel prepares data that should be displayed to the user. updateUI refreshes the UI (removes old controls and creates new

Parsing Firebase JSON F#

Deadly 提交于 2019-12-11 14:39:53
问题 I'm trying to convert the following Firebase JSON into something that can be parsed in F# : { "listings":{ "-L0pJmU9yj4hAocHjnrB":{ "listing_id":"-L0pJmU9yj4hAocHjnrB", "location":"Edinburgh", "messages":{ "SWs56OIGzMdiCjSXahzDQX8zve92":{ "-L3ELSSzZPRdjCRcFTrb":{ "senderId":"SWs56OIGzMdiCjSXahzDQX8zve92", "senderName":"alberto", "text":"Hi" }, "-L3EN1NW5hHWBTEGC9ve":{ "senderId":"YMM45tgFFvYB7rx9PhC2TE5eW6D2", "senderName":"David", "text":"Hey" } } } }, "-L19C5OjcDSjMi4-oha-":{ "listing_id":"

How do I use the extension method Sum on a .NET list in F#?

安稳与你 提交于 2019-12-11 14:03:46
问题 Suppose I have the following record type: open System.Collections.Generic type Store = { Products: List<Product>; } member this.TotalNumberOfItems = this.Products.Sum(fun p -> p.NumberInInventory) I want to sum a count of total items in the store as in the method above, however the System.Collections.Generic.List extension methods don't seem to be available. Intellisense does not show them. How can I call Sum from F#? 回答1: You just need to open the System.Linq namespace. For example: open

Monadic Retry logic w/ F# and async?

感情迁移 提交于 2019-12-11 13:18:49
问题 I've found this snippet: http://fssnip.net/8o But I'm working not only with retriable functions, but also with asynchronous such, and I was wondering how I make this type properly. I have a tiny piece of retryAsync monad that I'd like to use as a replacement for async computations, but that contains retry logic, and I'm wondering how I combine them? type AsyncRetryBuilder(retries) = member x.Return a = a // Enable 'return' member x.ReturnFrom a = x.Run a member x.Delay f = f // Gets wrapped

System.ComponentModel.Win32Exception when executing F# Nunit unit test code with Mono

会有一股神秘感。 提交于 2019-12-11 13:05:52
问题 I'm trying to use F# unittest on Mono. I use Mac OS X. I downloaded and copied NUNit frameworks. I have the following environments nunitFramework=.../bin/mono/NUnit.2.6.4/nunit.framework.dll console=.../bin/mono/NUnit.2.6.4/nunit-console.exe fsUnit=.../bin/mono/NUnit.2.6.4/FsUnit.NUnit.dll This is F# code: namespace HelloWorld.Core module Hello = let SayHello name = "Hello" This is the unittest for it. module HelloWorld.Tests.Hello open HelloWorld.Core.Hello open NUnit.Framework open FsUnit [

F# take items from a sequence

≯℡__Kan透↙ 提交于 2019-12-11 13:00:42
问题 Im trying to learn F# What I would like to do is download a webpage, split it into a sequence then find the index of an item and take the next 3 items after it. Heres the code -- can someone show me what Im doing wrong please? let find = "<head>" let page = downloadUrl("http://www.stackoverflow.com") let lines = seq ( page.Replace("\r", System.String.Empty).Split([|"\n"|], StringSplitOptions.RemoveEmptyEntries) ) let pos = lines |> Seq.findIndex(fun a -> a == find) // getting a Exception of