f#

Retrieve MethodInfo of a F# function

无人久伴 提交于 2019-12-18 13:07:31
问题 I would like to write a function that takes a function f as an argument and returns the System.Reflection.MethodInfo associated to f. I'm not quite sure if it is feasible or not. 回答1: So, I finally found a solution. Very hacky, but hey! It works! (edit: in Debug mode only). let Foo (f:S -> A[] -> B[] -> C[] -> D[] -> unit) = let ty = f.GetType() let argty = [|typeof<S>; typeof<A[]>; typeof<B[]>; typeof<C[]>;typeof<D[]>|] let mi = ty.GetMethod("Invoke", argty) let il = mi.GetMethodBody()

C#'s 'dynamic' in F#

心已入冬 提交于 2019-12-18 12:57:09
问题 One example of using the DLR in C# is as follows: dynamic dyn = new MyObject(); dyn.MyMethod(); //resolved at runtime what would be the equivalent in F#? Thanks. 回答1: The ? operator has similar expressive power to the dynamic keyword in C# (but it can be only used for reading of properties, method invocation and setting of properties). There is no built-in implementation that would allow you to dynamically use properties or methods of a .NET class (via Reflection or DLR), but there are some

Does F# documentation have a way to search for functions by their types?

夙愿已清 提交于 2019-12-18 12:52:28
问题 Say I want to know if F# has a library function of type ('T -> bool) -> 'T list -> int ie, something that counts how many items of a list that a function returns true for. (or returns the index of the first item that returns true) I used to use the big list at the MSR site for F# before the documentation on MSDN was ready. I could just search the page for the above text because the types were listed. But now the MSDN documentation only lists types on the individual pages--the module page is a

Creating F# record through reflection

走远了吗. 提交于 2019-12-18 12:33:13
问题 How can I create a record type in F# by using reflection? Thanks 回答1: You can use FSharpValue.MakeRecord[MSDN] to create a record instance, but I don't think there's anything baked into F# for defining record types . However, records compile to simple classes, so you could build a class as you would in C#. TypeBuilder[MSDN] may be a good starting point. UPDATE Adding [<CompilationMapping(SourceConstructFlags.RecordType)>] to the type is all that's required to make it a record. Here's an

Recursive grammars in FParsec

限于喜欢 提交于 2019-12-18 12:24:52
问题 I've decided to check out FParsec, and tried to write a parser for λ expressions. As it turns out, eagerness makes recursive parsing difficult. How can I solve this? Code: open FParsec type λExpr = | Variable of char | Application of λExpr * λExpr | Lambda of char * λExpr let rec FV = function | Variable v -> Set.singleton v | Application (f, x) -> FV f + FV x | Lambda (x, m) -> FV m - Set.singleton x let Λ0 = FV >> (=) Set.empty let apply f p = parse { let! v = p return f v } let λ e = let

Sequence vs LazyList

心不动则不痛 提交于 2019-12-18 12:15:31
问题 I can't wrap my head around the differences between sequence and LazyList . They're both lazy and potentially infinite. While seq<'T> is IEnumerable<'T> from .NET framework, LazyList is included in F# PowerPack. In practice, I encounter sequences much more often than LazyList s. What are their differences in terms of performance, usage, readability, etc? What are reasons for such a bad reputation of LazyList compared to that of seq ? 回答1: LazyList computes each element only once regardless of

What makes FSharpFunc<> faster than Func<>?

烂漫一生 提交于 2019-12-18 12:10:49
问题 I'm curious about the performance enhancements that have been made for FSharpFunc<>. Is it the fact that it does not contain multiple delegate so there is no need to loop over all the references when firing a function call ? Anything else ? 回答1: I think that the primary motivation for using FSharpFunc<> rather than Func<> or any other delegate is that you cannot create a class that would inherit from a delegate type (at first, this sounds reasonable, but in .NET, delegate is actually just

alternative to typeclasses?

一世执手 提交于 2019-12-18 11:40:27
问题 haskell programmer. using F#. no typeclasses in F#. what to use when I need typeclasses? 回答1: Do check out this as someone suggested. I think the short answer is to pass dictionaries-of-operations (as Haskell would under the hood; the witness for the instance). Or change the design so you don't need typeclasses. (This always feels painful, since typeclasses are the best thing ever and it's hard to leave them behind, but before Haskell and typeclasses came along, people still managed to

F#: Asynch and Tasks and PLINQ, oh my!

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 11:33:46
问题 When F# comes out, I am going to have an embarrassment of riches in the asynchronous/parallel programming area. An answer to this question does a pretty good job of describing the differences between Tasks, Parallel LINQ, and Reactive Framework, but I was wondering how asynchronous workflows fit into the picture, exactly. Please correct me if I'm wrong, but as I understand it asynch workflows are going to be the easiest way to work with IO-bound operations, particularly those that have an

Does F# provide you automatic parallelism?

荒凉一梦 提交于 2019-12-18 11:31:53
问题 By this I meant: when you design your app side effects free, etc, will F# code be automatically distributed across all cores? 回答1: No, I'm afraid not. Given that F# isn't a pure functional language (in the strictest sense), it would be rather difficult to do so I believe. The primary way to make good use of parallelism in F# is to use Async Workflows (mainly via the Async module I believe). The TPL (Task Parallel Library), which is being introduced with .NET 4.0, is going to fulfil a similar