f#

Compile-time constraints for strings in F#, similar to Units of Measure - is it possible?

試著忘記壹切 提交于 2019-12-17 19:48:23
问题 I'm developing a Web application using F#. Thinking of protecting user input strings from SQL, XSS, and other vulnerabilities. In two words, I need some compile-time constraints that would allow me discriminate plain strings from those representing SQL, URL, XSS, XHTML, etc. Many languages have it, e.g. Ruby’s native string-interpolation feature #{...} . With F#, it seems that Units of Measure do very well, but they are only available for numeric types. There are several solutions employing

What is the simplest way to access data of an F# discriminated union type in C#?

心不动则不痛 提交于 2019-12-17 19:36:13
问题 I'm trying to understand how well C# and F# can play together. I've taken some code from the F# for Fun & Profit blog which performs basic validation returning a discriminated union type: type Result<'TSuccess,'TFailure> = | Success of 'TSuccess | Failure of 'TFailure type Request = {name:string; email:string} let TestValidate input = if input.name = "" then Failure "Name must not be blank" else Success input When trying to consume this in C#; the only way I can find to access the values

Why can't F#'s type inference handle this?

送分小仙女□ 提交于 2019-12-17 19:35:37
问题 I have a sequence of FileInfo, but I only care about their string names, so I want a sequence of string. At first I tried something like this: Seq.map (fun fi -> fi.Name) fis But for some reason, F#'s type inference isn't good enough to allow this, and made me explicitly give a type to "fi": Seq.map (fun (fi : FileInfo) -> fi.Name) fis Why is this annotation required? If it is known that fis : seq<FileInfo> and that Seq.map : ('a -> 'b) -> seq<'a> -> seq<'b> , then shouldn't it infer that the

Method Chaining vs |> Pipe Operator

隐身守侯 提交于 2019-12-17 19:07:24
问题 So I have the following code: // Learn more about F# at http://fsharp.net open System open System.Linq open Microsoft.FSharp.Collections let a = [1; 2; 3; 4; 54; 9] let c = a |> List.map(fun(x) -> x*3) |> List.filter(fun(x) -> x > 10) let d = a.Select(fun(x) -> x*3).Where(fun(x) -> x > 10) for i in c do Console.WriteLine(i) for i in d do Console.WriteLine(i) Both seem to do the same thing, but most F# examples I see use the |> pipe operator, while I'm more used to method chaining (a.l.a. C#

Implementing a tail recursive version of quicksort-like function in F#/OCaML

故事扮演 提交于 2019-12-17 18:46:01
问题 Is it possible to implement a tail recursive version of the quick sort algorithm (via the continuation pattern)? And if it is, how would one implement it? Normal (not optimized) version: let rec quicksort list = match list with | [] -> [] | element::[] -> [element] | pivot::rest -> let ``elements smaller than pivot``, ``elements larger or equal to pivot``= rest |> List.partition(fun element -> element < pivot) quicksort ``elements smaller than pivot`` @ [pivot] @ quicksort ``elements larger

In Functional Programming, is it considered a bad practice to have incomplete pattern matchings

时光总嘲笑我的痴心妄想 提交于 2019-12-17 18:33:12
问题 Is it generally considered a bad practice to use non-exhaustive pattern machings in functional languages like Haskell or F#, which means that the cases specified don't cover all possible input cases? In particular, should I allow code to fail with a MatchFailureException etc. or should I always cover all cases and explicitly throw an error if necessary? Example: let head (x::xs) = x Or let head list = match list with | x::xs -> x | _ -> failwith "Applying head to an empty list" F# (unlike

Avoiding stack overflow (with F# infinite sequences of sequences)

时间秒杀一切 提交于 2019-12-17 18:27:09
问题 I have this "learning code" I wrote for the morris seq in f# that suffers from stack overflow that I don't know how to avoid. "morris" returns an infinite sequence of "see and say" sequences (i.e., {{1}, {1,1}, {2,1}, {1,2,1,1}, {1,1,1,2,2,1}, {3,1,2,2,1,1},...}). let printList l = Seq.iter (fun n -> printf "%i" n) l printfn "" let rec morris s = let next str = seq { let cnt = ref 1 // Stack overflow is below when enumerating for cur in [|0|] |> Seq.append str |> Seq.windowed 2 do if cur.[0]

F# - Should I learn with or without #light?

允我心安 提交于 2019-12-17 18:25:54
问题 I'm in the process of learning F# and am enjoying it so far. Almost all of the examples online use the lightweight syntax (#light); however, also give a comment about it being on for said example in most cases. Is it better to learn F# using #light enabled or disabled? I'm planning on eventually learning it w/o it turned on but am curious on if it would be better to learn it at the beginning or work on applying it after I know the core language more. 回答1: I'd definitely prefer learning F#

How to enumerate a discriminated union in F#?

夙愿已清 提交于 2019-12-17 18:13:43
问题 How can I enumerate through the possible "values" of a discriminated union in F#? I want to know if is there something like Enum.GetValues(Type) for discriminated unions, tough I am not sure over what kind of data I would enumerate. I would like to generate a list or array of a discriminated union with one item for each option. 回答1: Yes, F# has it's own reflection layer build on top of .NET's reflection to help you make sense of types that are specific to F#, like discriminating unions. Here

F# explicit member constraints: The type variable ^T could not be generalized because it would escape its scope

◇◆丶佛笑我妖孽 提交于 2019-12-17 17:01:30
问题 I'm attempting to use explicit member constraints in F#. The documentation says "F# supports the complete set of constraints that is supported by the common language runtime", but if I actually compile a class with such an explicit constraint, such as the following, I get quite the exotic error. type MyType<'T when ^T: (static member ( + ) : ^T * ^T -> ^T)> = member this.F a b = a + b reports error FS0670: This code is not sufficiently generic. The type variable ^T when ^T : (static member (