f#

Wildcard for type when matching discriminated unions

随声附和 提交于 2019-12-10 20:03:58
问题 In the following real world example I do a match: type Style = Nice | Cool | Ugly type Color = Blue | Yellow | Orange | Grey | Cyan type ClothingProperties = Style * Color type Clothes = | Jeans of ClothingProperties | Pullover of ClothingProperties | Shirt of ClothingProperties type Person = | Person of string * Clothes let team = [Person("Jan", Jeans (Cool, Blue)); Person("Pete", Shirt (Nice, Cyan)); Person("Harry", Pullover (Ugly, Grey))] let matchPerson person= match person with | Person

How can I consolidate two function calls into one?

青春壹個敷衍的年華 提交于 2019-12-10 20:02:00
问题 I would like to consolidate the following lines: let result1 = add (numbers, ",") let result2 = add (numbers, "\n") into something like this: let resultX = add (numbers, ",") |> add (numbers, "\n") Can I compose functions like this? NOTE: I am learning F# and apologize if this question seems silly. The code is below: module Calculator open FsUnit open NUnit.Framework open System let add (numbers:string) = let add (numbers:string) (delimiter:string) = if (numbers.Contains(delimiter)) then

Communication between processes using WCF and F#

孤街浪徒 提交于 2019-12-10 19:59:22
问题 I would like to make a simple one to one communication between two long time running F# processes using F#. They will exchange information regularly every ca. 5 seconds. Until now, I have reached this point: #r "System.ServiceModel" #r "System.Runtime.Serialization" //#r @"d:\DLL\Protobuf\protobuf-net.dll" #time "on" open System.ServiceModel [<ServiceContract>] type IService = [<OperationContract>] // [<ProtoBuf.ServiceModel.ProtoBehavior>] abstract Test: float [] [] [] -> string type Service

WebSharper JQuery Mobile Page Hidden

不想你离开。 提交于 2019-12-10 19:38:51
问题 I'm trying to put together a JQuery Mobile site using the F# WebSharper Framework. In WebSharper parlance I have created a Pagelet using JQueryMObile controls which is being served by a Sitelet. Everything compiles and runs, the problem is in the html that is generated. The page I have declared (simplePage) clearly exists in the markup and is marked with the JQueryMobile css class ui-active to make it visible. However, it is surrounded by a div that is also a page but is not marked with the

Retry Computation expression or other construct in F#

会有一股神秘感。 提交于 2019-12-10 19:36:06
问题 I want to be able to write a computation expression in F# that will be able to retry an operation if it throws an exception. Right now my code looks like: let x = retry (fun() -> GetResourceX()) let y = retry (fun() -> GetResourceY()) let z = retry (fun() -> DoThis(x, y)) etc. (this is obviously an astract representation of the actual code) I need to be able to retry each of the functions a set number of times, which I have defined elswhere. I was thinking a computation expression could help

F# inferred types in If/Then

非 Y 不嫁゛ 提交于 2019-12-10 19:35:57
问题 If I have the following function: let myFunc x y = if y = 0 then 1 x I get the error: Program.fs(58,17): error FS0001: This expression was expected to have type unit but here has type int Why does the compiler expect 'unit' instead of int ? 回答1: It might worth adding that this is not just a property of if . F# is an expression-based language meaning that pretty much every piece of code (aside from type declarations and a few exceptions) is an expression that evaluates to some result. In fact,

How to reference 'generated' types created by a TypeProvider

隐身守侯 提交于 2019-12-10 19:33:08
问题 I am trying to implement a type provider for Excel files that creates generated types. My goal is to be able to reference these types from C#. If I create the type provider using erased types, I can reference it from F# using this syntax: type DataTypesTest = ExcelFile<"tests\ExcelProvider.Tests\DataTypes.xlsx"> If I mark my types as generated. The above syntax produces this error: A direct reference to the generated type 'ExcelFile' is not permitted. Instead, use a type definition, e.g.

What reasoning lead to `Sequence expression containing recursive definition is compiled incorrectly`

一曲冷凌霜 提交于 2019-12-10 19:23:26
问题 The question Stack overflow despite tail call position but only in 64-bit lead to uncovering a bug in the F# compiler. After reading the answer I am curious as to the reasoning that lead to finding the bug as I would like to better improve my skills at resolving problems and understanding TCO. 回答1: My reasoning was something like this: Talking about "tail calls" when looking at a computation expression can be misleading - in general there really isn't such a thing (see e.g. this other answer

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

Compiling f# for xbox360

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 19:11:53
问题 I've been trying to compile some libraries I've written in f# to work in a game I'm writing for the xbox360, except I keep getting an error saying "The module/namespace 'System.Net.WebRequest' from compilation unit 'System' did not contain the namespace, module or type 'WebRequest' . The command I've been using to compile it is "C:\Program Files (x86)\Microsoft F#\v4.0\fsc.exe" -o:obj\Release\XBox360.dll --standalone --noframework --define:TRACE --optimize+ --tailcalls+ -r:"C:\Program Files