Created
July 24, 2014 07:31
-
-
Save SamClayton/d6463346bb933d915cc7 to your computer and use it in GitHub Desktop.
Groovy closures talk examples
This file contains hidden or 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
| def foo(closure) { | |
| println closure.maximumNumberOfParameters | |
| println closure.parameterTypes | |
| } | |
| foo { e -> println e} | |
| foo { int a, double b -> println "$a, $b"} | |
| def greet = { name -> println "${toUpperCase()} $name" } | |
| greet.delegate = 'hello' | |
| greet('Sam') | |
| // compare to JS prototypes | |
| greet.delegate = 'howdy' | |
| greet('Bill') | |
| // Currying | |
| def log = { date, level, message -> | |
| println "$date $level $message" | |
| } | |
| def today = new Date() | |
| def logWarningToday = log.curry(today, 'warning') | |
| logWarningToday 'starting...' | |
| logWarningToday 'running...' | |
| logWarningToday 'stopping...' | |
| // FIX THIS | |
| /* | |
| def anotherDayLog = log.rcurry('...', '...') | |
| anotherDayLog('error', 'foobar') | |
| */ | |
| // see also ncurry for currying middle values | |
| // also had a quick example using Thread.start { } | |
| def getPizza() { | |
| println 'getting pizza' | |
| Thread.sleep(2000) | |
| 'pizza' | |
| } | |
| def getDrink() { | |
| Thread.sleep(2000) | |
| println 'return drink' | |
| } | |
| /* | |
| TimeIt.code { | |
| def result = getPizza() | |
| getDrink() | |
| println result | |
| } | |
| */ | |
| TimeIt.code { | |
| withPool { // withPool is part of Geepers (sp?) | |
| // callAsync() has no timeout... bad practice | |
| //def result = { getPizza() }.callAsync() | |
| def result = { getPizza() }.callTimeOutAsync() | |
| getDrink() | |
| println result | |
| } | |
| } | |
| class TimeIt { | |
| def static code(codeBlock) { | |
| // getTime is throwing exception (method name, look up later) | |
| //def start = System.getTime() | |
| //... | |
| } | |
| } | |
| /* | |
| def factorial(n) { | |
| if (n == 1) | |
| 1 | |
| else | |
| n * factorial(n - 1) | |
| } | |
| // Large number, should trigger stack overflow | |
| println factorial(5000) | |
| */ | |
| // ------------------------------------- | |
| // changed all refs below to factorial to factorialImpl with a BigInteger-typed second arg, then added this interface to abstract out typing for end user | |
| def factorial(n) { | |
| factorialImpl(1, n) | |
| } | |
| def factorial | |
| factorial = { fact, n -> | |
| if (n==1) | |
| fact | |
| else | |
| factorial.trampoline(fact *n, n-1) | |
| }.trampoline() // converts this to a TrampolineClosure | |
| // This breaks in Groovy Console (returns 0) (probably due to overflow to sign bit) | |
| println factorial(1,27) | |
| /* | |
| // Exmplanation of trampoline | |
| while(result == type of trampoline) { | |
| result = call trampoline | |
| } | |
| */ | |
| // Poor recursion | |
| /* | |
| def fib(n) { | |
| if(n <2) | |
| 1 | |
| else | |
| fib(n-1) + fib(n-2) | |
| }.memoize() | |
| */ | |
| def fib | |
| fib = {n -> | |
| if(n <2) | |
| 1 | |
| else | |
| fib(n-1) + fib(n-2) | |
| }.memoize() | |
| def start = System.currentTimeMillis() | |
| println fib(32) | |
| println ((System.currentTimeMillis() - start)/1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment