f#

F# observable filter with side effect

会有一股神秘感。 提交于 2019-12-08 06:40:28
问题 I have a number of events that are merged into one observable that executes some commands. If a command succeeded some result takes place. In addition, the command should be logged. In terms of code, this looks like let mevts = modifyingevents |> Observable.filter exec_action |> Observable.add (fun action -> self.OutlineEdited <- true) where the function exec_action results in some side effect such as editing a treeview. If this succeeded then the property OutlineEdited is set to true . I was

F# Why can't I use the :? operator in F# interactive?

泪湿孤枕 提交于 2019-12-08 05:55:19
问题 I am trying to check if a variable is of a certain type like so: let s = "abc" let isString = s :? string but in F# interactive, I get the following error: error FS0016: The type 'string' does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion. Why is this happening? I expect isString to be a bool. 回答1: Because you are trying with a sealed type. Try instead this: let s = box "abc" let isString = s :? string There is no point in doing this coercion

FParsec and a delimiter based syntax

风流意气都作罢 提交于 2019-12-08 05:44:05
问题 I'm trying to use fparsec to parse a simple todo list language (the data from TaskPaper actually) as a simple parser combinator example. But I've run into a bug I can't seem to puzzle out. I'm new to parser combinators and FParsec seems to rely on me knowing Parsec, but I'm finding the parsec documentation inscrutable. The rules of the task paper language are simple (I'm ignoring @tags for now) Projects end with a ':' Tasks are start with '-' Any other line of text is a plain text note on

F# Why can't I use the :? operator in F# interactive?

南楼画角 提交于 2019-12-08 05:29:31
I am trying to check if a variable is of a certain type like so: let s = "abc" let isString = s :? string but in F# interactive, I get the following error: error FS0016: The type 'string' does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion. Why is this happening? I expect isString to be a bool. Because you are trying with a sealed type. Try instead this: let s = box "abc" let isString = s :? string There is no point in doing this coercion tests with sealed types since they can't have any subtype and that's what the error message is telling you.

The non-generic type Type does not expect type arguments

瘦欲@ 提交于 2019-12-08 05:22:28
问题 I am creating a simple test type provider. I want to provide a string, and return a type with the type name equal to that provided string. But the result doesn't work, saying that BasicProvider is a non-generic type. Error: The non-generic type 'SimpleStringProvider.BasicProvider' does not expect any type arguments, but here is given 1 type argument(s) module SimpleStringProvider open ProviderImplementation open ProviderImplementation.ProvidedTypes open Microsoft.FSharp.Core.CompilerServices

F# Recursive Tree Validation

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 03:32:52
问题 This is a somewhat beginner question. I have been trying to validate the following type of FamilyTree. I can't find a simple way to do this. All help would be appreciated. type BirthYear = int;; type Tree = Person of BirthYear * Children and Children = Tree list;; I want to validate a given family tree such that every Person is older than their Children and furthermore check if the list of Children is sorted in order of their age (eldest first). Preferably done with a function that return a

Extending the Indexer for an existing class

半城伤御伤魂 提交于 2019-12-08 02:20:31
问题 Suppose I have type A with indexer implemented, e.g. type A is a library. Now I want to extend the indexer of it, e.g. here I want to add float number into the indexer. I worked out the following code: type A(a:int array) = member this.Item with get(x) = a.[x] and set(x) value = a.[x] <- value type A with member m.Item with get(x:float) = m.[x |> int] and set(x:float) v = m.[x |> int] <- v But it seems not working: let a = A([| 1;2;3 |]) a.[1] a.[1] <- 10 a.[1.0] For the last line, I get:

Convert Railway oriented failure track to Rx friendly errors

非 Y 不嫁゛ 提交于 2019-12-08 01:52:39
问题 I'm using a library that takes the results as two-track values (success and failure).In the Observable.map function bodies I often get an observable from success track of a function, and I don't know how to handle them (at Observable.map body). In the other words I often get stuck in situations that the result looks something like the following (of course it's the simplest one): Rop.Result<IObservable<Rop.Result<IObservable,Messages>,Messages> On one hand, translating messages back to

Display a timer in Blazor

被刻印的时光 ゝ 提交于 2019-12-08 01:02:13
问题 I am attempting to display a countdown timer in a server-side Blazor app. My code is in both F# and C#. The code somewhat works, but the timer never stops as intended, and the timer display sporadically does not render all of the numbers. This is my first attempt at a Blazor server-side app. I am not sure if the problem is an async issue, timer issue, or rendering issue. Here's my code: F# let private setTimer countDown timeEvent = let timer = new Timer(float countDown * float 1000) let

Code Quotations: how to access variables of a lambda function internally?

江枫思渺然 提交于 2019-12-08 01:01:13
问题 I am having trouble at runtime in putting together a code quotation for a lambda function. Below is a highly simplified example to demonstrate the point. I have given the errors yielded at runtime (not compile time) underneath each attempt: open FSharp.Quotations // First Attempt let exprFun (a:int) (b:int) :Expr<int> = <@ a+b @> let q1:Expr<int->int->int> = <@ fun x y -> %(exprFun x y) @> // NB: need to pass around and access `x` & `y` within a nested quotation expression // error: The