computation-expression

How to keep the stacktrace when rethrowing an exception out of catch-context?

谁说胖子不能爱 提交于 2019-12-08 15:59:29
问题 TL;DR: how to raise a previously caught exception later on, while preserving the original exception's stacktrace. Since I think this is useful with the Result monad or computation expression, esp. since that pattern is often used for wrapping an exception without throwing it, here's a worked out example of that: type Result<'TResult, 'TError> = | Success of 'TResult | Fail of 'TError module Result = let bind f = function | Success v -> f v | Fail e -> Fail e let create v = Success v let

How do I change the Rx Builder implementation to fix the stack overflow exception?

让人想犯罪 __ 提交于 2019-12-04 17:59:31
问题 I'm trying to come up with an Rx Builder to use Reactive Extension within the F# Computation Expression syntax. How do I fix it so that it doesnt blow the stack? Like the Seq example below. And is there any plans to provide an implementation of the RxBuilder as part of the Reactive Extensions or as part of future versions of the .NET Framework ? open System open System.Linq open System.Reactive.Linq type rxBuilder() = member this.Delay f = Observable.Defer f member this.Combine (xs:

Recursive functions in computation expressions

与世无争的帅哥 提交于 2019-12-04 01:27:03
问题 Some background first. I am currently learning some stuff about monadic parser combinators. While I tried to transfer the 'chainl1' function from this paper (p. 16-17), I came up with this solution: let chainl1 p op = parser { let! x = p let rec chainl1' (acc : 'a) : Parser<'a> = let p' = parser { let! f = op let! y = p return! chainl1' (f acc y) } p' <|> succeed acc return! chainl1' x } I tested the function with some large input and got a StackOverflowException. Now I am wondering, is it

Why does this computation expression builder expect “unit” in my for loop?

和自甴很熟 提交于 2019-12-03 17:21:26
This is a follow-up question to this question . I'm trying to create a computation expression builder that accumulates a value through custom operations, and also supports standard F# language constructs at the same time. For the purposes of having a simple example to talk about, I'm using a computation expression that builds F# lists. Thanks to suggestions from kvb and Daniel I'm further along, but still having trouble with for loops. The builder: type Items<'a> = Items of 'a list type ListBuilder() = member x.Yield(vars) = Items [], vars member x.Run(l,_) = l member x.Zero() = Items [], ()

Defining new keywords in F#'s computation expression

眉间皱痕 提交于 2019-12-03 15:08:09
问题 The F# 3.0 beta contains a query {} computation expression with tons of new keywords. How can I define my own keywords in a computation builder? 回答1: In F# 3.0, you can use CustomOperationAttribute for this purpose. The new attribute is not very well-documented, the only examples I find are this great answer by @Tomas and this interesting blog post. 来源: https://stackoverflow.com/questions/9565986/defining-new-keywords-in-fs-computation-expression

How do I change the Rx Builder implementation to fix the stack overflow exception?

◇◆丶佛笑我妖孽 提交于 2019-12-03 11:50:10
I'm trying to come up with an Rx Builder to use Reactive Extension within the F# Computation Expression syntax. How do I fix it so that it doesnt blow the stack? Like the Seq example below. And is there any plans to provide an implementation of the RxBuilder as part of the Reactive Extensions or as part of future versions of the .NET Framework ? open System open System.Linq open System.Reactive.Linq type rxBuilder() = member this.Delay f = Observable.Defer f member this.Combine (xs: IObservable<_>, ys : IObservable<_>) = Observable.merge xs ys member this.Yield x = Observable.Return x member

Combine F# async and maybe computation expression

血红的双手。 提交于 2019-12-03 11:30:54
问题 Say i want to return an Option while in an async workflow: let run = async { let! x = doAsyncThing let! y = doNextAsyncThing x match y with | None -> return None | Some z -> return Some <| f z } Ideally I would use the maybe computation expression from FSharpx at the same time as async to avoid doing the match . I could make a custom builder, but is there a way to generically combine two computation expressions? It might look something like this: let run = async { let! x = doAsyncThing let! y

How do I write a computation expression builder that accumulates a value and also allows standard language constructs?

我的梦境 提交于 2019-12-03 10:14:21
问题 I have a computation expression builder that builds up a value as you go, and has many custom operations. However, it does not allow for standard F# language constructs, and I'm having a lot of trouble figuring out how to add this support. To give a stand-alone example, here's a dead-simple and fairly pointless computation expression that builds F# lists: type Items<'a> = Items of 'a list type ListBuilder() = member x.Yield(()) = Items [] [<CustomOperation("add")>] member x.Add(Items current,

(How) can I make this monadic bind tail-recursive?

余生长醉 提交于 2019-12-03 06:21:45
I have this monad called Desync - [<AutoOpen>] module DesyncModule = /// The Desync monad. Allows the user to define in a sequential style an operation that spans /// across a bounded number of events. Span is bounded because I've yet to figure out how to /// make Desync implementation tail-recursive (see note about unbounded recursion in bind). And /// frankly, I'm not sure if there is a tail-recursive implementation of it... type [<NoComparison; NoEquality>] Desync<'e, 's, 'a> = Desync of ('s -> 's * Either<'e -> Desync<'e, 's, 'a>, 'a>) /// Monadic return for the Desync monad. let internal

Extended computation expressions without for..in..do

梦想与她 提交于 2019-12-03 04:15:16
问题 What I mean by extended computation expressions is computation expressions with custom keywords defined via CustomOperation attribute. When reading about extended computation expressions, I come across very cool IL DSL by @kvb: let il = ILBuilder() // will return 42 when called // val fortyTwoFn : (unit -> int) let fortyTwoFn = il { ldc_i4 6 ldc_i4_0 ldc_i4 7 add mul ret } I wonder how the operations compose without using for..in..do construct. My gut feeling is that it starts with x.Zero