Smalltalk Blocks

ingve1 pts0 comments

Smalltalk Blocks | MediumSitemapOpen in appSign up<br>Sign in

Medium Logo

Get app<br>Write

Search

Sign up<br>Sign in

Smalltalk Blocks

Donald Raab

3 min read·<br>1 day ago

Listen

Share

The expression of the square bracket<br>Press enter or click to view image in full size

Photo by Chor Tsang on UnsplashBlock Syntax<br>In the Smalltalk programming language, blocks are anonymous deferred pieces of code that can be stored in variables or passed as parameters. These are referred to as closures or lambdas. Literal blocks start and end with square brackets — []. In Pharo Smalltalk, a block is an instance of the BlockClosure class. In a block, the pipe character separates the parameters on the left and the expression on the right (e.g. [ :parameter | expression ]. A zero argument block will have no pipe (e.g. [ true ]). Parameters have alphanumeric names, and start with the colon character. The code in a block can be evaluated by calling the method value. The return value will be the result of evaluating the last expression in the block. A block with parameters can be executed by calling the equivalent value methods with parameters. (e.g. [:a | a] value: 1). If a block has more than four parameters, then you can evaluate the block with the valueWithArguments: array method. A literal array in Smalltalk has the syntax #(1 2 3 4 5).<br>The following are some block examples that are evaluated with the specified parameters.<br>[ 0 ] value. // 0<br>[ :a | a ] value: 1. // 1<br>[ :a :b | a + b ] value: 1 value: 2. // 3<br>[ :a :b :c | a + b + c ] value: 1 value: 2 value: 3. // 6<br>[ :a :b :c :d | a + b + c + d] value: 1 value: 2 value: 3 value: 4. // 10<br>[ :a :b :c :d :e | a + b + c + d + e] valueWithArguments: #(1 2 3 4 5). // 15The classes of various blocks.<br>[ ] class. // ConstantBlockClosure<br>[ 0 ] class. // ConstanceBlockClosure<br>[ :a :b | a + b ] class. // FullBlockClosureBlocks in Practice<br>Blocks in Smalltalk are used to build control structures, including if-statements, for-loops, and internal iterators on collections.<br>If-statement<br>true ifTrue: [ 1 ]. // 1<br>false ifTrue: [ 1 ]. // nil<br>false ifFalse: [ 2 ]. // 2.<br>true ifTrue: [ 1 ] ifFalse: [2]. // 1<br>false ifTrue: [ 1 ] ifFalse: [2]. // 2For-loop<br>1 to: 10 do: [ :each | Transcript show: each ] // 12345678910Internal iterator<br>#(1 2 3 4 5) do: [:each | Transcript show: each ] // 12345<br>(1 to: 5) inject: 0 into: [ :a :b | a + b ]. // 15<br>(1 to: 10) select: [ :each | each even ]. // #(2 4 6 8 10)<br>(1 to: 10) select: [ :each | each odd ]. // #(1 3 5 7 9)Final Thoughts<br>Blocks are literally the building blocks of so many things in Smalltalk. I hope you found this brief introduction of blocks in Smalltalk interesting.<br>Thanks for reading!<br>I am the creator of and committer for the Eclipse Collections OSS project, which is managed at the Eclipse Foundation. Eclipse Collections is open for contributions. I am the author of the book, Eclipse Collections Categorically: Level up your programming game.

Smalltalk

Programming

Written by Donald Raab

2.5K followers<br>·340 following

Java Champion. Creator of the Eclipse Collections OSS Java library (https://github.com/eclipse-collections). Inspired by Smalltalk. Opinions are my own.

Help

Status

About

Careers

Press

Blog

Store

Privacy

Rules

Terms

Text to speech

value smalltalk blocks block parameters collections

Related Articles