f#

F# member constraints + ^a byref parameters

左心房为你撑大大i 提交于 2019-12-18 04:51:11
问题 After some playing around F# member constraints feature and writing function like this: let inline parse< ^a when ^a : (static member Parse: string -> ^a) > s = (^a: (static member Parse: string -> ^a) s) That works perfectly fine: let xs = [ "123"; "456"; "999" ] |> List.map parse<int> I'm trying to write other func tryParse , that uses static method TryParse and wraps the parse result into 'a option type for better support in F#. Something like this doesn't compiles: let inline tryParse s =

How can I pass an F# delegate to a P/Invoke method expecting a function pointer?

喜夏-厌秋 提交于 2019-12-18 04:49:06
问题 I'm attempting to set up a low-level keyboard hook using P/Invoke in an F# application. The Win32 function SetWindowsHookEx takes a HOOKPROC for its second argument, which I've represented as a delegate of (int * IntPtr * IntPtr) -> IntPtr , similar to how this would be handled in C#. When calling the method, I get a MarshalDirectiveException stating that the delegate parameter cannot be marshaled because Generic types cannot be marshaled I'm not sure how generics are involved, as all types

Tree Representation in F#

一曲冷凌霜 提交于 2019-12-18 04:46:15
问题 I'm trying to implement a tree in F# using a list of tuples. [a] where a = (string, [a]) Each node has a list of their children and leaf nodes would be (name, []) I want to be able to recursively iterate through each level of the list like this. a b e c d f g They wont always be binary trees however. let t2 = [("a", [("b", [("c", []), ("d", [])]), ("e", [("f", []), ("g", [])])])] let rec checkstuff tple = match tple with | (_, []) -> true | (node, children) -> List.fold ( || ) false (List.map

F# equivalent to Eval

心已入冬 提交于 2019-12-18 04:38:15
问题 Is there an F# equivalent to eval? My intent is to have my app load a small code sample from a file and essentially let file = "c:\mysample" let sample = loadFromFile file let results = eval(sample) I am new to F# and trying to figure out some of the limitations before I apply it to a project. Thank you 回答1: There is no function that would allow you to do this directly. However, when you want to compile an F# source file programatically, you can invoke the F# compiler from your application.

Using NLog with F# Interactive in Visual Studio - Need documentation

ⅰ亾dé卋堺 提交于 2019-12-18 04:23:24
问题 I have a need to capture the input and output of F# functions when using F# Interactive. I am able to get NLog to work just fine when the program is run under Visual Studio using F5 or Ctrl-F5. Also the same methods that contain statements to output to the log work just fine and are called when invoked via F# Interactive; just nothing in the log file. I also tried the following with F# Interactive to setup references to NLog and still nothing in the log when run from F# Interactive. #I @"..

Need help regarding Async and fsi

元气小坏坏 提交于 2019-12-18 04:23:11
问题 I'd like to write some code that runs a sequence of F# scripts (.fsx). The thing is that I could have literally hundreds of scripts and if I do that: let shellExecute program args = let startInfo = new ProcessStartInfo() do startInfo.FileName <- program do startInfo.Arguments <- args do startInfo.UseShellExecute <- true do startInfo.WindowStyle <- ProcessWindowStyle.Hidden //do printfn "%s" startInfo.Arguments let proc = Process.Start(startInfo) () scripts |> Seq.iter (shellExecute "fsi") it

Can you define your own operators in F#?

荒凉一梦 提交于 2019-12-18 04:02:36
问题 Is there a way to define your own operators in F#? If so can someone give me an example for this? I searched briefly, but couldn't find anything. 回答1: Yes: let (+.) x s = [for y in s -> x + y] let s = 1 +. [2;3;4] The characters that can be used in an F# operator are listed in the docs. They are !%&*+-./<=>@^|~ and for any character after the first, ? . Precedence and fixity are determined by the first character of the operator (see the spec). You can create your own let-bound operators as I

How do I translate a `where T : U` generic type parameter constraint from C# to F#?

陌路散爱 提交于 2019-12-18 03:09:16
问题 F# is giving me some trouble with its type inference rules. I'm writing a simple computation builder but can't get my generic type variable constraints right. The code that I would want looks as follows in C# : class FinallyBuilder<TZ> { readonly Action<TZ> finallyAction; public FinallyBuilder(Action<TZ> finallyAction) { this.finallyAction = finallyAction; } public TB Bind<TA, TB>(TA x, Func<TA, TB> cont) where TA : TZ { // ^^^^^^^^^^^^^ try // this is what gives me a headache { // in the F#

Is using a StringBuilder a right thing to do in F#?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 03:01:59
问题 StringBuiler is a mutable object, F# encourages employing immutability as much as possible. So one should use transformation rather than mutation. Does this apply to StringBuilder when it comes to building a string in F#? Is there an F# immutable alternative to it? If so, is this alternative as efficient? A snippet 回答1: I think that using StringBuilder in F# is perfectly fine - the fact that sb.Append returns the current instance of StringBuilder means that it can be easily used with the fold

Scrap Your Boilerplate in f#

断了今生、忘了曾经 提交于 2019-12-17 22:44:27
问题 I've used the Scrap Your Boilerplate and Uniplate libraries in the Haskell programming language, and I would find that form of generic programming over discriminated unions to be really useful. Is there an equivalent library in the f# programming language? 回答1: Not that I know of; without support built-in to the language/compiler, I expect the only alternative is a reflection-based version. (I don't know how Uniplate is implemented - do you?) Here's the code for a reflection-based version