f#

F# - Breaking a List into Concatenated Strings by an Interval

青春壹個敷衍的年華 提交于 2019-12-09 04:33:26
I have a list of email addresses, and I need to send an email notification to each address. I'd like to do this in blocks of 25 addresses at a time. Is there any quick way in a functional language like F# to "fold" (concatenate) 25 emails addresses together... each separated by a semicolon. I know there is the String.Split method in .NET, but I need to concat 25 at a time. What is the most elegant way to perform this in F#? Here's a way to break into groups of at most N: // break a sequence up into a sequence of arrays, // each of length at most 'n' let Break n (s:seq<_>) = seq { use e = s

How to make illegal values unrepresentable? [duplicate]

夙愿已清 提交于 2019-12-09 04:32:59
问题 This question already has answers here : Positive integer type (6 answers) Closed 2 years ago . A method of design in Functional Programming is making illegal states unrepresentable. I always see this being accomplished with the structure of types, but what about the value of types? What if I have a string called Email and I only want it to hold a valid email address (checked against some Regex)? How can I do this in a functional way (without using OOP)? 回答1: I'd posit, the same way you do

F# vs IronPython: When is one preferred to the other?

≯℡__Kan透↙ 提交于 2019-12-09 04:15:48
问题 While the languages F# and IronPython are technically dissimilar, there is large overlap between their potential uses in my opinion. When is one more applicable than the other? So far it look to me like F# is computationally more efficient while IronPython inherits a better library from Python. I am happy to be corrected. There is a somewhat relevant question is F# to IronPython/IronRuby as C# is to VB.NET? but most of the answers there are about the language paradigms rather than their

Is F# really faster than Erlang at spawning and killing processes?

萝らか妹 提交于 2019-12-09 04:13:13
问题 Updated: This question contains an error which makes the benchmark meaningless. I will attempt a better benchmark comparing F# and Erlang's basic concurrency functionality and inquire about the results in another question. I am trying do understand the performance characteristics of Erlang and F#. I find Erlang's concurrency model very appealing but am inclined to use F# for interoperability reasons. While out of the box F# doesn't offer anything like Erlang's concurrency primitives -- from

Log in function or function using it?

主宰稳场 提交于 2019-12-09 03:38:10
问题 Is it best (I'm aware of that there's no silver bullet, but there may be some advantage by using one over the other) - to log in the calling function, or the function calling it? Examples: Approach 1 module MongoDb = let tryGetServer connectionString = try let server = new MongoClient(connectionString).GetServer() server.Ping() Some server with _ -> None Usage: match MongoDb.tryGetServer Config.connectionString with | None -> logger.Information "Unable to connect to the database server." // .

does a tee function exist somewhere in F# library?

感情迁移 提交于 2019-12-09 03:29:25
问题 ...or in FSharpx? let tee sideEffect = fun x -> do sideEffect x x The usage could be something like f >> tee (printfn "F returned: %A") >> g >> h Or is there another simple way to do this? thanks! 回答1: ExtCore includes a function called tap which does exactly what you want. I use it for primarily for inspecting intermediate values within an F# "pipeline" (hence the name). For example: [| 1;2;3 |] |> Array.map (fun x -> x * 2) |> tap (fun arr -> printfn "The mapped array values are: %A" arr) |

Working around incomplete pattern matching on enums

为君一笑 提交于 2019-12-09 02:41:46
问题 Are there any creative ways to work around .NET's "weak" enums when pattern matching? I'd like them to function similarly to DUs. Here's how I currently handle it. Any better ideas? [<RequireQualifiedAccess>] module Enum = let unexpected<'a, 'b, 'c when 'a : enum<'b>> (value:'a) : 'c = //' failwithf "Unexpected enum member: %A: %A" typeof<'a> value //' match value with | ConsoleSpecialKey.ControlC -> () | ConsoleSpecialKey.ControlBreak -> () | _ -> Enum.unexpected value //without this, gives

Is there anyway to use C# implicit operators from F#?

浪子不回头ぞ 提交于 2019-12-09 02:36:57
问题 If I have a C# class with implicit conversion to double, like so: public class Parameter { private double _value; public Parameter(double value) { _value = value } public static implicit operator double(Parameter p) { return _value; } } F# doesn't like me trying to use it as if it were a float : let a = Parameter(4.0) let b = Parameter(2.0) let c = a * Math.Sin(b) <-- 'expected float, here Parameter' Is there any way to do this (I'm guessing there isn't, based on this question/answer), and if

F# performance question: what is the compiler doing?

谁都会走 提交于 2019-12-09 00:57:13
问题 Referencing this code: F# Static Member Type Constraints Why is, for example, let gL = G_of 1L [1L..100000L] |> List.map (fun n -> factorize gL n) significantly slower than [1L..100000L] |> List.map (fun n -> factorize (G_of 1L) n) By looking at Reflector, I can see that the compiler is treating each of these in very different ways, but there is too much going on for me to decipher the essential difference. Naively I assumed the former would perform better than the later because gL is

Generate powerset lazily

匆匆过客 提交于 2019-12-09 00:50:27
问题 I want to calculate powerset of a set. Because I don't need the whole powerset at a time, it's better to generate it lazily. For example: powerset (set ["a"; "b"; "c"]) = seq { set []; set ["a"]; set ["b"]; set ["c"]; set ["a"; "b"]; set ["a"; "c"]; set ["b"; "c"]; set ["a";"b"; "c"]; } Since the result is a sequence, I prefer it in the above order. How can I do it in an idomatic way in F#? EDIT: This is what I'm going to use (based on BLUEPIXY's answer): let powerset s = let rec loop n l =