f#

Get from IQueryable to IEnumerable

元气小坏坏 提交于 2019-12-07 01:27:47
问题 this should be a quick one for all you f# rockers, but it's got me stuck at the moment. I've got an F# type which I'm trying to get to implement a c# interface public interface ICrudService<T> where T: DelEntity, new() { IEnumerable<T> GetAll(); } here's how it's implemnted in c#: public IEnumerable<T> GetAll() { return repo.GetAll(); } repo.GetAll returns an IQueryable , but the c# compiler knows enough to convert to IEnumerable because IQueryable<T> : IEnumerable<T> . but In F# the compiler

What can I do to pass a list from C# to F#?

南楼画角 提交于 2019-12-07 01:19:46
问题 I know that the f# list is not the same at the c# List. What do I need to do to be able to pass a list of ints from a c# application to an f# library? I'd like to be able to use pattern matching on the data once it's in the f# code. 回答1: You can use Seq.toList : IEnumerable<'a> -> list<'a> to convert any IEnumerable<'a> seq to an F# list. Note that F# lists are immutable; if you want to work with the mutable list, you don't need to do anything special, but you won't be able to use pattern

When does an F# type need to be initialised using new?

家住魔仙堡 提交于 2019-12-07 00:28:39
问题 Given a class such as: type MyClass() = member this.Greet(x) = printfn "Hello %s" x is it appropriate to initialize instances using let x = new MyClass() or without the new ? Also, when is the use of a new constructor more useful than a do binding with parameters supplied to the type definition? 回答1: My pattern in F# for using new is to only do so when the type implements IDisposable . The compiler special cases this use and emits a warning if new is omitted. So in your case I would not use

Weird accessibility scopes when F# record's fields are declared private

自闭症网瘾萝莉.ら 提交于 2019-12-07 00:28:31
问题 I just noticed a rather counter intuitive behaviour when the field part of an F# record is declared private. (This is related to Is it possible to make a field of a record private? or to make a member of record private?) In this example... type MyRec = private // Fields declared private, or at least I thought so. { a : int b : int } member x.A = x.a member private x.Both = x.a + x.b static member CreateMyRec(a, b) = { a = a; b = b } let foo = MyRec.CreateMyRec(1,2) let bar = foo.a // No error

a timeit function for F#

自作多情 提交于 2019-12-07 00:12:08
问题 I am trying to write something like let timeit (x:'a->'b) = let start = System.DateTime.Now x let duration = System.DateTime.Now - start printfn "time usage = %A" duration.Milliseconds () it works for let matrixtest() = let x = vector[1.;2.;4.] let y = matrix[[1.;2.;4.;];[3.;4.;9.;]] printfn "%A" (y * x) () but not for let rec fib x = match x with | 0 | 1 -> 1 | n -> fib (n-1) + fib (n-2) sa F# is static typed. Any idea? Thanks. 回答1: Even in the matrix case you need to apply the function to a

Providing connection string to Linq-To-Sql data provider

让人想犯罪 __ 提交于 2019-12-06 23:47:26
问题 Is there a way to provide a connection string to Linq-To-Sql data provider in F# from App.Config file. I have tried the following just for testing: let mutable connString = @"Data Source=PCSQLEXPRESS;Initial Catalog=NortwindDB;Integrated Security=True" type SqlConnection = SqlDataConnection<ConnectionString = connString> but I get an error message "This is not a constant expression or valid custom attribute value" Thanks 回答1: The type provider itself requires a hard-coded connection string

Learning F#: What books using other programming languages can be translated to F# to learn functional concepts?

此生再无相见时 提交于 2019-12-06 23:39:52
问题 I am now several months into learning F# with the greatest asset for learning F# being translating the OCaml code in "Handbook of Practical Logic and Automated Reasoning" (WorldCat) by John Harrison, into F#. With this being such an effective method of learning, I plan to translate the code in more books to F#, but books primarily focused on functional concepts or real world applications typically known for being written with a functional language such as AI, compilers, Theorem Provers and

Unit-safe square roots

可紊 提交于 2019-12-06 21:44:47
问题 I just wondered how it is possible to write a user-defined square root function (sqrt) in a way that it interacts properly with F#'s unit system. What it should be like: let sqrt (x : float<'u ^ 2>) = let x' = x / 1.0<'u ^ 2> // Delete unit (x ** 0.5) * 1.0<'u> // Reassign unit But this is disallowed due to nonzero constants not being allowed to have generic units . Is there a way to write this function? With the builtin sqrt it works fine, so what magic does it perform? 回答1: Allowing nonzero

Why Paket installs way more packages than Nuget?

旧城冷巷雨未停 提交于 2019-12-06 21:26:50
问题 Why Paket installs way more packages than Nuget by default? Is it normal behaviour or am I doing something wrong? I followed Getting Started guide (but with the help of paket.powershell which I installed by choco install paket.powershell): I made a new WPF project Initialized Paket with Paket-Init command Added nuget reactiveui to paket.dependencies file Executed Paket-Install command to download packages As a result I have the following in my packages folder: reactiveui reactiveui-core Rx

Can you encapsulate multi case discriminated unions?

孤者浪人 提交于 2019-12-06 20:47:30
I see that you can enforce constructor usage of single-case discriminated unions, can you do the same with multi-case? for example type MemberId = | MemberId of int | MemberGuid of Guid I'm currently trying in the fsi like this val create : int -> T option val create : Guid -> T option but I'm guessing like C#, F# won't allow you to overload based on return type for the unwrap: val value : T -> string Edit --------------- MemberId.fsi = module MemberId open System type _T val createId : int -> _T option val createGuid : Guid -> _T option val value : _T -> 'a MemberId.fs = module MemberId open