Questions tagged [computation-expression]

Computation expressions in F# is a technique for writing computations that can be combined using control flow constructs and bindings.

Computation expressions are derived from , which in turn represent programming implementation of monads, a mathematical concept in .

Computation expressions are usually implemented by a developer as a set of functions, while the F# development environment provides the means of indirect calling of these functions with a variety of keywords and other syntactic constructs (see ).

Further reading:
F# Programming/Computation Expressions on WikiBooks
Computation Expressions on MSDN
F# 2.0 Language Specification/Computation Expressions

134 questions
2
votes
3 answers

Making computation expressions using single case discriminated unions

If I have a function that attempts to divide a starting number two times. The entire workflow has to return a boolean. let divideBy bottom top = if bottom = 0 then None else Some (top/bottom) let divideByWorkflow init x y = match init…
MiP
  • 5,846
  • 3
  • 26
  • 41
2
votes
1 answer

Why does PSeq.map with computation expression seem to hang?

I'm writing a scraper using FSharp.Collections.ParallelSeq and a retry computation. I would like to retrieve HTML from multiple pages in parallel, and I would like to retry requests when they fail. For example: open System open…
JoshVarty
  • 9,066
  • 4
  • 52
  • 80
2
votes
0 answers

Checking the status of an Async computation

Say I want to frequently check the status of a long-running asynchronous operation while doing a bunch of other work. Is it possible to check whether an Async computation expression is completed (just like with Task's IsCompleted property)? In other…
nphx
  • 1,426
  • 3
  • 19
  • 30
2
votes
1 answer

Are Computation Expressions an alternative approach to Aspect-oriented Programming?

Are Computation Expressions an alternative approach to Aspect-oriented Programming? Is this F#'s solution for managing cross-cutting concerns. I viewed the following article and couldn't help but think of AOP (i.e. Aspect-oriented Programming). In…
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
2
votes
2 answers

F# "exit early" computation expression?

In attempting to learn more about how computation expressions work, I'm attempting to code a builder that skips the remainder of the expression after evaluating the then block of an if statement, whereupon the workflow itself would evaluate to true.…
MiloDC
  • 2,373
  • 1
  • 16
  • 25
2
votes
2 answers

Can I Access Parameters of a Computation Expression?

Is it possible to create methods or stand-alone functions in a computation expression that can later be used by one of the canonical methods of a computation expression? I want something like this: type FormletBuilder(ctx : HttpContext) = let…
Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
2
votes
1 answer

How to define Yield and For for custom computation operation in F#

I'm working on some DSL for my application and here's how I defined computation type and builder: // expression type type Action<'a,'b> = Action of ('a -> Async<'b>) let runAction (Action r) ctx = r ctx let returnF a = Action (fun _ -> async…
olegz
  • 1,189
  • 10
  • 20
2
votes
1 answer

Composing async computations in F#

I'm writing an asynchronous HTTP API client module/library. To make everything as DRY as possible, I'm trying to compose every HTTP API call from separate parts that make an API call, bottom-up: building a request, getting a response, reading a…
1
vote
3 answers

fsharp / dotnet and temporal database

I am looking for a way to integrate as directly as possible a temporal awareness into my classes. I deal with data that change with time quite a lot, like share prices, so this would probably need some attention, and be dealt with in one place to…
nicolas
  • 9,549
  • 3
  • 39
  • 83
1
vote
0 answers

what is the relationship between `CustomOperationAttribute` and the conventional CE Yield member?

The excellent ElementBuilder [GitHub] of Bolero inspires me to try to share/centralize certain HTML-DSL expressions for reuse. So, in almost total ignorance, I start with this: open Bolero open Bolero.Builders open Bolero.Html type ElementBuilder…
rasx
  • 5,288
  • 2
  • 45
  • 60
1
vote
2 answers

How to run the continuation of a task in the taskBuilder CE in the same thread of the code before the let! operation?

I am coding a long running operation in FSharp's taks CE as follows let longRunningTask = Task.Run(...) // Now let's do the rest of the multi-tasking program task { DO SOMETHING let! result = longRunningTask DO SOMETHING ELSE } The problem…
vincenzoml
  • 413
  • 1
  • 3
  • 10
1
vote
1 answer

Is it safe to use mutable let bindings inside task CEs or async CEs?

When you think you know, you don't know, is what went through my head earlier today. While going over someone's code I noticed something similar to: task { let mutable pleaseContinue = true let mutable state = MyState.Running while…
Abel
  • 56,041
  • 24
  • 146
  • 247
1
vote
1 answer

F# Computation Expressions de-sugaring tool

A great help in dealing with F# computation expressions would be a way to look at how they are de-sugared during compilation. Is there a way to get the de-sugared version? For example: A tool like de-sugar ce.fs A compiler flag to print the output…
Franco Tiveron
  • 2,364
  • 18
  • 34
1
vote
1 answer

F# Binding to output while carrying the state

I am trying to use a Computation Expression to build a list of actions. I need to bind to a value that gets returned from the getFood action so that I can register a later step to consume it. type Food = | Chicken | Rice type Step = |…
Matthew Crews
  • 4,105
  • 7
  • 33
  • 57
1
vote
1 answer

return position in an async block, in F#

I have the following code: let private SendOk (payload: 'a) : WebPart = payload |> Json.serialize |> OK >=> Writers.setMimeType "application/json" let getBTCBalance (addresses: string list) : Async = async { try …
Thomas
  • 10,933
  • 14
  • 65
  • 136
1 2 3
8 9