f#

Pattern match guard with DateTime.TryParseExact?

百般思念 提交于 2019-12-13 00:41:20
问题 How to guard with DateTime.TryParseExact (and get the parsed value if possible)? The following code doesn't work. [<EntryPoint>] let main args = let argList = args |> List.ofSeq match argList with | "aaa" :: [] -> aaa.main "aaa" | "bbb" :: [] -> bbb.main "bbb" | "ccc" :: yyyymm :: [] when DateTime.TryParseExact (yyyymm, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None)-> ccc.main "ccc" yyyymm 回答1: You can use a mutable : let mutable dt = Unchecked.defaultof<_> match argList with |

Why does F# infer this type?

妖精的绣舞 提交于 2019-12-13 00:20:16
问题 This is my code: type Cell<'t>(initial : 't) = let mutable v = initial let callbacks = new List<'t -> unit>() member x.register c = callbacks.Add(c) member x.get () = v member x.set v' = if v' <> v then v <- v' for callback in callbacks do callback v' member x.map f = let c = new Cell<_>(f v) x.register(fun v' -> c.set (f v')) ; c My problem is with the map member. F# infers the type map : ('t -> 't) -> Cell<'t> I think it should infer this more general type (just like Seq's map): map : ('t -

Using F# Indexed Properties in a Type

笑着哭i 提交于 2019-12-13 00:07:56
问题 I'm trying to convert the following C# into F#: public class Matrix { double[,] matrix; public int Cols { get { return this.matrix.GetUpperBound(1) + 1; } } public int Rows { get { return this.matrix.GetUpperBound(0) + 1; } } public Matrix(double[,] sourceMatrix) { this.matrix = new double[sourceMatrix.GetUpperBound(0) + 1, sourceMatrix.GetUpperBound(1) + 1]; for (int r = 0; r < this.Rows; r++) { for (int c = 0; c < this.Cols; c++) { this[r, c] = sourceMatrix[r, c]; } } } public double this

How can I get the compiler to insert the correct type in this example?

假装没事ソ 提交于 2019-12-12 22:33:54
问题 this is somehow follow-up on this question How can I get rid of this "Can not be generalised" error? given the following types type IModel<'value, 'target when 'target :> IModel<'value, 'target>> = abstract value: 'value with get type IModelSimple<'value, 'target> = inherit IModel<'value, IModelSimple<'value, 'target>> abstract ReInitWith: #IModel<_ , _ > -> 'target and this function returning an object expression let rec mkModelSimple<'value, 'target> vctor value = { new IModelSimple<'value,

Emulating ReadAsync

时光怂恿深爱的人放手 提交于 2019-12-12 21:16:25
问题 In .Net 4.5 there exists a method on the Stream class which read asynchronously from a stream while monitoring the cancellation token. ReadAsync : buffer:byte[] * offset:int * count:int * cancellationToken:CancellationToken -> Task<int> If another thread should first trigger the cancellation token then closes the stream, then is it guaranteed that the reading thread will be cancelled before ReadAsync throws an exception? Can I somehow achieve this guarantee using the .Net 4.0 framework and F#

How extract the int from a FsCheck.Gen.choose

耗尽温柔 提交于 2019-12-12 21:08:06
问题 I'm new on F#, and can't see how extract the int value from: let autoInc = FsCheck.Gen.choose(1,999) The compiler say the type is Gen<int> , but can't get the int from it!. I need to convert it to decimal, and both types are not compatible. 回答1: From a consumer's point of view, you can use the Gen.sample combinator which, given a generator (e.g. Gen.choose ), gives you back some example values. The signature of Gen.sample is: val sample : size:int -> n:int -> gn:Gen<'a> -> 'a list (* `size`

Print case-identifier of discriminated union

陌路散爱 提交于 2019-12-12 21:06:34
问题 I have an Option type: type Option<'a> = | Some of 'a | None override x.ToString() = sprintf "%A" x printf "%A" None // "None" printf "%A" (Some 1) // "Some 1" Supposedly, in a function I want to print Some 1 , but in another function I want to print its case-identifier, i.e. Some (discard the " 1" value). How can I do it? 回答1: If you want a generic way of doing it rather than implement a member for each of your types, you can use reflection for that: open Microsoft.FSharp.Reflection let

Why does foldBack not execute the same side-effects that fold does?

家住魔仙堡 提交于 2019-12-12 19:35:37
问题 I was working through the answers to Example of the difference between List.fold and List.foldBack trying to get my head around the difference between fold and foldBack . I understand the difference in application order now, but there's a difference in side-effects that I don't understand. I used List.fold and List.foldBack for my testing. My accumulator functions that were basically equivalent to :: , so that accumulation order would matter. The accumulator functions I used were as follows:

F# signature file error

大兔子大兔子 提交于 2019-12-12 19:27:13
问题 I was trying to use a fsi file to allow mutually recursive classes in separate files, but my fsi file did not compile. Below is a simple example which demonstrates the problem. File program.fs: module mod1 type first = |zero = 0 File File1.fs: module mod2 type second = |zero2 = 0 Compiling with --sig:signature.fsi produces: #light module mod1 type first = | zero = 0 module mod2 type second = | zero2 = 0 Which has an error on the line type second Which is Error 1 Unexpected keyword 'type' in

How to implement ISerializable in F#

半城伤御伤魂 提交于 2019-12-12 19:22:56
问题 Let's say you start off with this stub: [<Serializable>] type Bounderizer = val mutable _boundRect : Rectangle new (boundRect : Rectangle) = { _boundRect = boundRect ; } new () = { _boundRect = Rectangle(0, 0, 1, 1); } new (info:SerializationInfo, context:StreamingContext) = { // to do } interface ISerializable with member this.GetObjectData(info, context) = if info = null then raise(ArgumentNullException("info")) info.AddValue("BoundRect", this._boundRect) // TODO - add BoundRect property