Generator pattern - background, basics and use cases around the pattern. I talk through some of the fundamentals of functional programming, continuation passing style and closures before landing up in iterators and generators. While there exits a good number of scenarios where the generators are useful, I pick a handful of them which I think are handy and unique.
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Generator patterns
1. Show and tell:
Generator patterns and new use
cases
Gireesh Punathil, IBM India
gpunathi@in.ibm.com | @gireeshpunam | https://github.com/gireeshpunathil
3. Functional Programming
• A programming language paradigm in which computation is the
evaluation of function(s)
• Functions as first class variables
• Higher order functions
7/28/2017 Eclipse summit India 2017 3
4. Continuation Passing Style
• A functional programming style in which the caller delegates both the
current as well as the continuation control flow to the callee.
Continuation retention style Continuation passing style
7/28/2017 Eclipse summit India 2017 4
5. Closure
• Binding of a function and its operating environment that is defined in
the lexical scope of an enclosing function.
7/28/2017 Eclipse summit India 2017 5
6. Closure context
• The operating environment that is defined in the lexical scope of an
enclosing function.
7/28/2017 Eclipse summit India 2017 6
7. Iterator Pattern
• An object oriented programming pattern which defines an
Enumerator interface that traverses Components in a Container
7/28/2017 Eclipse summit India 2017 7
Java Javascript
8. Generator Pattern
• An programming pattern which defines an Controller interface for
incrementally traversing a data structure / executing a code sequence
7/28/2017 Eclipse summit India 2017 8
9. Generator Pattern Use cases: #1
• Simple iterator over heterogeneous collections
7/28/2017 Eclipse summit India 2017 9
10. Generator Pattern Use cases: #2
• Implement stream sources in a clean manner
7/28/2017 Eclipse summit India 2017 10
11. Generator Pattern Use cases: #3
• Traverse deeply nested data structures, yield intermediary values
while retaining the traversal context
7/28/2017 Eclipse summit India 2017 11
12. Further reading
Functional Programming
https://en.wikipedia.org/wiki/Functional_programming
Continuation passing style
https://en.wikipedia.org/wiki/Continuation-passing_style
Closures and Closure Contexts
https://www.ibm.com/developerworks/library/wa-use-javascript-
closures-efficiently
Iterators and Generators
https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Iterators
_and_Generators
7/28/2017 Eclipse summit India 2017 12