Last active
August 29, 2015 14:01
-
-
Save Gab-km/e33fa8b37e859e06f3e8 to your computer and use it in GitHub Desktop.
This is a sample implementation of Callable in F#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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