Closure
A closure is a function that references variables bound in its lexical
environment.
Depending on the language, it ...
Anonymous Classes in Java
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.addWindowListe...
Implementation of Closures
One object per closure.
One object representing shared state.
Invisible accessors.
Michal P´ıˇs...
Coroutine
A coroutine is a subroutine generalization with multiple entry points
for suspending and resuming execution at c...
Example
class Set {
int[] contents;
...
class SetIterator {
int next() {
for (int i = 0; i < contents.length; i++)
yield c...
Continuation
When a program is written using continuation-passing style, its
functions never return.
Instead, they invoke ...
Continuation
When a program is written using continuation-passing style, its
functions never return.
Instead, they invoke ...
Example
data Regexp = Character Char | Concat Regexp Regexp
m :: Regexp -> Maybe String ->
(Maybe String -> Maybe String) ...
Example (II)
data Regexp = Character Char | Concat Regexp Regexp |
Altern Regexp Regexp
Michal P´ıˇse (CTU in Prague) Obje...
Example (II)
data Regexp = Character Char | Concat Regexp Regexp |
Altern Regexp Regexp
m (Altern r1 r2) str cont
m r1 str...
See
Hayo Thielecke. Continuations, functions and jumps. SIGACT News
30, 2 (June 1999). 33–42.
http://doi.acm.org/10.1145/5...
Flow Control
Upcoming SlideShare
Loading in …5
×

Flow Control

783 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
783
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
46
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Flow Control

  1. 1. Closure A closure is a function that references variables bound in its lexical environment. Depending on the language, it may or may not be able to change their values. A variable may therefore exist even outside of its scope. Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 2 / 10
  2. 2. Anonymous Classes in Java public static void main(String[] args) { final JFrame frame = new JFrame(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { frame.dispose(); } }); frame.setVisible(true); } Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 3 / 10
  3. 3. Implementation of Closures One object per closure. One object representing shared state. Invisible accessors. Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 4 / 10
  4. 4. Coroutine A coroutine is a subroutine generalization with multiple entry points for suspending and resuming execution at certain locations. Invocation of a coroutine is not stateless—context and environment are withheld. Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 5 / 10
  5. 5. Example class Set { int[] contents; ... class SetIterator { int next() { for (int i = 0; i < contents.length; i++) yield contents[i]; throw new RuntimeException(); } ... } } Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 6 / 10
  6. 6. Continuation When a program is written using continuation-passing style, its functions never return. Instead, they invoke functions that represent the rest of the computation (continuations). Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 7 / 10
  7. 7. Continuation When a program is written using continuation-passing style, its functions never return. Instead, they invoke functions that represent the rest of the computation (continuations). It can be viewed as a goto statement with parameters. Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 7 / 10
  8. 8. Example data Regexp = Character Char | Concat Regexp Regexp m :: Regexp -> Maybe String -> (Maybe String -> Maybe String) -> Maybe String m regex Nothing cont = cont Nothing m (Character c) (Just "") cont = cont Nothing m (Character c) (Just (first:rest)) cont | c == first = cont (Just rest) | otherwise = cont Nothing m (Concat r1 r2) str cont = m r1 str (param -> m r2 param cont) match :: Regexp -> String -> Bool match regexp str = (m regexp (Just str) id) == Just "" Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 8 / 10
  9. 9. Example (II) data Regexp = Character Char | Concat Regexp Regexp | Altern Regexp Regexp Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 9 / 10
  10. 10. Example (II) data Regexp = Character Char | Concat Regexp Regexp | Altern Regexp Regexp m (Altern r1 r2) str cont m r1 str (param -> if cont param /= Just "" then m r2 str cont else cont param) Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 9 / 10
  11. 11. See Hayo Thielecke. Continuations, functions and jumps. SIGACT News 30, 2 (June 1999). 33–42. http://doi.acm.org/10.1145/568547.568561 Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 10 / 10

×