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
4
votes
1 answer

Can I create nested Computation Expressions for Builder Like DSLs?

This is what I'd like to do: type DirectionBuilder() = member self.Yield(()) = [] [] member self.Left (acc, degree) = None [] member self.Right (acc, degree) = None …
robkuz
  • 9,488
  • 5
  • 29
  • 50
4
votes
1 answer

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

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…
Joel Mueller
  • 28,324
  • 9
  • 63
  • 88
4
votes
3 answers

F# computation expression for nested Boolean tests?

I think I've got enough understanding of F# monads (workflows) that I see a few places in my code where implementing them makes sense. For example, I've got a function with multiple nested if/thens, i.e. the function should continue only so long as…
MiloDC
  • 2,373
  • 1
  • 16
  • 25
3
votes
1 answer

Computational Expression using Zero

When using a computational expression, the first definition works but the second does not for Zero. What is the difference between this: member o.Zero() = 3 and this: member o.Zero = fun() -> 3 The first evaluates to unit -> int and the second to…
Paul Nikonowicz
  • 3,883
  • 21
  • 39
3
votes
3 answers

Computation Expression doesn't execute Let

I'm using F# v 1.9.6.2, and I've defined a very simple computation expression: type MaybeBuilder() = member this.Let(x, f) = printfn "this.Let: %A" x this.Bind(Some x, f) member this.Bind(x, f) = printfn "this.Bind:…
Juliet
  • 80,494
  • 45
  • 196
  • 228
3
votes
1 answer

F# query: find rows with max value in each group

In my project I use: F# with query workflow Entity Framework Core 3.11 MS SQL I have a problem similar to this one: SQL select only rows with max value on a column, but I'm wondering how to express the SQL presented in that question using F# query…
LA.27
  • 1,888
  • 19
  • 35
3
votes
0 answers

Is there an easy way to inherit a computation expressions behaviour?

How would you build a new computation expression that inherits most behaviour from an existing one, but you might need to override some behaviour? Context: I will use CE as the abbreviation for computation expression from now on. I'm using…
retendo
  • 1,309
  • 2
  • 12
  • 18
3
votes
1 answer

Creating an 'add' computation expression

I'd like the example computation expression and values below to return 6. For some the numbers aren't yielding like I'd expect. What's the step I'm missing to get my result? Thanks! type AddBuilder() = let mutable x = 0 member _.Yield i = x…
LockeGarmin
  • 195
  • 1
  • 1
  • 10
3
votes
1 answer

Do computations in F# call Dispose() on completion?

I have a database connection with a query similar to as follows in a couple separate files in a project: SqlTypes.fs module SqlTypes open FSharp.Data.TypeProviders type DBType = SqlDataConnection let getDb() =…
LSM07
  • 787
  • 2
  • 7
  • 21
3
votes
3 answers

F# Flattening Nested Tuples in Computation Expression

I have a computation expression which I want to return a flattened tuple as the first element and an int as the second. I am trying to use method overloading to accomplish this. Right now the compiler is throwing an error saying it cannot find a…
Matthew Crews
  • 4,105
  • 7
  • 33
  • 57
3
votes
3 answers

Retry Computation expression or other construct in F#

I want to be able to write a computation expression in F# that will be able to retry an operation if it throws an exception. Right now my code looks like: let x = retry (fun() -> GetResourceX()) let y = retry (fun() -> GetResourceY()) let z = retry…
3
votes
0 answers

Parsing Computation Expressions with F# Compiler Services

I have developed an F# REST service framework which has recently gone into production usage, and one of the follow-up tools I'm working on is an automatic OpenAPI spec generator. The generator uses the FSharp.Compiler.Services library to examine…
Aaron M. Eshbach
  • 6,380
  • 12
  • 22
3
votes
1 answer

F# rewrite computation expression

I'm studying continuations because I want to make some interesting use of coroutines... anyway, I want to better understand one implementation I found. To do so I want to rewrite the implementation without using the computation expression…
3
votes
2 answers

F# async workflow / tasks combined with free monad

I'm trying to build pipeline for message handling using free monad pattern, my code looks like that: module PipeMonad = type PipeInstruction<'msgIn, 'msgOut, 'a> = | HandleAsync of 'msgIn * (Async<'msgOut> -> 'a) | SendOutAsync of 'msgOut *…
kagetoki
  • 4,339
  • 3
  • 14
  • 17
3
votes
3 answers

Avoiding the pyramid of doom with Computation Expressions?

I came across this question about the "pyramid of doom" in F#. The accepted answer there involves using Active Patterns, however my understanding is that it can also be solved using Computation Expressions. How can I remove the "pyramid of doom"…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
1 2 3
8 9