f#

Windows Store Apps and F#

◇◆丶佛笑我妖孽 提交于 2019-12-21 14:11:10
问题 I am trying to create a portable library using F# to use with Windows Store apps. I created one fs file with one class: module FunctionalRT open System.Net open System.IO type WebHelper = static member DownloadStringAsync (url:string) = async { let req = HttpWebRequest.Create(url) use! resp = req.AsyncGetResponse() use stream = resp.GetResponseStream() let reader = new StreamReader(stream) let s = reader.ReadToEnd() return s } I referenced this library in my Windows Store app with no problems

Generated Types Provider working example [closed]

孤者浪人 提交于 2019-12-21 14:03:43
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Can somebody point me to working example of generated types F# TypeProvider? Ideally, based on F# sample pack "ProvidedTypes-*.fs"

Comparing F# discriminated union instances via pattern matching

痞子三分冷 提交于 2019-12-21 13:59:12
问题 Firstly, apologies for the poor title - I don't understand enough F# to describe the problem better. Consider this simple DU: type Money = | USD of decimal | GBP of decimal | EUR of decimal static member (+) (first: Money, second: Money) = match first, second with | USD(x), USD(y) -> USD(x + y) | GBP(x), GBP(y) -> GBP(x + y) | EUR(x), EUR(y) -> EUR(x + y) | _ -> failwith "Different currencies" I'm representing money in different currencies, and overloading the (+) operator so that I can

Can I use the pipeline operator in F# to pass an argument to a constructor?

a 夏天 提交于 2019-12-21 12:17:49
问题 This code reverses a string: let reverse (s : string) = new string(s.ToCharArray() |> Array.rev) Can this be rewritten using the pipeline operator to pass the required argument to the string() constructor? For example, this seems more idiomatic: // Doesn't compile: let reverse (s : string) = s.ToCharArray() |> Array.rev |> new string Similarly, why can't I use the string operator in the following way? let reverse2 (s : string) = s.ToCharArray() |> Array.rev |> string Here it is in action: >

State monad and strategy pattern

落爺英雄遲暮 提交于 2019-12-21 11:22:14
问题 I am redesigning a library and I am not happy with the current design pattern. This question concerns the use of the strategy pattern in conjunction with a State monad I have a Filter . All it does, in its basic implementation, is to take a some datafeed of type 'd and update itself, generating a new updated copy of itself. [<AbstractClass>] type Filter<'d, 'F> (state: 'F) = member val StateVariable = state with get abstract member Update: 'd -> Filter<'d, 'F> I have then a ISignalGenerator ,

Strange behavior of F# records

人走茶凉 提交于 2019-12-21 09:27:43
问题 There is a few cases when F# records behavior is strange to me: No warning on ambiguity type AnotherPerson = {Id: int; Name: string} type Person = {Id: int; Name: string;} // F# compiler will use second type without any complains or warnings let p = {Id = 42; Name = "Foo";} Warning on records deconstruction instead of records construction Instead of getting a warning on records construction in previous case, F# compiler issued a warning on records "deconstruction": // Using Person and

Signature Files and Access Modifers in F#

╄→гoц情女王★ 提交于 2019-12-21 09:23:14
问题 I've recently been trying to learn the Object-Oriented aspects of F#, and have become curious about how to restrict access to types/modules in the language. More specifically, I want to know the difference between writing this: Example.fsi module Stack = val foo : string Example.fs module Stack = let foo = "foo" let bar = "bar" and alternatively this: module Stack = let foo = "foo" let private bar = "bar" Do they not accomplish exactly the same thing in the end? Coming from a C# background, I

Implementing custom comparison with CustomComparison and CustomEquality in F# tuple

扶醉桌前 提交于 2019-12-21 09:14:58
问题 I'm here to ask a specific topic - I really found few info about this on the web. I'm implementing a F# version of Minimax algorithm. The problem I'm having now is that I want to compare Leaf of my tree (data structure below). Searching the erros the VS gave to me I arrived to something like this: The tree type I used to have: type TreeOfPosition = | LeafP of Position | BranchP of Position * TreeOfPosition list and the temptative for implementing the IComparable type staticValue = int [

F# using sequence cache correctly

北城以北 提交于 2019-12-21 09:14:08
问题 I'm trying to use Seq.cache with a function that I made that returns a sequence of primes up to a number N excluding the number 1. I'm having trouble figuring out how to keep the cached sequence in scope but still use it in my definition. let rec primesNot1 n = {2 .. n} |> Seq.filter (fun i -> (primesNot1 (i / 2) |> Seq.for_all (fun o -> i % o <> 0))) |> Seq.append {2 .. 2} |> Seq.cache Any ideas of how I could use Seq.cache to make this faster? Currently it keeps dropping from scope and is

When to favor untyped over typed quotations in F#?

送分小仙女□ 提交于 2019-12-21 09:02:12
问题 F# has both typed and untyped code quotations and I wonder what are the use cases where one would choose one over the other? Is the distinction just convenience and untyped and typed quotations are convertible to each in all cases or are typed quotations e. g. a subset of the ones possible with untyped quotations? Are there any examples which only work with typed, but not with untyped quotations – or the other way around? 回答1: In general, I'd recommend using typed quotations whenever you can.