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

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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🔝
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 

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