Skip to content

Instantly share code, notes, and snippets.

@Gab-km
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gab-km/e33fa8b37e859e06f3e8 to your computer and use it in GitHub Desktop.
Save Gab-km/e33fa8b37e859e06f3e8 to your computer and use it in GitHub Desktop.
This is a sample implementation of Callable in F#.
// Interface
type ICallable<'a, 'b> =
abstract member callback: 'a -> 'b
// concrete class
type Callable<'a, 'b>(f: 'a -> 'b) =
interface ICallable<'a, 'b> with
member self.callback x = f x
// computation builder
type CallerBuilder() =
member self.Bind(x: ICallable<'a, 'b>, rest) = x.callback |> rest
member self.Zero() = ()
let caller = CallerBuilder()
// usage
caller {
let! f = Callable(fun x -> x + 2)
f 3 |> printfn "%d"
}
//=> 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment