SlideShare a Scribd company logo
1 of 12
Download to read offline
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

What's hot

Extreme JavaScript Minification and Obfuscation
Extreme JavaScript Minification and ObfuscationExtreme JavaScript Minification and Obfuscation
Extreme JavaScript Minification and ObfuscationSergey Ilinsky
 
SCaml compiler
SCaml compilerSCaml compiler
SCaml compilerJun Furuse
 
Cs2303 theory of computation november december 2015
Cs2303 theory of computation november december 2015Cs2303 theory of computation november december 2015
Cs2303 theory of computation november december 2015appasami
 
15CS664 Python Question Bank-3
15CS664 Python Question Bank-315CS664 Python Question Bank-3
15CS664 Python Question Bank-3Syed Mustafa
 
A nice 64-bit error in C
A  nice 64-bit error in CA  nice 64-bit error in C
A nice 64-bit error in CPVS-Studio
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationIIUM
 
15CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 115CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 1Syed Mustafa
 
Clojure presentation
Clojure presentationClojure presentation
Clojure presentationedpaget
 
Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...
Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...
Ruby Under The Hood - By Craig Lehmann and Robert Young - Ottawa Ruby Novembe...ottawaruby
 
[C++ korea] effective modern c++ study item 4 - 6 신촌
[C++ korea] effective modern c++ study   item 4 - 6 신촌[C++ korea] effective modern c++ study   item 4 - 6 신촌
[C++ korea] effective modern c++ study item 4 - 6 신촌Seok-joon Yun
 

What's hot (16)

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

Similar to Flow Control

Java 7 JUG Summer Camp
Java 7 JUG Summer CampJava 7 JUG Summer Camp
Java 7 JUG Summer Campjulien.ponge
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinaloscon2007
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinaloscon2007
 
Saumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona TucsonSaumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona Tucsonjeronimored
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011julien.ponge
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()daewon jeong
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new featuresShivam Goel
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrencyAlex Navis
 
Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic OperationsWai Nwe Tun
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for RSeth Falcon
 

Similar to Flow Control (20)

An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
Java 7 JUG Summer Camp
Java 7 JUG Summer CampJava 7 JUG Summer Camp
Java 7 JUG Summer Camp
 
JAVA SE 7
JAVA SE 7JAVA SE 7
JAVA SE 7
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Os Reindersfinal
Os ReindersfinalOs Reindersfinal
Os Reindersfinal
 
Saumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona TucsonSaumya Debray The University of Arizona Tucson
Saumya Debray The University of Arizona Tucson
 
Java 7 at SoftShake 2011
Java 7 at SoftShake 2011Java 7 at SoftShake 2011
Java 7 at SoftShake 2011
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Scala is java8.next()
Scala is java8.next()Scala is java8.next()
Scala is java8.next()
 
Java 7 new features
Java 7 new featuresJava 7 new features
Java 7 new features
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrency
 
Java 7 LavaJUG
Java 7 LavaJUGJava 7 LavaJUG
Java 7 LavaJUG
 
Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic Operations
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for R
 

More from Michal Píše

More from Michal Píše (10)

Prototype Languages
Prototype LanguagesPrototype Languages
Prototype Languages
 
Reflection and Metadata
Reflection and MetadataReflection and Metadata
Reflection and Metadata
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
Reclassification
ReclassificationReclassification
Reclassification
 
Multiple Dispatch
Multiple DispatchMultiple Dispatch
Multiple Dispatch
 
Inheritance
InheritanceInheritance
Inheritance
 
Subtyping
SubtypingSubtyping
Subtyping
 
Type Systems
Type SystemsType Systems
Type Systems
 

Recently uploaded

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 

Recently uploaded (20)

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 

Flow Control

  • 1.
  • 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