c#-to-f#

C# to F# class transition

三世轮回 提交于 2019-12-11 04:28:02
问题 I'm a C# dev since long ago. I learn F# and use it for scientific purposes/research/approbation works. I find many its functional features powerful but I'm straggling to write classes — the essential thing I know to do very well in C#. What would help me (and the community) is to translate the following C# code. class CoolAttribute : Attribute { } class BaseClass { public BaseClass(int zzz) { // zzz doesn't matter; what matters is that zzz is required } public virtual void XHello() { Console

Convert C# delegate to f#

柔情痞子 提交于 2019-12-10 19:19:48
问题 how do I convert delegate to F#? the delegate: delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam); Edited What I'm doing is to do Low Level Keyboard Hook using managed API from c# in F# Code: [<DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)>] extern bool UnhookWindowsHookEx(System.IntPtr hhk); [<DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)>] extern System.IntPtr CallNextHookEx(System.IntPtr hhk, int nCode,System.IntPtr wParam, System

F# compiler error “This expression was expected to have type unit but here has type bool.” expression in {if else} statements

不打扰是莪最后的温柔 提交于 2019-12-10 17:14:29
问题 I have written such a function in F#: let TwistBasket (reverse: bool, quarters: int, overTwist: int byref) = overTwist <- 50 WaitForBasketReady() waitBasket.Reset() let move = 135*quarters - 25 + overTwist let speed = match reverse with | true -> -75y | false -> 75y let waitHandle = motorBasket.SpeedProfile(speed, 15u, uint32 move, 10u, true) Task.Factory.StartNew(fun () -> waitHandle.WaitOne() if (overTwist <> 0) then motorBasket.SpeedProfile(sbyte -speed, 0u, uint32 overTwist, 0u, true)

How to generate null strings for FsCheck tests

五迷三道 提交于 2019-12-10 13:03:34
问题 Using FsCheck, the F# version of the Haskell QuickCheck test library, to generate tests from C#, I found that the random string generator does not generate the null string. using FsCheck.Fluent; Spec.ForAny<string>(s => s != null).QuickCheck(); // always pass Furthermore, there seems not to handle null strings by design, but I have not managed to pin it down from the documentation. For example, just picking between two strings, one of them null, won't work: var strings = Any.ValueIn<string>

C#-style event accessors for CLI events in F#

戏子无情 提交于 2019-12-10 02:21:06
问题 I am exposing an event from F# to C# like this: let event = new DelegateEvent<EventHandler>() member x.Ping() = event.Trigger([| x; EventArgs.Empty |]) [<CLIEvent>] member x.PingEvent = event.Publish But I'd like some code to run whenever handlers are added or removed. I think this can be done in C# like this: public event EventHandler PingEvent { add { //do something } remove { //do something } } How do I write the above in F#? 回答1: In F#, you can define events with implementations of the

In F# how can I produce an expression with a type of Func<obj>?

旧时模样 提交于 2019-12-09 14:25:31
问题 I'm working with an api that requires a value of type Func. (Specifically, I'm trying to call ModelMetadataProviders.Current.GetMetadataForType(). How can I construct that value in F#? 回答1: When calling a method that takes any delegate of the Func you shouldn't need to explicitly create the delegate, because F# implicitly converts lambda expressions to delegate type (in member calls). I think that just calling the method with lambda function should work (if it doesn't, could you share the

Agent/MailboxProcessor in C# using new async/await

孤街醉人 提交于 2019-12-08 23:15:08
问题 This question combines two topics I don't fully understand Reading through a paper about async in F#, I came across the topic of Agents/MailboxProcessors, which can be used to implement reactive state machines. Could the new async/await functionality in C#5 be used to implement something similar in C#, or is there already something analogue that would be better suited? 回答1: With a bit of pretty horrible hacking, you can use the MailboxProcessor type from C# using async . Some difficulties are

Converting a list of strings into floats/ints in F#

时光毁灭记忆、已成空白 提交于 2019-12-08 17:38:22
问题 Is there a quick and simple way to convert an entire list of strings into floats or integers and add them together similar to this in F#? foreach(string s in list) { sum += int.Parse(s); } 回答1: Something like this should have the same effect: let sum = list |> Seq.map System.Int32.Parse |> Seq.sum F# doesn't seem to support referring to the method on int so I had to use System.Int32 instead. In F# the type seq is an alias for the .NET IEnumerable , so this code works on arrays, lists etc.

F#: Some, None, or Exception?

半世苍凉 提交于 2019-12-08 14:59:45
问题 I have been teaching myself F# lately, and I come from an imperative (C++/C#) background. As an exercise I have been working on functions that can do stuff with matrices, like add, multiply, get determinants, etc. Everything is going well in this regard, but I find that maybe I am not making the best decisions when it concerns handling invalid inputs, for example: // I want to multiply two matrices let mult m1 m2 = let sizeOK = validateDims m1 m2 // Here is where I am running to conceptual

Implementing C# method returning Task<T> in F#

Deadly 提交于 2019-12-07 08:41:21
问题 I'm creating a type in F# that inherits from a C# class that exposes a method that returns Task<T> in C#. I'm trying to work out what'd be the best way to do that in F# Say my C# looks like this: public class Foo { public TextWriter Out { get { return Console.Out; } } public virtual Task<SomeEnum> DoStuff() { return Task.FromResult(SomeEnum.Foo); } } public enum SomeEnum { Foo, Bar } My first pass of inheriting that type in F# looks like so: type Bar() = inherits Foo() override this.DoStuff()