f#

Is F# aware of its discriminated unions' compiled forms?

狂风中的少年 提交于 2019-12-21 08:23:06
问题 A discriminated union in F# is compiled to an abstract class and its options become nested concrete classes. type DU = A | B DU is abstract while DU.A and DU.B are concrete. With ServiceStack, the serialization of types to JSON strings and back can be customized with functions. With respect to the DU type, here's how I could do it in C#. using ServiceStack.Text; JsConfig<DU.A>.SerializeFn = v => "A"; // Func<DU.A, String> JsConfig<DU.B>.SerializeFn = v => "B"; // Func<DU.B, String> JsConfig

Is F# aware of its discriminated unions' compiled forms?

喜你入骨 提交于 2019-12-21 08:23:05
问题 A discriminated union in F# is compiled to an abstract class and its options become nested concrete classes. type DU = A | B DU is abstract while DU.A and DU.B are concrete. With ServiceStack, the serialization of types to JSON strings and back can be customized with functions. With respect to the DU type, here's how I could do it in C#. using ServiceStack.Text; JsConfig<DU.A>.SerializeFn = v => "A"; // Func<DU.A, String> JsConfig<DU.B>.SerializeFn = v => "B"; // Func<DU.B, String> JsConfig

MonadFix in strict language

喜你入骨 提交于 2019-12-21 08:17:23
问题 I'm working on camlp4 extension for haskell-like do notation in Ocaml, and trying to figure out how GHC compiles recursive do-bindings (enabled with -XDoRec). I wonder if it possible for monadic fixpoint combinator to exist in strict language (like Ocaml/F#/SML/...)? If yes, how can it look like? Would it be very useful? 回答1: The F# computation expression syntax (related to Haskell do ) supports recursion: let rec ones = seq { yield 1 yield! ones } This is supported because the computation

What am I missing: is function composition with multiple arguments possible?

最后都变了- 提交于 2019-12-21 07:58:06
问题 I understand the basics of function composition in F#, as, for example, described here. Maybe I am missing something, though. The >> and << operators seem to have been defined with the assumption that each function only takes one argument: > (>>);; val it : (('a -> 'b) -> ('b -> 'c) -> 'a -> 'c) = <fun:it@214-13> > (<<);; val it : (('a -> 'b) -> ('c -> 'a) -> 'c -> 'b) = <fun:it@215-14> What I'd like to do, however, is something like the following: let add a b = a + b let double c = 2*c let

F# Unit of Measure, Casting without losing the measure type

眉间皱痕 提交于 2019-12-21 07:55:11
问题 Is there built in version of the type casting functions that preserves units and if not how would I make them? So for example with this code how would I cast intWithSecondsMeasure to a float without losing the measure or multiplying by 1.0<s> ? [<Measure>] type s let intWithSecondsMeasure = 1<s> let justAFloat = float intWithSecondsMeasure 回答1: The answer provided by @kvb certainly works, but I'd prefer not to use the unbox operator for this conversion. There's a better, built in way that I

how to add third party dll reference to F# project?

徘徊边缘 提交于 2019-12-21 07:34:16
问题 I'm adding a third party dll reference to my F# project. I added the dll in references and when I use this i.e highlight the code and do Alt+Ent, I get the error "The namespace or module 'AZROLESLib' not defined." Am I missing some thing. 回答1: In short, you have to use #r "/path/to/AZROLESLib.dll" in order that F# Interactive recognizes and loads the dll file. Adding a dll reference helps Visual Studio to find correct libraries when compiling the project, but it has nothing to do with F#

F# Casting Operators

孤街浪徒 提交于 2019-12-21 07:22:13
问题 What is the difference between the following F# casting operators? I can't seem to understand why and how they are all different. (type) X X :> type X :?> type 回答1: The first isn't a cast in F#, though if you're used to C# it might appear that it works like one. But this is actually invoking a type conversion function (like int ), and the parentheses aren't actually required (and are just likely to make everything more confusing). (int) "4" // the number 4 - this is a conversion, not a cast

Why should successive arguments involving method application be parenthesized?

拟墨画扇 提交于 2019-12-21 07:13:12
问题 Suppose the following F# function: let f (x:int) (y:int) = 42 I suspect that the reason I need to parenthesize the arguments in example z2 below is because of type inference; my example might not be great, but it's easy to imagine how things could get very hairy: let z1 = f 2 3 let z2 = f 2 (f 3 5) However, the following case is less clear to me: let rng = System.Random() let z3 = f 1 rng.Next(5) z3 doesn't work, with a clear error message: error FS0597: Successive arguments should be

Optional parameters and option types using F#

梦想的初衷 提交于 2019-12-21 07:12:10
问题 Consider the following code: type Test () = member o.fn1 (?bo) = 1 member o.fn2 (?bo) = o.fn1 bo member o.fn3 (?bo) = 1 + bo.Value member o.fn4 (?bo) = o.fn3 bo While fn1 and fn2 work just fine, fn4 produces the following error: init.fsx(6,30): error FS0001: This expression was expected to have type int but here has type 'a option MSDN states: Optional parameters are interpreted as the F# option type, so you can query them in the regular way that option types are queried, by using a match

Statically Typed Metaprogramming?

霸气de小男生 提交于 2019-12-21 07:09:04
问题 I've been thinking about what I would miss in porting some Python code to a statically typed language such as F# or Scala; the libraries can be substituted, the conciseness is comparable, but I have lots of python code which is as follows: @specialclass class Thing(object): @specialFunc def method1(arg1, arg2): ... @specialFunc def method2(arg3, arg4, arg5): ... Where the decorators do a huge amount: replacing the methods with callable objects with state, augmenting the class with additional