damien cassou, stéphane ducasse and luc...

15
Introduction to Blocks Damien Cassou, Stéphane Ducasse and Luc Fabresse W2S06 http://www.pharo.org

Upload: others

Post on 11-Mar-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Introduction to BlocksDamien Cassou, Stéphane Ducasse and Luc Fabresse

W2S06

http://www.pharo.org

Page 2: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Blocks

Blocks are: kind of anonymous methods

◦ also called (lexical) closures used everywhere in Pharo

◦ loops, conditionals, iterators, ...◦ GUI frameworks, DSLs, ...◦ at the heart of the system

just introduced in Java 8.0

W2S06 2 / 15

Page 3: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Block Definition

A block is defined by [ ]

[ expressions. ... ]

W2S06 3 / 15

Page 4: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Block Definition Does not Execute Code Executing code may signal an Error

( 1 / 0 )−> Error

But, no error when defining a block◦ a block definition does not execute its body◦ a block definition freezes its body computation

[ 1 / 0 ]> [ 1 /0 ]

[ 1 / 0 ].1 + 2> 3

W2S06 4 / 15

Page 5: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Executing a Block

Executing a block is done explicitly through value

[ 2 + 6 ] value> 8

[ 1 / 0 ] value> Error

W2S06 5 / 15

Page 6: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

A Block with 1 Argument

A block can take arguments (just like methods)

[ :x | x + 2 ]

[ ] delimits the block :x is a block argument x + 2 is the block body

[ :x | x + 2 ] value: 5> 7

value: 5 executes the block with 5 as argument◦ x is 5 during the block execution

W2S06 6 / 15

Page 7: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Block Execution Value

Block execution returns the value of the last expression

[ :x |x + 33.x + 2 ] value: 5> 7

W2S06 7 / 15

Page 8: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Blocks can be Stored

A block can be stored in a variable A block can be evaluated multiple times

| add2 |add2 := [ :x | x + 2 ].

add2 value: 5.> 7

add2 value: 33> 35

W2S06 8 / 15

Page 9: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Defining a Block with 2 Arguments

Example:

[ :x :y | x + y ]

:x :y are block argumentsHow to execute a block with two arguments?

[ :x :y | x + y ] ??? 5 7> 12

W2S06 9 / 15

Page 10: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Executing a Block with 2 Arguments

[ :x :y | x + y ] value: 5 value: 7> 12

value: 5 value: 7 evaluates the block with 5 and 7◦ x is 5 and y is 7 during the block evaluation

W2S06 10 / 15

Page 11: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

A Block with Temporary Variables

Blocks can define temp. variables (just like methods)

Collection>>a�ect: anObject when: aBooleanself do: [ :index | | args |args := ....aBooleanifTrue: [ anObject do: args ]ifFalse: [ anObject doDi�erently: args ] ].

| args | defines a temporary variable named args args exists only during block evaluation

W2S06 11 / 15

Page 12: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Returning from a Block Returns from the MethodWhen a return ^ is executed in a block, computation exits themethod defining the block

Integer>>factorial"Answer the factorial of the receiver."

self = 0 ifTrue: [ ^ 1 ].self > 0 ifTrue: [ ^ self * (self− 1) factorial ].self error: 'Not valid for negative integers'

0 factorial>1

42 factorial>1405006117752879898543142606244511569936384000000000

W2S06 12 / 15

Page 13: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

A Design Advice

Use blocks with 2 or 3 arguments maximum Define a class instead of a block for more arguments A block encapsulates only 1 computation

◦ it cannot define more facets (e.g., printing)

W2S06 13 / 15

Page 14: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

Summary on Blocks

[ :variable1 :variable2 ... || tmp |expression1.... variable1 ...] value: ... value: ...

Kind of anonymous method Technically lexical closures Can be stored in variables and method arguments Basis of conditionals, loops and iterators (see companion

lectures) Further readings: http://deepintopharo.org

W2S06 14 / 15

Page 15: Damien Cassou, Stéphane Ducasse and Luc Fabressermod-pharo-mooc.lille.inria.fr/MOOC/PharoMOOC/Week2/C019...Executing a Block with 2 Arguments [:x:y|x+y]value:5value:7 >12 value: 5value:

A course by

and

in collaboration with

Inria 2020

Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 Francehttps://creativecommons.org/licenses/by-nc-nd/3.0/fr/