2. 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
3. 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
4. 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
5. 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
6. 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
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).
Michal P´ıˇse (CTU in Prague) Object Programming Lect. 8: C3
November 16, 2010 7 / 10
8. 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
9. 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
10. 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
11. 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
12. 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