Created
September 16, 2018 05:30
-
-
Save jigargandhi/ef5c1f7d444c5257503ef3010e8aaf99 to your computer and use it in GitHub Desktop.
Decorator pattern 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
let logger (f: 'a->'b) (valA:'a) = | |
printfn "Pre" | |
let result = f valA | |
printfn "Post" | |
result | |
let add5To x = | |
printfn "calling add5To" | |
x+5 | |
let decoratedAdd = logger add5To | |
decoratedAdd 12 | |
|>printfn "%d" | |
let add x y = | |
printfn "calling add" | |
x + y | |
let decoratedAdder = logger add | |
decoratedAdder 1 4 | |
|>printfn "%d" | |
let twoParamedLogger (f:'a->'b->'c) (a:'a) (b:'b) = | |
printfn "Pre" | |
let result = f a b | |
printfn "Post" | |
result | |
let decoratedAdd2 = twoParamedLogger add | |
decoratedAdd2 1 4 | |
|>printfn "%d" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment