SlideShare a Scribd company logo
Flow Control
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
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 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
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, 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
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
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
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 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

More Related Content

PDF
Introducing Parameter Sensitivity to Dynamic Code-Clone Analysis Methods
PDF
Intro python-object-protocol
PDF
Functional Concepts
PDF
Queue in C, Queue Real Life of Example
PPTX
Java8 and Functional Programming
ODP
Sysprog 9
PPTX
PDF
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
Introducing Parameter Sensitivity to Dynamic Code-Clone Analysis Methods
Intro python-object-protocol
Functional Concepts
Queue in C, Queue Real Life of Example
Java8 and Functional Programming
Sysprog 9
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)

What's hot (16)

ODP
Extreme JavaScript Minification and Obfuscation
PDF
SCaml compiler
PDF
Cs2303 theory of computation november december 2015
PDF
15CS664 Python Question Bank-3
PDF
A nice 64-bit error in C
PPTX
PPTX
Csc1100 lecture02 ch02-datatype_declaration
PDF
15CS664- Python Application Programming- Question bank 1
PPTX
Roslyn: el futuro de C#
ODP
Clojure presentation
PDF
Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...
PDF
Lo18
PDF
[C++ korea] effective modern c++ study item 4 - 6 신촌
PPTX
Introduction linked list
Extreme JavaScript Minification and Obfuscation
SCaml compiler
Cs2303 theory of computation november december 2015
15CS664 Python Question Bank-3
A nice 64-bit error in C
Csc1100 lecture02 ch02-datatype_declaration
15CS664- Python Application Programming- Question bank 1
Roslyn: el futuro de C#
Clojure presentation
Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...
Lo18
[C++ korea] effective modern c++ study item 4 - 6 신촌
Introduction linked list
Ad

Similar to Flow Control (20)

PPT
Compiler design Lexical analysis based on lex
PDF
JAVA SE 7
PDF
Java 7 JUG Summer Camp
PPT
Os Reindersfinal
PPT
Os Reindersfinal
PPT
1CompilerDesigningss_LexicalAnalysis.ppt
PPT
Saumya Debray The University of Arizona Tucson
PDF
Java 7 at SoftShake 2011
PPTX
JavaScript.pptx
PDF
Scala is java8.next()
PPT
Java 7 new features
PPT
Clojure concurrency
PDF
Java 7 LavaJUG
PDF
Matlab and Python: Basic Operations
PDF
FP in Java - Project Lambda and beyond
PDF
Java Version(v5 -v23) Features with sample code snippet
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
PPT
Java Performance Tuning
PDF
Functional programming ii
Compiler design Lexical analysis based on lex
JAVA SE 7
Java 7 JUG Summer Camp
Os Reindersfinal
Os Reindersfinal
1CompilerDesigningss_LexicalAnalysis.ppt
Saumya Debray The University of Arizona Tucson
Java 7 at SoftShake 2011
JavaScript.pptx
Scala is java8.next()
Java 7 new features
Clojure concurrency
Java 7 LavaJUG
Matlab and Python: Basic Operations
FP in Java - Project Lambda and beyond
Java Version(v5 -v23) Features with sample code snippet
A Brief Conceptual Introduction to Functional Java 8 and its API
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Java Performance Tuning
Functional programming ii
Ad

More from Michal Píše (10)

PDF
Prototype Languages
PDF
Reflection and Metadata
PDF
Multiple Inheritance
PDF
Encapsulation
PDF
Garbage Collection
PDF
Reclassification
PDF
Multiple Dispatch
PDF
Inheritance
PDF
Subtyping
PDF
Type Systems
Prototype Languages
Reflection and Metadata
Multiple Inheritance
Encapsulation
Garbage Collection
Reclassification
Multiple Dispatch
Inheritance
Subtyping
Type Systems

Recently uploaded (20)

PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
IGGE1 Understanding the Self1234567891011
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Empowerment Technology for Senior High School Guide
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Trump Administration's workforce development strategy
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
IGGE1 Understanding the Self1234567891011
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Final Presentation General Medicine 03-08-2024.pptx
A systematic review of self-coping strategies used by university students to ...
Weekly quiz Compilation Jan -July 25.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Cell Types and Its function , kingdom of life
Empowerment Technology for Senior High School Guide
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
Final Presentation General Medicine 03-08-2024.pptx
Hazard Identification & Risk Assessment .pdf
Trump Administration's workforce development strategy
Computing-Curriculum for Schools in Ghana
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Unit 4 Skeletal System.ppt.pptxopresentatiom
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx

Flow Control

  • 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