SlideShare uma empresa Scribd logo
1 de 36
Baixar para ler offline
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”



                                             1/36
Agenda EKON 06.11.2012
• Closure as a Code Block
• Long History
• Dynamic Languages
• Functional Programming Examples
• Closures & Refactoring
• Case Study Experiences with a Polygraph Design
• Links



                                         2/36
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?


                                  3/36
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



                                          4/36
What‘s a closure ?
Never touch a running system ?!:
//Calls n times a value:
• python
•
• 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

                                   5/36
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



                                       6/36
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.

                                   7/36
Closure time
• def times(n):
def _f(x):
 return x * n
return _f

• t3 = times(3)
• print t3 #
• print t3(7) # 21

                         8/36
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


                                           9/36
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.


                                    10/36
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,
  Delphi and PHP.
Scheme was the first language to introduce closures,
  allowing full lexical scoping, simplifying many types of
  programming style.
  to discuss:
  for i:= 0 to SourceTable.FieldCount - 1 do
        DestTable.Fields[i].Assign(SourceTable.Fields[i]);
    DestTable.Post;




                                                        11/36
Closure in Math Optimization




                         12/36
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

                   271_closures_study_workingset2.txt
                                                        13/36
Anonymous I
type
  TWriter = reference to procedure;
var xt: TWriter;
begin
  xt:= procedure
  begin
     writeln('Display this Text Ext');
  end;
 xt;
readln;
end;



                                         14/36
Anonymous II
type
  TIntegerConvertFunc = reference to function(s: string):
integer; //Has a return type

var
  myFunction: TIntegerConvertFunc;
begin
  myfunction:= function(s: string): integer
    begin
       result:= IntToStr(s);
    end;
// ...

                                               15/36
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

                                                                16/36
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.


                                                      17/36
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



                                   18/36
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.



                                               19/36
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!

                                                     20/36
Closure SEQ




              21/36
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


                                        22/36
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)


                                    23/36
Closure Syntax I
procedure OuterMethod;
var upperScope: string;
     procedure InnerMethod;
     var innerScope: string;
     begin
          // Here, upperScope and innerScope are visible.
          // lowerScope is not visible.
          writeln(text);
     end;
var lowerScope: string;
begin
// upperScope and lowerScope are visible here.
  InnerMethod;
end;




                                                            24/36
Closure Syntax II
procedure TMyClass.DoCount;
var j: integer;
ShowCounter: TProc; // Method no longer has a parameter.
begin
 ShowCounter:= procedure
                     begin
                     // j is known here because it is wrapped in a closure
                     // and made available to us!
                        writeln(IntToStr(j));
                     end;
     for j:= 1 to 10 do
        ShowCounter; // j is no longer passed
end;



                                                               25/36
Syntax Schema
>>> def outer(x):
    ... def inner(y):
       ... return x+y
    ... return inner
...
• >>> customInner=outer(2)
• >>> customInner(3)
• result: 5

                             26/36
Let‘s practice
• 1
• 11
• 21
• 1211
• 111221
• 312211
• ???
Try to find the next pattern, look for a rule or
  logic behind !
                                    27/36
function runString(Vshow: string): string;
var i: byte;
Rword, tmpStr: string;
cntr, nCount: integer;
begin
cntr:=1; nCount:=0;
                         Before C.
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;                                        UEB: 9_pas_umlrunner.txt
end;

                                                                    28/36
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/36
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


                                             30/36
Refactoring Closure
Unit        Refactoring Function       Description
Package     Rename Package             Umbenennen eines Packages
Package     Move Package               Verschieben eines Packages
Class       Extract Superclass         Aus Methoden, Eigenschaften eine
                                          Oberklasse erzeugen und verwenden
Class       Introduce Parameter        Ersetzen eines Ausdrucks durch einen
                                          Methodenparameter
Class       Extract Method             Extract a Codepassage or define a Closure
Interface   Extract Interface          Aus Methoden ein Interface erzeugen
Interface   Use Interface              Erzeuge Referenzen auf Klasse mit
                                          Referenz auf implementierte Schnittstelle
Component   Replace Inheritance with   Ersetze vererbte Methoden durch Delegation
               Delegation                 in innere Klasse
Class       Encapsulate Fields         Getter- und Setter einbauen
Model       Safe Delete                Delete a class with reference


                                                                31/36
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;




                                                            32/36
Polyp Design




Dependency Inversion


                                      33/36
Final - $Closure Test




                        34/36
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



                                                     35/36
This is not The End:
        Q&A?
      max@kleiner.com
      softwareschule.ch




                          36/36

Mais conteúdo relacionado

Mais procurados

Virtual platform
Virtual platformVirtual platform
Virtual platform
sean chen
 
Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me?
DVClub
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
xu liwei
 

Mais procurados (20)

ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogramming
 
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
An Embedded Error Recovery and Debugging Mechanism for Scripting Language Ext...
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
Virtual platform
Virtual platformVirtual platform
Virtual platform
 
Async await in C++
Async await in C++Async await in C++
Async await in C++
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
Go Lang Tutorial
Go Lang TutorialGo Lang Tutorial
Go Lang Tutorial
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminary
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me?
 
Java Generics
Java GenericsJava Generics
Java Generics
 
FTD JVM Internals
FTD JVM InternalsFTD JVM Internals
FTD JVM Internals
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
C++11
C++11C++11
C++11
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
 
C# for C++ Programmers
C# for C++ ProgrammersC# for C++ Programmers
C# for C++ Programmers
 

Semelhante a A closure ekon16

c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444
PurvaShyama
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
mspline
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
jamvantsolanki
 

Semelhante a A closure ekon16 (20)

EKON 12 Closures Coding
EKON 12 Closures CodingEKON 12 Closures Coding
EKON 12 Closures Coding
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
maXbox Starter 31 Closures
maXbox Starter 31 ClosuresmaXbox Starter 31 Closures
maXbox Starter 31 Closures
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
 
c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
CODEsign 2015
CODEsign 2015CODEsign 2015
CODEsign 2015
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Appsec obfuscator reloaded
Appsec obfuscator reloadedAppsec obfuscator reloaded
Appsec obfuscator reloaded
 
Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)Algorithm and Programming (Procedure and Function)
Algorithm and Programming (Procedure and Function)
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
C++ theory
C++ theoryC++ theory
C++ theory
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 
Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 
Advance python
Advance pythonAdvance python
Advance python
 

Mais de Max Kleiner

Mais de 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
 
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
 
EKON22 Introduction to Machinelearning
EKON22 Introduction to MachinelearningEKON22 Introduction to Machinelearning
EKON22 Introduction to Machinelearning
 
TensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 MachinelearningTensorFlow BASTA2018 Machinelearning
TensorFlow BASTA2018 Machinelearning
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

A closure ekon16

  • 1. 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” 1/36
  • 2. Agenda EKON 06.11.2012 • Closure as a Code Block • Long History • Dynamic Languages • Functional Programming Examples • Closures & Refactoring • Case Study Experiences with a Polygraph Design • Links 2/36
  • 3. 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? 3/36
  • 4. 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 4/36
  • 5. What‘s a closure ? Never touch a running system ?!: //Calls n times a value: • python • • 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 5/36
  • 6. 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 6/36
  • 7. 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. 7/36
  • 8. Closure time • def times(n): def _f(x): return x * n return _f • t3 = times(3) • print t3 # • print t3(7) # 21 8/36
  • 9. 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 9/36
  • 10. 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. 10/36
  • 11. 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, Delphi and PHP. Scheme was the first language to introduce closures, allowing full lexical scoping, simplifying many types of programming style. to discuss: for i:= 0 to SourceTable.FieldCount - 1 do DestTable.Fields[i].Assign(SourceTable.Fields[i]); DestTable.Post; 11/36
  • 12. Closure in Math Optimization 12/36
  • 13. 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 271_closures_study_workingset2.txt 13/36
  • 14. Anonymous I type TWriter = reference to procedure; var xt: TWriter; begin xt:= procedure begin writeln('Display this Text Ext'); end; xt; readln; end; 14/36
  • 15. Anonymous II type TIntegerConvertFunc = reference to function(s: string): integer; //Has a return type var myFunction: TIntegerConvertFunc; begin myfunction:= function(s: string): integer begin result:= IntToStr(s); end; // ... 15/36
  • 16. 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 16/36
  • 17. 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. 17/36
  • 18. 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 18/36
  • 19. 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. 19/36
  • 20. 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! 20/36
  • 21. Closure SEQ 21/36
  • 22. 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 22/36
  • 23. 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) 23/36
  • 24. Closure Syntax I procedure OuterMethod; var upperScope: string; procedure InnerMethod; var innerScope: string; begin // Here, upperScope and innerScope are visible. // lowerScope is not visible. writeln(text); end; var lowerScope: string; begin // upperScope and lowerScope are visible here. InnerMethod; end; 24/36
  • 25. Closure Syntax II procedure TMyClass.DoCount; var j: integer; ShowCounter: TProc; // Method no longer has a parameter. begin ShowCounter:= procedure begin // j is known here because it is wrapped in a closure // and made available to us! writeln(IntToStr(j)); end; for j:= 1 to 10 do ShowCounter; // j is no longer passed end; 25/36
  • 26. Syntax Schema >>> def outer(x): ... def inner(y): ... return x+y ... return inner ... • >>> customInner=outer(2) • >>> customInner(3) • result: 5 26/36
  • 27. Let‘s practice • 1 • 11 • 21 • 1211 • 111221 • 312211 • ??? Try to find the next pattern, look for a rule or logic behind ! 27/36
  • 28. function runString(Vshow: string): string; var i: byte; Rword, tmpStr: string; cntr, nCount: integer; begin cntr:=1; nCount:=0; Before C. 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; UEB: 9_pas_umlrunner.txt end; 28/36
  • 29. 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/36
  • 30. 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 30/36
  • 31. Refactoring Closure Unit Refactoring Function Description Package Rename Package Umbenennen eines Packages Package Move Package Verschieben eines Packages Class Extract Superclass Aus Methoden, Eigenschaften eine Oberklasse erzeugen und verwenden Class Introduce Parameter Ersetzen eines Ausdrucks durch einen Methodenparameter Class Extract Method Extract a Codepassage or define a Closure Interface Extract Interface Aus Methoden ein Interface erzeugen Interface Use Interface Erzeuge Referenzen auf Klasse mit Referenz auf implementierte Schnittstelle Component Replace Inheritance with Ersetze vererbte Methoden durch Delegation Delegation in innere Klasse Class Encapsulate Fields Getter- und Setter einbauen Model Safe Delete Delete a class with reference 31/36
  • 32. 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; 32/36
  • 34. Final - $Closure Test 34/36
  • 35. 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 35/36
  • 36. This is not The End: Q&A? max@kleiner.com softwareschule.ch 36/36