f#


Is there an equivalent to creating a C# implicit operator in F#?

北城以北 提交于 2020-01-28 19:46:24
问题 In C# I can add implicit operators to a class as follows: public class MyClass { private int data; public static implicit operator MyClass(int i) { return new MyClass { data = i }; } public static implicit operator MyClass(string s) { int result; if (int.TryParse(s, out result)) { return new MyClass { data = result }; } else { return new MyClass { data = 999 }; } } public override string ToString() { return data.ToString(); } } Then I can pass any function that is expecting a MyClass object a

Interop between F# and C# lambdas

徘徊边缘 提交于 2020-01-27 22:40:56
问题 F# powerpack comes with a set of conversion methods to translate from Func<...> to F# functions, either standard or tupled ones. But is it possible to achieve the opposite: in case you want to call from F# code a C# method that takes Func<...> and want to use native F# lambda expression (e.g. fun x -> some_function_of(x))? If I send a F# function with a signature 'a -> 'b to a C# method that expects Func then F# compiler generates the following error: This expression was expected to have type

Interop between F# and C# lambdas

好久不见. 提交于 2020-01-27 22:39:53
问题 F# powerpack comes with a set of conversion methods to translate from Func<...> to F# functions, either standard or tupled ones. But is it possible to achieve the opposite: in case you want to call from F# code a C# method that takes Func<...> and want to use native F# lambda expression (e.g. fun x -> some_function_of(x))? If I send a F# function with a signature 'a -> 'b to a C# method that expects Func then F# compiler generates the following error: This expression was expected to have type

How to use Map.TryFind?

二次信任 提交于 2020-01-26 04:19:19
问题 When I run the code below I get an error. I am using Map.TryFind and its not working. In console, I get a red line under familyinc.TryFind(tract) and the error below. let data = seq { for (state,work) in statecsv do let (family,income) = familyinc.TryFind(state) let fam = match fam with | Some x -> x | None -> "Not a Record" let inc = match inc with | Some x -> x | None -> "Not an income" yield (state,work,inc,fam) } The ERROR: error FS0001: This expression was expected to have type ''a * 'b'

Find repeating values in List

一曲冷凌霜 提交于 2020-01-26 01:46:06
问题 I want to return true if I find a repeating value in the list let rec repeats L = match L with | [] -> false | x::xs when x = xs.Head -> true | x::xs -> repeats xs;; repeats [1;2;3;4;5] Should return false. But I get this error: System.InvalidOperationException: The input list was empty. at Microsoft.FSharp.Collections.FSharpList`1.get_Head() at FSI_0003.repeats[a](FSharpList`1 L) at <StartupCode$FSI_0004>.$FSI_0004.main@() at main@dm() Stopped due to error What should I do to fix the error?

Code coverage using dotCover throws an error - FAKE F#MAKE

血红的双手。 提交于 2020-01-25 21:26:28
问题 I am trying to use DotCover in FAKE , but it is throwing some error , as I am new to FAKE as well as F# , it's becoming difficult for me to understand the root cause of the problem . Here is the code : #r "D:/FAKEProject/Fake/packages/FAKE/tools/FakeLib.dll" open Fake open Fake.DotCover let testDir = "D:/FAKEProject/Fake/test/" let filters = "" Target "Clean" (fun _ -> CleanDirs [testDir] ) Target "TestCoverage" (fun _ -> !! ("D:/FAKEProject/Fake/UnitTest/UnitTest.dll") |> DotCoverNUnit (fun

xUnit tests under F# : It looks like you're trying to execute an xUnit.net unit test ('System.ArgumentException' occurred in xunit.runner.utility.dll)

▼魔方 西西 提交于 2020-01-25 11:53:29
问题 0. My setup is as follows Visual Studio 2012 Ultimate, update 3, v.11.0.60610.01 Visual F# 2012 04940-004-0038003-02527 TestDriven.Net 3.5 Beta 3 Personal 1. What I have in packages.config: <?xml version="1.0" encoding="utf-8"?> <packages> <package id="FsUnit.xUnit" version="1.2.1.2" targetFramework="net45" /> <package id="xunit" version="1.9.1" targetFramework="net45" /> </packages> 2. What I ran: xunit.installer.exe 3. This is what the packages folder looks like \packages\FsUnit.xUnit.1.2.1

F# Count how Many times a substring Contains within a string

纵饮孤独 提交于 2020-01-25 08:59:06
问题 How could one count how many times a substring exists within a string? I mean if you have a String " one, two, three, one, one, two" how could you make it count "one" being present 3 times? I thought String.Contains would be able to do the job but that only checks if the substring is present at all. String.forall is for chars and therefofre niether an option. So i am really at a complete halt here. Can some enligten me? 回答1: You can use Regex.Escape to turn the string you're searching for

How to convert string array to float array and substitute Double.NaN for non-numeric values?

笑着哭i 提交于 2020-01-25 03:41:16
问题 I'm writing a parser for CSV data, and am trying to determine how to handle records that are blank ("") or contain character data ("C"). The parser code I have below works great, but forces me to deal with the float conversions later. I'd like to be able to just make my string[][] a float[][], and handle the conversions when I parse the file, but I notice that it blows up with any non-numeric data. Ideally there would be no non-numeric or blank values, but they are unavoidable, and as such,

Create an action in F# in order to use a c# method

人走茶凉 提交于 2020-01-24 21:22:19
问题 I would like to use this method from Owin implementation of HttpListener : public static IDisposable Create(AppFunc app, IDictionary<string, object> properties) AppFunc has a signature like this : IDictionnary<string,Object> -> Task I wanted to create a task but it requires an action, and I failed to know how to do it in F#. The best I managed so far was using the code in this question : module CsharpAction open System type Wrapper<'T>(f:'T -> unit) = member x.Invoke(a:'T) = f a let

工具导航Map