Created
May 24, 2016 01:36
-
-
Save bytor99999/72d85585cd6d98096d9c61365d771bc9 to your computer and use it in GitHub Desktop.
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
// Closure that will be curried inside curryExample() | |
Closure nextFunction = {name, age, document -> | |
//do work here after I got back the document from Mongo | |
println("Hello $name I am $age here is the Document I got from Mongo $document") | |
} | |
void curryExample() { | |
def name = "John" | |
def age = 12 | |
Closure callBackFromMongo = nextFunction.curry(name, age) | |
callMongo(documentId, callBackFromMongo) | |
} | |
void whatItWouldHaveLookedLikeWithCallBack() { | |
def name = "John" | |
def age = 12 | |
callMongo(documentId, {document -> | |
//do work here after I got back the document from Mongo | |
println("Hello $name I am $age Document $document") | |
}) | |
} | |
// This is a simple example, but when you nest further it gets clearer. Basically each method/closure is their own method | |
// declaration like a class with methods, that you call, instead of nesting callbacks. The curried Closure is still saved with | |
// the values of name and age, even outside of the name and age scope within the main methods. MongoDB knows nothing about extra | |
// values and doesn't need to know, but its callback it is calling does and now has it via currying. | |
// It is very powerful when you see it in a real app. The biggest problem I found was teaching people what currying does. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment