f#

Why aren't patterns allowed in “use” bindings?

↘锁芯ラ 提交于 2019-12-11 06:29:12
问题 According to the spec, a use binding requires an identifier (unlike let ) instead of a pattern. Why is this? Here's an example of a scenario that doesn't work. type Disposable = Resource of IDisposable let f disposable = use (Resource d) = disposable //ERROR: 'use' bindings must be of the form 'use <var> = <expr>' () 回答1: I think the likely answer is that lots of patterns don't make sense. For instance, how would you expect the compiler to handle the following code? type DisposablePair =

How to zip files in FSharp?

一笑奈何 提交于 2019-12-11 06:22:45
问题 Cursory Google search didn't return anything simple enough to understand (i'm pretty new to functional programming). if I have an array of files, how can i zip each file and then create a zip of all zipped files? I have something like this so far: let zip f = f.zip //this is where I need the most direction let zipAllAttachments f = f |> Seq.map zip //do I need to create another function to create a single zip of all zips? EDIT: this is what I have so far, but I'm getting some strange behavior

chaining async rest calls in a pipeline while managing errors

谁都会走 提交于 2019-12-11 06:11:33
问题 Building on previous question based on synchronous calls, how do we approach asynchronous methodology in the following scenario. let fetch1 (result: string) : Result<string, string> = try use request = WebRequest.Create("http://bing.com") :?> HttpWebRequest use response = request.GetResponse() use reader = new StreamReader(response.GetResponseStream()) let html = reader.ReadToEnd() Ok "success" with | :? WebException as e -> Error "error with the url" let fetch2 (result: string) : Result

Int Option instead of Int in F#

我的梦境 提交于 2019-12-11 05:59:56
问题 I am having trouble with the following: let safeDiv x y = match (x,y) with | (_, Some 0) -> None | (Some xx, Some yy) -> Some (xx/yy) | _ -> None When I go to run this simple function in the interactive window of Visual Studio like so: safeDiv 4 2 I get the following error... This expression was expected to have type int option but here has type int. Could it be I'm meant to use safeDiv Some(4) Some(2) ? This doesn't work either... 回答1: Ok, this is overkill but I actually did something

Cannot get model based test working

与世无争的帅哥 提交于 2019-12-11 05:54:34
问题 As an exercise I wanted to implement a 2-3 finger tree. That should be the perfect opportunity to try out FsCheck's model-based testing. I decided to try the newer experimental version. So far I only coded one command for the test machine because I already fail at making that work—one the other hand it keeps the post short. The full code is available on GitHub. open CmdQ open Fuchu open FsCheck open FsCheck.Experimental type TestType = uint16 type ModelType = ResizeArray<TestType> type

Creating a list with multiple units of measurements of floats in F#

流过昼夜 提交于 2019-12-11 05:50:27
问题 So, I've tried to get around this in various ways, but I just can't make this work. Is there any way to make a list that contains values of varying units of measurement (all based on floats)? For example: let myList = [0.07<ms>; 0.9; 7.2<mm>;] As they are treated as different types, you cannot put them in the same list. I tried declaring the list as let myList : float<_> list = ... , and giving dimensionless numbers a unit of measurement, but I still got a typing error: expecting float<'u>

f# spacing and mergesort

…衆ロ難τιáo~ 提交于 2019-12-11 05:36:25
问题 I have this code provided by my instructor. I am supposed to fix it by finding what type f# infers from mergesort. When I try to send to interactive i get an error . I asked my proffesor what was wrong and he said that it was due to formatting errors on the class website. I have tried adding spaces removing spaces you name it but every time i get a ~vs4489.fsx(8,14): error FS0588: Block following this 'let' is unfinished. Expect an expression. on the last two methods. How can I fix this? Here

Type Provider hangs app when loading data from localhost

99封情书 提交于 2019-12-11 05:28:23
问题 I have reason to believe that the type provider that I'm using is hanging my app that's running on the Android emulator. On my main page within a Xamarin.Forms app, I have the following load operation: protected override void OnAppearing() { _viewModel.Load(); return; // Never gets reached when using Type Provider... } My viewmodel has the following load operation: public void Load() => Cars = new ObservableCollection<Car>(getCars()); My F# code is the following: open FSharp.Data // https:/

Returning different types of arrays from a lambda expression in F#

做~自己de王妃 提交于 2019-12-11 05:14:34
问题 i have a List of records type Item = { Color : string; Size : int} let itemList = [{Color="Red"; Size=1}; {Color="Green"; Size=2}; {Color="Blue"; Size=3};] I am looking to get turn my list of records into an array of values like [|"Red";"Green";"Blue"|] or [|1;2;3|] I can sorta get there like this type ItemType = | Color of string | Size of int type ItemEnum = | C | S let GetProp x y = match x with | C -> List.toArray y |> Array.map(fun x -> ItemType.Color(x.Color)) | S -> List.toArray y |>

Adding Overloaded Constructors to Implicit F# Type

我的未来我决定 提交于 2019-12-11 05:08:41
问题 I have created the following type using implicit type construction: open System type Matrix(sourceMatrix:double[,]) = let rows = sourceMatrix.GetUpperBound(0) + 1 let cols = sourceMatrix.GetUpperBound(1) + 1 let matrix = Array2D.zeroCreate<double> rows cols do for i in 0 .. rows - 1 do for j in 0 .. cols - 1 do matrix.[i,j] <- sourceMatrix.[i,j] //Properties ///The number of Rows in this Matrix. member this.Rows = rows ///The number of Columns in this Matrix. member this.Cols = cols //