Mike Harris
Mike Harris

Reputation: 869

How do you write a SelectMany from a Task in F#

I would like to write a SelectMany monadic bind from a Task in F#. How would I write the following C# code which uses language-ext in F#?

Task<int> result = from task in Task.Run<int>(() => 40) select task + 2;

Upvotes: 3

Views: 152

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

You can use the F# TaskBuilder library to get the F# computation expression (monadic syntax) for tasks. With this, you can rewrite your example as:

let result = task {
  let! t = Task.Run<int>(() => 40)
  return t + 2 }

Upvotes: 4

Related Questions