f#

Thread-safe normal random number generator in F#

半腔热情 提交于 2019-12-07 06:04:03
问题 Needing a random number generator that returns a sample from a normal (Gaussian) distribution, I've ported to F# a portion of John D. Cook's C# generator: let mutable m_w = 521288629u let mutable m_z = 362436069u let private getUint () = m_z <- 36969u * (m_z &&& 65535u) + (m_z >>> 16) m_w <- 18000u * (m_w &&& 65535u) + (m_w >>> 16) (m_z <<< 16) + m_w let private setSeed () = let dt = System.DateTime.Now let x = dt.ToFileTime () m_w <- uint32 (x >>> 16) m_z <- uint32 (x % 4294967296L) let

Haskell style to-infix operator in F#

核能气质少年 提交于 2019-12-07 05:56:58
问题 There is a common problem that F# does not natively support infix-style use of functions that is available in Haskell: isInfixOf :: Eq a => [a] -> [a] -> Bool isInfixOf "bar" "foobarbaz" "bar" `isInfixOf` "foobarbaz" The best known solution for F# can be found here: let isInfixOf (what:string) (where:string) = where.IndexOf(what, StringComparison.OrdinalIgnoreCase) >= 0 let found = "bar" |>isInfixOf<| "foobarbaz" Also, it is easy to improve it a bit, employing native operators precedence: let

Statistical functionalities of F# (or .NET libraries)

柔情痞子 提交于 2019-12-07 05:40:46
问题 Is it possible for a person working with statistic to replace his specialized programs by F#? I'm thinking about SAS/SPSS mainly? Any native support for it in F#? I am not talking about the trivial things as standard deviation and the likes, but for example item-response modeling. UPDATE : Dont't let the item-response modeling put you of! I don't even know it, just an example of things I know they do with SPSS to clarify it's about more advanced features. Short : is there a way to use F# as

Array covariance in F#

为君一笑 提交于 2019-12-07 05:24:11
问题 Since .NET arrays are covariant, the following works in C#: var strArray = new string[0]; object[] objArray = strArray; In F#, given an array, 'T[] , what would be the best way to convert it to obj[] , without re-creating the array (e.g., Array.map box )? I'm using (box >> unbox) , but it feels sloppy. 回答1: box >> unbox seems like a good idea; O(1), and does the job, apparently. Consider also not using this CLR mis-feature. ;) 回答2: As Brian says, there's nothing wrong with box >> unbox ,

Can exception types be generic?

感情迁移 提交于 2019-12-07 05:22:19
问题 I've tried the following, which don't work. exception MyError<'a> of 'a exception 'a MyError of 'a Do I have to use the long form: type MyError<'a>(value) = inherit System.Exception() member this.Value : 'a = value 回答1: According to the specification, you have to use the long form. I didn't find any explanation why that's the case, but the grammar for exception declarations looks like this (and maybe also hints why the behavior is as you described): exception-defn := attributes opt exception

Create an instance of Action<'T> using reflection

China☆狼群 提交于 2019-12-07 05:11:10
问题 How would I create an instance of Action<'T> using reflection? Here's what I have: let makeAction (typ:Type) (f:'T -> unit) = let actionType = typedefof<Action<_>>.MakeGenericType(typ) let converter = FSharpFunc.ToConverter(f) Delegate.CreateDelegate(actionType, converter.Method) which barfs with: System.ArgumentException: Error binding to target method. at System.Delegate.CreateDelegate(Type type, MethodInfo method, Boolean throwOnBindFailure) 'T is an interface, which typ implements. 回答1: I

How do you implement goto in F#?

好久不见. 提交于 2019-12-07 05:04:17
问题 All my favorite languages have a goto command. That is, you can create a label, and then later interrupt the flow of the program to go to the label. One of the more useful applications of this construct is to create an infinite loop, like this: start: goto start Unfortunately, if I undertstand the compiler errors correctly, I can't use this same syntax in F#. So, since it doesn't seem to be supported natively, how can I implement the goto command in F#? Surely, F# is a powerful enough

Parameterized tests in f# - This is not a valid constant expression

巧了我就是萌 提交于 2019-12-07 05:02:12
问题 For some reason when passing arguments to the test via TestCase attrubute, I get the following error message about the first argument, which in this case is an array: This is not a valid constant expression or custom attribute value module GameLogicTest = open FsUnit open NUnit.Framework open GameLogic.Examle // This is not a valid constant expression or custom attribute value [<TestCase( [| 1; 2; 3 |], 3, 1,1)>] let ``let example.`` (a, m, h, c) = a |> proof1 m |> should equal (h,c) But when

F# Type Providers and REST apis

时光总嘲笑我的痴心妄想 提交于 2019-12-07 04:59:05
问题 Is there any reason why the default plug and play F# type providers to web services is soap based wsdl? Is it because of a lack of formal contracts in REST? Such that each REST api may differ significantly and hence making a general REST provider difficult to provide? 回答1: Type providers need machine-readable type schema to do their jobs well. 回答2: Type providers need schema. So you could use Open Data Protocol that is REST + schema. For that task you could use ODataTypeProvider that is

Getting functional sieve of Eratosthenes fast

岁酱吖の 提交于 2019-12-07 04:57:29
问题 I read this other post about a F# version of this algorithm. I found it very elegant and tried to combine some ideas of the answers. Although I optimized it to make fewer checks (check only numbers around 6) and leave out unnecessary caching, it is still painfully slow. Calculating the 10,000 th prime already take more than 5 minutes. Using the imperative approach, I can test all 31-bit integers in not that much more time. So my question is if I am missing something that makes all this so