SlideShare a Scribd company logo
1 of 41
Download to read offline
1/41
Closures & Functional
Programing
They are a block of code plus the
bindings to the environment they came
from (Ragusa Idiom).
“Refactoring is improving the design of code after it
has been written”
2/41
Agenda 05.06.2012
• Closure as a Code Block
• Long History
• Dynamic Languages
• Functional Programming Examples
• Closures & Refactoring
• Case Study Experiences with a Polygraph Design
• Links
3/41
Code Blocks
How many times have you written a piece of
code and thought “I wish I could reuse this
block of code”?
How many times have you refactored existing
code to remove redundancies?
How many times have you written the same
block of code more than once before
realising that an abstraction exists?
4/41
Why Closures?
Languages that support closures allow you to
define functions with very little syntax.
While this might not seem an important
point, I believe it's crucial - it's the key to
make it natural to use them frequently.
Look at Lisp, Smalltalk, or Ruby code and
you'll see closures all over the place.
UEB: demos/threads/thrddemo.exe
5/41
Closure Improves
automatisation and integration of new
methods, better testability and readability
(simplification)
SourceSource
findfind
FunctionFunction
nUnitnUnit
test
test
RefactorRefactor
changechange
PatternPattern
block
block
idiom
idiom
6/41
What‘s a closure ?
Never touch a running system ?!:
//Calls n times a value:
• Ruby
•
• def n_times(a_thing)
• return proc{ |n| a_thing * n}
• end
• c1 = n_times(23)
• puts c1.call(1) # -> 23
• puts c1.call(3) # -> 69
7/41
Closure Counter
• def counter(start)
return proc {start *= 3}
end
k = counter(1)
g = counter(10)
• puts k.call # -> 3
• puts k.call # -> 9
• puts g.call # -> 30
• puts k.call # -> 27
UEB: 8_pas_verwechselt.txt
8/41
Closure: Definition
• Closures are reusable blocks of code that
capture the environment and can be passed
around as method arguments for immediate
or deferred execution.
• Simplification: Your classes are small and
your methods too; you’ve said everything and
you’ve removed the last piece of unnecessary
code.
9/41
Closure in PHP
• def times(n):
def _f(x):
return x * n
return _f
• t3 = times(3)
• print t3 #
• print t3(7) # 21
10/41
Closure in Python
def generate_power_func(n):
def nth_power(x): //closure
return x**n
return nth_power
>>> raised_to_4 = generate_power_func(4)
debug:
id(raised_to_4) == 0x00C47561 == id(nth_power)).
>>> raised_to_4(2) 16
11/41
History
• Lisp started in 1959
• Scheme as a dialect of Lisp
• One feature of the language was function-
valued expressions, signified by lambda.
• Smalltalk with Blocks.
• Lisp used something called dynamic
scoping.
12/41
Dynamic Languages
The increase in importance of web services, however, has
made it possible to use dynamic languages (where
types are not strictly enforced at compile time) for large
scale development. Languages such as Python, Ruby,
and PHP.
Scheme was the first language to introduce closures,
allowing full lexical scoping, simplifying many types of
programming style.
for i:= 0 to SourceTable.FieldCount - 1 do
DestTable.Fields[i].Assign(SourceTable.Fields[i]);
DestTable.Post;
13/41
Closure in Math Optimization
14/41
Function Pointers
• Possible Callback or Procedure Type
• Type
TMath_func = Procedure(var x: float); //Proc Type
procedure fct1(var x: float); //Implement
begin
x:= Sin(x);
end;
• var
• fct1x:= @fct1 //Reference
• fct_table(0.1, 0.7, 0.4, fct1x); //Function as Parameter
• fct_table(0.1, 0.7, 0.4, @fct1); //alternativ direct
15/41
Function Pointer II
Are Business Objects available (Closure Extensions)?
In a simple business object (without fields in the class),
you do have at least 4 tasks to fulfil:
1. The Business-Class inherits from a Data-Provider
2. The closure query is part of the class
3. A calculation or business-rule has to be done
4. The object is independent from the GUI, GUI calls the
object
“Business objects are sometimes referred to as conceptual objects,
because they provide services which meet business requirements,
regardless of technology”.
16/41
Delegates
Method Pointers (Object Organisation)
• ppform:= TForm.Create(self);
• with ppform do begin
• caption:= 'LEDBOX, click to edit, dblclick write out pattern'+
• ' Press <Return> to run the Sentence';
• width:= (vx*psize)+ 10 + 300;
• height:= (vy*psize)+ 30;
• BorderStyle:= bsDialog;
• Position:= poScreenCenter;
• OnKeyPress:= @FormKeyPress
• OnClick:= @Label1Click;
• OnClose:= @closeForm;
• Show;
• end
UEB: 15_pas_designbycontract.txt
17/41
Closure as Parameter
• Consider the difference
• def managers(emps)
• return emps.select {|e| e.isManager}
• end
Select is a method defined on the Ruby collection class. It takes a
block of code, a closure, as an argument.
• In C or Pascal you think as a function pointer
• In Java as an anonymous inner class
• In Delphi or C# you would consider a delegate.
18/41
Function Conclusion
Step to Closure Callback
A block of code plus the bindings to the
environment they came from.
This is the formal thing that sets
closures apart from function pointers
and similar techniques.
UEB: 14_pas_primetest.txt
19/41
Closure as Object
• When a function is written enclosed in another
function, it has full access to local variables from the
enclosing function; this feature is called lexical scoping.
• Evaluating a closure expression produces a closure
object as a reference.
• The closure object can later be invoked, which results
in execution of the body, yielding the value of the
expression (if one was present) to the invoker.
20/41
Closure as Callback
• function AlertThisLater(message, timeout)
{
function fn() { alert(message); }
window.setTimeout(fn, timeout);
}
AlertThisLater("Hi, JScript", 3000);
A closure is created containing the message parameter, fn is
executed quite some time after the call to AlertThisLater has
returned, yet fn still has access to the original content of message!
21/41
Closure SEQ
22/41
Closure as Thread
groovy> Thread.start { ('A'..'Z').each {sleep 100;
println it} }
groovy> Thread.start { (1..26).each {sleep 100;
println it} }
>>> A
>>> 1
>>> B
>>> 2
23/41
Lambda Expression
• There are three main parts of a function.
1. The parameter list
2. The return type
3. The method body
• A Lambda expression is just a shorthand
expression of these three elements.
// C#
• string s => Int.Parse(s)
24/41
Closure Syntax
We should keep 3 things in mind:
The ability to bind to local variables is part
of that, but I think the biggest reason is
that the notation to use them is simple
and clear.
Java's anonymous inner classes can
access locals - but only if they are final.
25/41
Syntax Schema
>>> def outer(x):
... def inner(y):
... return x+y
... return inner
...
• >>> customInner=outer(2)
• >>> customInner(3)
• result: 5
26/41
Let‘s practice
• 1
• 11
• 21
• 1211
• 111221
• 312211
• ???
Try to find the next pattern, look for a rule or
logic behind !
27/41
Before C.
function runString(Vshow: string): string;
var i: byte;
Rword, tmpStr: string;
cntr, nCount: integer;
begin
cntr:=1; nCount:=0;
Rword:=''; //initialize
tmpStr:=Vshow; // input last result
for i:= 1 to length(tmpStr) do begin
if i= length(tmpstr) then begin
if (tmpStr[i-1]=tmpStr[i]) then cntr:= cntr +1;
if cntr = 1 then nCount:= cntr
Rword:= Rword + intToStr(ncount) + tmpStr[i]
end else
if (tmpStr[i]=tmpStr[i+1]) then begin
cntr:= cntr +1;
nCount:= cntr;
end else begin
if cntr = 1 then cntr:=1 else cntr:=1; //reinit counter!
Rword:= Rword + intToStr(ncount) + tmpStr[i] //+ last char(tmpStr)
end;
end; // end for loop
result:=Rword;
end;
UEB: 9_pas_umlrunner.txt
28/41
After C.
function charCounter(instr: string): string;
var i, cntr: integer;
Rword: string;
begin
cntr:= 1;
Rword:=' ';
for i:= 1 to length(instr) do begin
//last number in line
if i= length(instr) then
concatChars()
else
if (instr[i]=instr[i+1]) then cntr:= cntr +1
else begin
concatChars()
//reinit counter!
cntr:= 1;
end;
end; //for
result:= Rword;
end; UEB: 009_pas_umlrunner_solution_2step.txt
29/41
Top 7 to avoid with Closures
1. Duplicated or Dead Code
2. Long Method
3. Large Class (Too many responsibilities)
4. Long Parameter List (Object or closure is missing)
5. Shotgun Surgery (Little changes distributed over
too many objects or procedures patterns
missed)
6. Data Clumps (Data always together (x,y -> point))
7. Middle Man (Class with too many delegating
methods)
30/41
Closure Advantage – We
can avoid:
Bad Naming (no naming convention)
Duplicated Code (side effects)
Long Methods (to much code)
Temporary Fields (confusion)
Long Parameter List (Object is missing)
Data Classes (no methods)
• Large Class
• Class with too many delegating methods
• Coupled classes UEB: 33_pas_cipher_file_1.txt
31/41
Refactoring Closure
Delete a class with referenceSafe DeleteModel
Getter- und Setter einbauenEncapsulate FieldsClass
Ersetze vererbte Methoden durch Delegation
in innere Klasse
Replace Inheritance with
Delegation
Component
Erzeuge Referenzen auf Klasse mit
Referenz auf implementierte Schnittstelle
Use InterfaceInterface
Aus Methoden ein Interface erzeugenExtract InterfaceInterface
Extract a Codepassage or define a ClosureExtract MethodClass
Ersetzen eines Ausdrucks durch einen
Methodenparameter
Introduce ParameterClass
Aus Methoden, Eigenschaften eine
Oberklasse erzeugen und verwenden
Extract SuperclassClass
Verschieben eines PackagesMove PackagePackage
Umbenennen eines PackagesRename PackagePackage
DescriptionRefactoring FunctionUnit
32/41
Case Study: The Polygraph
• Design Case Study Polyp
An easy one states that only one statement should exists for every source
line, like next and put a Var lifetime as short as possible.
Can you read (Yes-No)?
if YES then OK
if NO then you are a Liar!
var Rec: TMyRec;
begin ..
with Rec do begin
.. title:= ‘Hello Implikation';
end;
end;
33/41
Polyp Design
Dependency Inversion
34/41
Polyp Optimizations
Some tips and hints to optimize your code:
1. one statement and short var
2. assert call, IfThen()
3. use closure snippets
4. naming convention
5. $IF-directive
6. Const instead of var parameters
7. extract code from idioms
8. comment your closure code
35/41
Polyp Packages
36/41
Polyp - Demeter konkret
1. [M1] an Objekt O selbst
Bsp.: self.initChart(vdata);
2. [M2] an Objekte, die als Parameter in der Nachricht m
vorkommen
Bsp.: O.acceptmemberVisitor(visitor)
visitor.visitElementChartData;
3. [M3] an Objekte, die O als Reaktion auf m erstellt
Bsp.: visitor:= TChartVisitor.create(cData, madata);
4. [M4] an Objekte, auf die O direkt mit einem Member
zugreifen kann
Bsp.: O.Ctnr:= visitor.TotalStatistic
37/41
Polyp Demeter Test as SEQ
UEB: 56_pas_demeter.txt
38/41
Polyp – GUI Optimization
function digitButton (digit)
return Button { label = digit,
action = function ()
add_to_display(digit)
end
}
end
• Each button has a callback function to be called when the user
presses the button; Closures are useful for callback functions, too.
39/41
Final - $Closure Test
40/41
Closure Links:
• http://gafter.blogspot.com/2007/01/definition-of-
closures.html
• Michael Bolin, “Closure: The Definitive Guide", O'Reilly
Media; Auflage: 1 (20. Oktober 2010).
• http://www.javac.info/
• http://sourceforge.net/projects/maxbox/
• Refactoring Martin Fowler (1999, Addison-Wesley)
• http://www.softwareschule.ch/download/closures_report.pdf
• http://www.refactoring.com
41/41
This is not The End:
Q&A?
max@kleiner.com
softwareschule.ch

More Related Content

What's hot

Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc pptpssraikar
 
Introduction of flex
Introduction of flexIntroduction of flex
Introduction of flexvip_du
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism Intro C# Book
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asmaAbdullahJana
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
 
DBMask: Fine-Grained Access Control on Encrypted Relational Databases
DBMask: Fine-Grained Access Control on Encrypted Relational DatabasesDBMask: Fine-Grained Access Control on Encrypted Relational Databases
DBMask: Fine-Grained Access Control on Encrypted Relational DatabasesMateus S. H. Cruz
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
Defect prevention software
Defect prevention softwareDefect prevention software
Defect prevention softwareZarko Acimovic
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab MashaelQ
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
 
Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...
Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...
Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...Mateus S. H. Cruz
 
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesEelco Visser
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 

What's hot (20)

Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
Introduction of flex
Introduction of flexIntroduction of flex
Introduction of flex
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
10. sub program
10. sub program10. sub program
10. sub program
 
DBMask: Fine-Grained Access Control on Encrypted Relational Databases
DBMask: Fine-Grained Access Control on Encrypted Relational DatabasesDBMask: Fine-Grained Access Control on Encrypted Relational Databases
DBMask: Fine-Grained Access Control on Encrypted Relational Databases
 
Lexical analysis-using-lex
Lexical analysis-using-lexLexical analysis-using-lex
Lexical analysis-using-lex
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Ch4c
Ch4cCh4c
Ch4c
 
Defect prevention software
Defect prevention softwareDefect prevention software
Defect prevention software
 
Abstraction
AbstractionAbstraction
Abstraction
 
7 compiler lab
7 compiler lab 7 compiler lab
7 compiler lab
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
 
Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...
Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...
Realizing Fine-Grained and Flexible Access Control to Outsourced Data with At...
 
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor Services
 
C#unit4
C#unit4C#unit4
C#unit4
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 

Similar to Closures & Functional Programming in Depth

Similar to Closures & Functional Programming in Depth (20)

A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16
 
maXbox Starter 31 Closures
maXbox Starter 31 ClosuresmaXbox Starter 31 Closures
maXbox Starter 31 Closures
 
Python_intro.ppt
Python_intro.pptPython_intro.ppt
Python_intro.ppt
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
Functional Smalltalk
Functional SmalltalkFunctional Smalltalk
Functional Smalltalk
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
Plc part 3
Plc  part 3Plc  part 3
Plc part 3
 
Oop(object oriented programming)
Oop(object oriented programming)Oop(object oriented programming)
Oop(object oriented programming)
 
C++primer
C++primerC++primer
C++primer
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Lambdas & Streams
Lambdas & StreamsLambdas & Streams
Lambdas & Streams
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
 
CLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptxCLEAN CODING AND DEVOPS Final.pptx
CLEAN CODING AND DEVOPS Final.pptx
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Intro to Multitasking
Intro to MultitaskingIntro to Multitasking
Intro to Multitasking
 

More from Max Kleiner

EKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfEKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfMax Kleiner
 
EKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfEKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfMax Kleiner
 
maXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementmaXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementMax Kleiner
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Max Kleiner
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4Max Kleiner
 
maXbox Starter87
maXbox Starter87maXbox Starter87
maXbox Starter87Max Kleiner
 
maXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapmaXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapMax Kleiner
 
maXbox starter75 object detection
maXbox starter75 object detectionmaXbox starter75 object detection
maXbox starter75 object detectionMax Kleiner
 
BASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationBASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationMax Kleiner
 
EKON 24 ML_community_edition
EKON 24 ML_community_editionEKON 24 ML_community_edition
EKON 24 ML_community_editionMax Kleiner
 
maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingMax Kleiner
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklistMax Kleiner
 
EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP Max Kleiner
 
NoGUI maXbox Starter70
NoGUI maXbox Starter70NoGUI maXbox Starter70
NoGUI maXbox Starter70Max Kleiner
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIIMax Kleiner
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VIMax Kleiner
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning VMax Kleiner
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3Max Kleiner
 
EKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsEKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsMax Kleiner
 
Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Max Kleiner
 

More from Max Kleiner (20)

EKON26_VCL4Python.pdf
EKON26_VCL4Python.pdfEKON26_VCL4Python.pdf
EKON26_VCL4Python.pdf
 
EKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdfEKON26_Open_API_Develop2Cloud.pdf
EKON26_Open_API_Develop2Cloud.pdf
 
maXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_ImplementmaXbox_Starter91_SyntheticData_Implement
maXbox_Starter91_SyntheticData_Implement
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4
 
maXbox Starter87
maXbox Starter87maXbox Starter87
maXbox Starter87
 
maXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmapmaXbox Starter78 PortablePixmap
maXbox Starter78 PortablePixmap
 
maXbox starter75 object detection
maXbox starter75 object detectionmaXbox starter75 object detection
maXbox starter75 object detection
 
BASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data VisualisationBASTA 2020 VS Code Data Visualisation
BASTA 2020 VS Code Data Visualisation
 
EKON 24 ML_community_edition
EKON 24 ML_community_editionEKON 24 ML_community_edition
EKON 24 ML_community_edition
 
maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage coding
 
EKON 23 Code_review_checklist
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
 
EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP EKON 12 Running OpenLDAP
EKON 12 Running OpenLDAP
 
NoGUI maXbox Starter70
NoGUI maXbox Starter70NoGUI maXbox Starter70
NoGUI maXbox Starter70
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VI
 
maXbox starter67 machine learning V
maXbox starter67 machine learning VmaXbox starter67 machine learning V
maXbox starter67 machine learning V
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
 
EKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_DiagramsEKON22_Overview_Machinelearning_Diagrams
EKON22_Overview_Machinelearning_Diagrams
 
Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2Ekon22 tensorflow machinelearning2
Ekon22 tensorflow machinelearning2
 

Recently uploaded

Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationBoston Institute of Analytics
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknowmakika9823
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 

Recently uploaded (20)

Predicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project PresentationPredicting Employee Churn: A Data-Driven Approach Project Presentation
Predicting Employee Churn: A Data-Driven Approach Project Presentation
 
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service LucknowAminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
Aminabad Call Girl Agent 9548273370 , Call Girls Service Lucknow
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 

Closures & Functional Programming in Depth

  • 1. 1/41 Closures & Functional Programing They are a block of code plus the bindings to the environment they came from (Ragusa Idiom). “Refactoring is improving the design of code after it has been written”
  • 2. 2/41 Agenda 05.06.2012 • Closure as a Code Block • Long History • Dynamic Languages • Functional Programming Examples • Closures & Refactoring • Case Study Experiences with a Polygraph Design • Links
  • 3. 3/41 Code Blocks How many times have you written a piece of code and thought “I wish I could reuse this block of code”? How many times have you refactored existing code to remove redundancies? How many times have you written the same block of code more than once before realising that an abstraction exists?
  • 4. 4/41 Why Closures? Languages that support closures allow you to define functions with very little syntax. While this might not seem an important point, I believe it's crucial - it's the key to make it natural to use them frequently. Look at Lisp, Smalltalk, or Ruby code and you'll see closures all over the place. UEB: demos/threads/thrddemo.exe
  • 5. 5/41 Closure Improves automatisation and integration of new methods, better testability and readability (simplification) SourceSource findfind FunctionFunction nUnitnUnit test test RefactorRefactor changechange PatternPattern block block idiom idiom
  • 6. 6/41 What‘s a closure ? Never touch a running system ?!: //Calls n times a value: • Ruby • • def n_times(a_thing) • return proc{ |n| a_thing * n} • end • c1 = n_times(23) • puts c1.call(1) # -> 23 • puts c1.call(3) # -> 69
  • 7. 7/41 Closure Counter • def counter(start) return proc {start *= 3} end k = counter(1) g = counter(10) • puts k.call # -> 3 • puts k.call # -> 9 • puts g.call # -> 30 • puts k.call # -> 27 UEB: 8_pas_verwechselt.txt
  • 8. 8/41 Closure: Definition • Closures are reusable blocks of code that capture the environment and can be passed around as method arguments for immediate or deferred execution. • Simplification: Your classes are small and your methods too; you’ve said everything and you’ve removed the last piece of unnecessary code.
  • 9. 9/41 Closure in PHP • def times(n): def _f(x): return x * n return _f • t3 = times(3) • print t3 # • print t3(7) # 21
  • 10. 10/41 Closure in Python def generate_power_func(n): def nth_power(x): //closure return x**n return nth_power >>> raised_to_4 = generate_power_func(4) debug: id(raised_to_4) == 0x00C47561 == id(nth_power)). >>> raised_to_4(2) 16
  • 11. 11/41 History • Lisp started in 1959 • Scheme as a dialect of Lisp • One feature of the language was function- valued expressions, signified by lambda. • Smalltalk with Blocks. • Lisp used something called dynamic scoping.
  • 12. 12/41 Dynamic Languages The increase in importance of web services, however, has made it possible to use dynamic languages (where types are not strictly enforced at compile time) for large scale development. Languages such as Python, Ruby, and PHP. Scheme was the first language to introduce closures, allowing full lexical scoping, simplifying many types of programming style. for i:= 0 to SourceTable.FieldCount - 1 do DestTable.Fields[i].Assign(SourceTable.Fields[i]); DestTable.Post;
  • 13. 13/41 Closure in Math Optimization
  • 14. 14/41 Function Pointers • Possible Callback or Procedure Type • Type TMath_func = Procedure(var x: float); //Proc Type procedure fct1(var x: float); //Implement begin x:= Sin(x); end; • var • fct1x:= @fct1 //Reference • fct_table(0.1, 0.7, 0.4, fct1x); //Function as Parameter • fct_table(0.1, 0.7, 0.4, @fct1); //alternativ direct
  • 15. 15/41 Function Pointer II Are Business Objects available (Closure Extensions)? In a simple business object (without fields in the class), you do have at least 4 tasks to fulfil: 1. The Business-Class inherits from a Data-Provider 2. The closure query is part of the class 3. A calculation or business-rule has to be done 4. The object is independent from the GUI, GUI calls the object “Business objects are sometimes referred to as conceptual objects, because they provide services which meet business requirements, regardless of technology”.
  • 16. 16/41 Delegates Method Pointers (Object Organisation) • ppform:= TForm.Create(self); • with ppform do begin • caption:= 'LEDBOX, click to edit, dblclick write out pattern'+ • ' Press <Return> to run the Sentence'; • width:= (vx*psize)+ 10 + 300; • height:= (vy*psize)+ 30; • BorderStyle:= bsDialog; • Position:= poScreenCenter; • OnKeyPress:= @FormKeyPress • OnClick:= @Label1Click; • OnClose:= @closeForm; • Show; • end UEB: 15_pas_designbycontract.txt
  • 17. 17/41 Closure as Parameter • Consider the difference • def managers(emps) • return emps.select {|e| e.isManager} • end Select is a method defined on the Ruby collection class. It takes a block of code, a closure, as an argument. • In C or Pascal you think as a function pointer • In Java as an anonymous inner class • In Delphi or C# you would consider a delegate.
  • 18. 18/41 Function Conclusion Step to Closure Callback A block of code plus the bindings to the environment they came from. This is the formal thing that sets closures apart from function pointers and similar techniques. UEB: 14_pas_primetest.txt
  • 19. 19/41 Closure as Object • When a function is written enclosed in another function, it has full access to local variables from the enclosing function; this feature is called lexical scoping. • Evaluating a closure expression produces a closure object as a reference. • The closure object can later be invoked, which results in execution of the body, yielding the value of the expression (if one was present) to the invoker.
  • 20. 20/41 Closure as Callback • function AlertThisLater(message, timeout) { function fn() { alert(message); } window.setTimeout(fn, timeout); } AlertThisLater("Hi, JScript", 3000); A closure is created containing the message parameter, fn is executed quite some time after the call to AlertThisLater has returned, yet fn still has access to the original content of message!
  • 22. 22/41 Closure as Thread groovy> Thread.start { ('A'..'Z').each {sleep 100; println it} } groovy> Thread.start { (1..26).each {sleep 100; println it} } >>> A >>> 1 >>> B >>> 2
  • 23. 23/41 Lambda Expression • There are three main parts of a function. 1. The parameter list 2. The return type 3. The method body • A Lambda expression is just a shorthand expression of these three elements. // C# • string s => Int.Parse(s)
  • 24. 24/41 Closure Syntax We should keep 3 things in mind: The ability to bind to local variables is part of that, but I think the biggest reason is that the notation to use them is simple and clear. Java's anonymous inner classes can access locals - but only if they are final.
  • 25. 25/41 Syntax Schema >>> def outer(x): ... def inner(y): ... return x+y ... return inner ... • >>> customInner=outer(2) • >>> customInner(3) • result: 5
  • 26. 26/41 Let‘s practice • 1 • 11 • 21 • 1211 • 111221 • 312211 • ??? Try to find the next pattern, look for a rule or logic behind !
  • 27. 27/41 Before C. function runString(Vshow: string): string; var i: byte; Rword, tmpStr: string; cntr, nCount: integer; begin cntr:=1; nCount:=0; Rword:=''; //initialize tmpStr:=Vshow; // input last result for i:= 1 to length(tmpStr) do begin if i= length(tmpstr) then begin if (tmpStr[i-1]=tmpStr[i]) then cntr:= cntr +1; if cntr = 1 then nCount:= cntr Rword:= Rword + intToStr(ncount) + tmpStr[i] end else if (tmpStr[i]=tmpStr[i+1]) then begin cntr:= cntr +1; nCount:= cntr; end else begin if cntr = 1 then cntr:=1 else cntr:=1; //reinit counter! Rword:= Rword + intToStr(ncount) + tmpStr[i] //+ last char(tmpStr) end; end; // end for loop result:=Rword; end; UEB: 9_pas_umlrunner.txt
  • 28. 28/41 After C. function charCounter(instr: string): string; var i, cntr: integer; Rword: string; begin cntr:= 1; Rword:=' '; for i:= 1 to length(instr) do begin //last number in line if i= length(instr) then concatChars() else if (instr[i]=instr[i+1]) then cntr:= cntr +1 else begin concatChars() //reinit counter! cntr:= 1; end; end; //for result:= Rword; end; UEB: 009_pas_umlrunner_solution_2step.txt
  • 29. 29/41 Top 7 to avoid with Closures 1. Duplicated or Dead Code 2. Long Method 3. Large Class (Too many responsibilities) 4. Long Parameter List (Object or closure is missing) 5. Shotgun Surgery (Little changes distributed over too many objects or procedures patterns missed) 6. Data Clumps (Data always together (x,y -> point)) 7. Middle Man (Class with too many delegating methods)
  • 30. 30/41 Closure Advantage – We can avoid: Bad Naming (no naming convention) Duplicated Code (side effects) Long Methods (to much code) Temporary Fields (confusion) Long Parameter List (Object is missing) Data Classes (no methods) • Large Class • Class with too many delegating methods • Coupled classes UEB: 33_pas_cipher_file_1.txt
  • 31. 31/41 Refactoring Closure Delete a class with referenceSafe DeleteModel Getter- und Setter einbauenEncapsulate FieldsClass Ersetze vererbte Methoden durch Delegation in innere Klasse Replace Inheritance with Delegation Component Erzeuge Referenzen auf Klasse mit Referenz auf implementierte Schnittstelle Use InterfaceInterface Aus Methoden ein Interface erzeugenExtract InterfaceInterface Extract a Codepassage or define a ClosureExtract MethodClass Ersetzen eines Ausdrucks durch einen Methodenparameter Introduce ParameterClass Aus Methoden, Eigenschaften eine Oberklasse erzeugen und verwenden Extract SuperclassClass Verschieben eines PackagesMove PackagePackage Umbenennen eines PackagesRename PackagePackage DescriptionRefactoring FunctionUnit
  • 32. 32/41 Case Study: The Polygraph • Design Case Study Polyp An easy one states that only one statement should exists for every source line, like next and put a Var lifetime as short as possible. Can you read (Yes-No)? if YES then OK if NO then you are a Liar! var Rec: TMyRec; begin .. with Rec do begin .. title:= ‘Hello Implikation'; end; end;
  • 34. 34/41 Polyp Optimizations Some tips and hints to optimize your code: 1. one statement and short var 2. assert call, IfThen() 3. use closure snippets 4. naming convention 5. $IF-directive 6. Const instead of var parameters 7. extract code from idioms 8. comment your closure code
  • 36. 36/41 Polyp - Demeter konkret 1. [M1] an Objekt O selbst Bsp.: self.initChart(vdata); 2. [M2] an Objekte, die als Parameter in der Nachricht m vorkommen Bsp.: O.acceptmemberVisitor(visitor) visitor.visitElementChartData; 3. [M3] an Objekte, die O als Reaktion auf m erstellt Bsp.: visitor:= TChartVisitor.create(cData, madata); 4. [M4] an Objekte, auf die O direkt mit einem Member zugreifen kann Bsp.: O.Ctnr:= visitor.TotalStatistic
  • 37. 37/41 Polyp Demeter Test as SEQ UEB: 56_pas_demeter.txt
  • 38. 38/41 Polyp – GUI Optimization function digitButton (digit) return Button { label = digit, action = function () add_to_display(digit) end } end • Each button has a callback function to be called when the user presses the button; Closures are useful for callback functions, too.
  • 40. 40/41 Closure Links: • http://gafter.blogspot.com/2007/01/definition-of- closures.html • Michael Bolin, “Closure: The Definitive Guide", O'Reilly Media; Auflage: 1 (20. Oktober 2010). • http://www.javac.info/ • http://sourceforge.net/projects/maxbox/ • Refactoring Martin Fowler (1999, Addison-Wesley) • http://www.softwareschule.ch/download/closures_report.pdf • http://www.refactoring.com
  • 41. 41/41 This is not The End: Q&A? max@kleiner.com softwareschule.ch