Upload
michal-pise
View
796
Download
0
Embed Size (px)
Citation preview
Closure
A closure is a function that references variables bound in its lexicalenvironment.
Depending on the language, it may or may not be able to changetheir 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
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
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
Coroutine
A coroutine is a subroutine generalization with multiple entry pointsfor suspending and resuming execution at certain locations.
Invocation of a coroutine is not stateless—context andenvironment are withheld.
Michal Pıse (CTU in Prague) Object Programming Lect. 8: C3 November 16, 2010 5 / 10
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
Continuation
When a program is written using continuation-passing style, itsfunctions never return.
Instead, they invoke functions that represent the rest of thecomputation (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
Continuation
When a program is written using continuation-passing style, itsfunctions never return.
Instead, they invoke functions that represent the rest of thecomputation (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
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
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
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
See
Hayo Thielecke. Continuations, functions and jumps. SIGACT News30, 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