f#

Unable to use protected events in F#

你。 提交于 2019-12-12 11:27:10
问题 Let's say we have the following C# class public class Class1 { protected event EventHandler ProtectedEvent; protected virtual void OverrideMe() { } } It seems to be impossible to use the ProtectedEvent in F#. type HelpMe() as this = inherit Class1() do printfn "%A" this.ProtectedEvent member x.HookEvents() = printfn "%A" x.ProtectedEvent member private x.HookEvents2() = printfn "%A" x.ProtectedEvent override x.OverrideMe() = printfn "%A" x.ProtectedEvent In this example I have attempted to

F# multi-condition if/else versus matching

这一生的挚爱 提交于 2019-12-12 11:26:31
问题 I'm new to F# and have been implementing simple algorithms to learn the language constructs. I implemented the bisection method using if/else and then wanted to learn how to do it using matching. if fc = 0.0 then printfn "%i/%i - f(%f) = %f, f(%f) = %f, f(%f) = %f" new_count n a fa b fb c fc else if ((b - a) * 0.5) < eps then printfn "%i/%i - f(%f) = %f, f(%f) = %f, f(%f) = %f" new_count n a fa b fb c fc else if new_count = n then printfn "%i/%i - f(%f) = %f, f(%f) = %f, f(%f) = %f" new_count

Pondering name of pattern seen in Elm and if other similar cases

好久不见. 提交于 2019-12-12 11:20:23
问题 I'm currently a student of FP. As I look around different syntax offer by different functional languages, I've come across a pattern in Elm sample code. I am curious about it. Here is the sample code myList = [{foo = "bar1"},{foo = "bar2"}] foos = myList |> List.map .foo In the last line here, List.map is being passed .foo . I believe this style is called point-free but what about the specific pattern of passing in an attribute to the List.map function? Is this a more common thing? Is it

What is the difference between the generic signifier ' and the symbol ^ In F# method signatures

北城余情 提交于 2019-12-12 11:14:44
问题 I understand the tick to signify a generic parameter, as in: Seq.append : seq<'T> -> seq<'T> -> seq<'T> but what does the caret signify, as in: Seq.average : seq<^T> -> ^T 回答1: The detailed signature is: Seq.average : seq<^T> -> ^T (requires ^T with static member (+) and ^T with static member DivideByInt and ^T with static member Zero) Unlike Seq.append , Seq.average needs some more constraints on type of elements. Particularly: _ DivideByInt (s1 + s2 + ... + sn) n where n <> 0 Seq.average

Extension method with F# function type

时间秒杀一切 提交于 2019-12-12 11:14:26
问题 The MSDN doc on Type Extensions states that "Before F# 3.1, the F# compiler didn't support the use of C#-style extension methods with a generic type variable, array type, tuple type, or an F# function type as the “this” parameter." (http://msdn.microsoft.com/en-us/library/dd233211.aspx) How can be a Type Extension used on F# function type? In what situations would such a feature be useful? 回答1: Here is how you can do it: [<Extension>] type FunctionExtension() = [<Extension>] static member

Why can't we satisfy F# static member constraints with type extensions?

我只是一个虾纸丫 提交于 2019-12-12 11:10:46
问题 I'd like to be able to extend types from other libraries with static methods to enable generic arithmetic. Take, for example, the newly minted SIMD-friendly fixed-size VectorN types from Microsoft. They define Zero , they define (+) , they define (/) , but I can't use Array.average on them because they don't define DivideByInt , which I'd be happy to: open System.Numerics type Vector2f with static member DivideByInt (v:Vector2f) (i:int) = v / Vector2f(single i, single i) let bigArray :

Why does the F# compiler give an error for one case but not the other?

安稳与你 提交于 2019-12-12 11:04:10
问题 I'm working on a platform invoke call from F#, and I am getting a compiler error I really can't make that much sense out of. First, let me show the C signature of what I am doing: int Foo( ULONG_PTR *phHandle, DWORD flags ); In F#, I think the correct way to invoke this natively is as so: [<DllImport("somedll.dll")>] static extern int APlatformInvokeCall ( [<Out>]nativeint& phHandle, uint32 flags ) If I try to call this in a class , I get a compilation error when calling it like so: type

Reflection to find out if property is of option type

匆匆过客 提交于 2019-12-12 11:03:07
问题 I am filling some object's fields with data using reflection. As my object is of F# type, it has some Option fields. In case of option property.SetValue(object, newValue) reasonably fails, because it needs property.SetValue(object, Some(newValue)) Hence, I am trying to find out if a property is of type Option . I can do it like this: let isOption (p:PropertyInfo) = p.PropertyType.Name.StartsWith("FSharpOption") But there must be some better way, must it not? And I must say it is strange to me

Suave - Control when responses are 'cached' or recalculated

夙愿已清 提交于 2019-12-12 10:56:28
问题 I want to understand how to control when responses are 'cached' versus when they are 'recalculated'. As an example: [<EntryPoint>] let main [| port |] = let config = { defaultConfig with bindings = [ HttpBinding.mk HTTP IPAddress.Loopback (uint16 port) ] listenTimeout = TimeSpan.FromMilliseconds 3000. } let appDemo:WebPart = DateTime.Now.ToString() |> sprintf "Server timestamp: %s" |> Successful.OK startWebServer config appDemo If I run the above webserver and hit it several times then each

FParsec: backtracking `sepBy`

怎甘沉沦 提交于 2019-12-12 10:54:36
问题 Consider the following toy grammar and parser: (* in EBNF: ap = "a", { "ba" } bp = ap, "bc" *) let ap = sepBy1 (pstring "a") (pstring "b") let bp = ap .>> (pstring "bc") let test = run bp "abababc" I get the following output: Error in Ln: 1 Col: 7 abababc ^ Expecting: 'a' Clearly sepBy1 sees the last b and expects it to lead into another a , failing when it doesn't find one. Is there a variant of sepBy1 which would backtrack over b and make this parse succeed? Is there any reason why I