f#

How to translate the intro ML.Net demo to F#?

大城市里の小女人 提交于 2019-12-19 05:49:30
问题 I'm looking at a the cs file here: https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet/get-started/windows and in my attempt to translate it to F# it compiles just fine but throws a System.Reflection.TargetInvocationException when run: FormatException: One of the identified items was in an invalid format . What am I missing? Editted: Was using records before open Microsoft.ML open Microsoft.ML.Runtime.Api open Microsoft.ML.Trainers open Microsoft.ML.Transforms open

How to translate the intro ML.Net demo to F#?

◇◆丶佛笑我妖孽 提交于 2019-12-19 05:47:43
问题 I'm looking at a the cs file here: https://www.microsoft.com/net/learn/apps/machine-learning-and-ai/ml-dotnet/get-started/windows and in my attempt to translate it to F# it compiles just fine but throws a System.Reflection.TargetInvocationException when run: FormatException: One of the identified items was in an invalid format . What am I missing? Editted: Was using records before open Microsoft.ML open Microsoft.ML.Runtime.Api open Microsoft.ML.Trainers open Microsoft.ML.Transforms open

Option vs Exception in exception handling

怎甘沉沦 提交于 2019-12-19 05:46:51
问题 After using F# option type for a while, I realize that it could be used for handling exceptional cases. I can use either option or Exception in the following examples: The find functions from List/Array/Seq modules raise KeyNotFoundException in uncommon cases, while corresponding tryFind counterparts return None in those situations. When I do backtracking (in solving N-queens, Sudoku, etc), whenever a branch has no solution, I can either raise an exception and catch it later or return None to

What is the name of |> in F# and what does it do?

我们两清 提交于 2019-12-19 05:37:48
问题 A real F# noob question, but what is |> called and what does it do? 回答1: It's called the forward pipe operator. It pipes the result of one function to another. The Forward pipe operator is simply defined as: let (|>) x f = f x And has a type signature: 'a -> ('a -> 'b) -> 'b Which resolves to: given a generic type 'a, and a function which takes an 'a and returns a 'b, then return the application of the function on the input. You can read more detail about how it works in an article here. 回答2:

Using extension methods defined in C# from F# code

戏子无情 提交于 2019-12-19 05:18:29
问题 I have a series of extension methods defined for various classes in a C# library. I'm currently writing some F# code and instead of rewriting that code I would simply like to use my existing extension methods in my F# code. I have added a reference to the library and used the open statement to import the namespace, but the extension methods to not appear in F# 回答1: Update: In the current version of F#, you can simply consume extension methods by adding open TheNamespaceOfExtensionMethod to

F# Option equivalent of C#'s ?? operator

无人久伴 提交于 2019-12-19 05:04:58
问题 I am looking for a way to get the value of an F# option or use a default value if it is None. This seems so common I can't believe something predefined doesn't exist. Here is how I do it right now: // val getOptionValue : Lazy<'a> -> Option<'a> -> 'a let getOptionValue (defaultValue : Lazy<_>) = function Some value -> value | None -> defaultValue.Force () I am (sort of) looking for the F# equivalent of the C# ?? operator: string test = GetString() ?? "This will be used if the result of

Is there a standard option workflow in F#?

喜夏-厌秋 提交于 2019-12-19 05:03:28
问题 Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations (1, 2) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. 回答1: There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number

Is there a standard option workflow in F#?

烂漫一生 提交于 2019-12-19 05:03:04
问题 Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations (1, 2) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. 回答1: There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number

In F#, is it possible to have a tryParse function that infers the target type

假装没事ソ 提交于 2019-12-19 03:17:07
问题 Presently we do this... let parseDate defaultVal text = match DateTime.TryParse s with | true, d -> d | _ -> defaultVal Is it possible to do this... let d : DateTime = tryParse DateTime.MinValue "2015.05.01" 回答1: Yes. Welcome to the world of member constraints, ref, and byref values. let inline tryParseWithDefault defaultVal text : ^a when ^a : (static member TryParse : string * ^a byref -> bool) = let r = ref defaultVal if (^a : (static member TryParse: string * ^a byref -> bool) (text, &r

Can I explicitly check for cancellation / terminate async computation?

百般思念 提交于 2019-12-19 03:17:03
问题 I have an async computation like the following (see inline comments): async { //... do! Async.Sleep(100) //cancellation may happen during sleep //... but isn't checked at the end of the sleep, so regular, non-async computations are executed here } In order to force a cancellation check / terminate the entire async computation before the "regular" computation part is reached, I insert an effective no-op do! Async.Sleep(1) immediately after the do! Async.Sleep(100) . Is there a cleaner way to