David T. Lewis uploaded a new version of Help-Squeak-TerseGuide to project The Trunk:
http://source.squeak.org/trunk/Help-Squeak-TerseGuide-dtl.2.mcz==================== Summary ====================
Name: Help-Squeak-TerseGuide-dtl.2
Author: dtl
Time: 28 November 2010, 7:43:55.955 pm
UUID: 8b18cab9-7183-4c5e-8cac-f79c4400da43
Ancestors: Help-Squeak-TerseGuide-dtl.1
Add Juan's factorial example to illustrate variable scoping for block closures
=============== Diff against Help-Squeak-TerseGuide-dtl.1 ===============
Item was changed:
----- Method: TerseGuideHelp classSide>>block (in category 'pages') -----
block
^HelpTopic
title: 'Blocks'
contents:
'"************************************************************************
* Blocks: *
* - blocks are objects and may be assigned to a variable *
* - value is last expression evaluated unless explicit return *
* - blocks may be nested *
* - specification [ arguments | | localvars | expressions ] *
* - ^expression terminates block & method (exits all nested blocks) *
* - blocks intended for long term storage should not contain ^ *
************************************************************************"
+ | x y z fac |
- | x y z |
x := [ y := 1. z := 2. ]. x value. "simple block usage"
x := [ :argOne :argTwo | argOne, '' and '' , argTwo.]. "set up block with argument passing"
Transcript show: (x value: ''First'' value: ''Second''); cr. "use block with argument passing"
x := [:e | | v | v := 1. e + v] value: 2. "localvar in a block"
+ fac := [ :n | n > 1 ifTrue: [n * (fac value: n-1)] ifFalse: [1]]. "closure on block variable"
+ fac value: 5. "closure variable scoped to its block"
'!