Leaning Smalltalk - how to modify BlockContexts

Richard A. O'Keefe ok at cs.otago.ac.nz
Mon Jun 14 02:26:08 UTC 2004


Edouard Poor <edouard.lists at gmail.com> wrote:
	As a learning excercise I've been mentally translating what I'm used
	to in C++ into Smalltalk, and have been very impressed so far at the
	power of the language - until I wanted to start manipulating
	BlockContext objects.
	
	The idea I wanted to use from C++ (well, from STL, so it has a more
	functional origin) was that of binding arguments in a BlockContext in
	order to create a new BlockContext with one or more of the arguments
	fixed with a binding.
	
If you have good support for lambda-expressions (blocks) then you don't
need this; C++ only really has it because it doesn't have good support
for blocks.

	int multiple( int a, int b ) { return a * b );
	function<int, int> doubles = bind( multiple, _1, 2 );
	x = doubles( 12 );   // x equals 24

Instead of your multiple() function, let's start from Number>> *

Just write

    double := [:aNumber | aNumber * 2].
    x := double value: 12.

	So what am I looking for in Smalltalk?
	
You are looking for the simplest thing that could possibly work, which
is never to use bind1st(), bind2nd(), or bind() at all.  If you want a
function of n arguments, write a block with n arguments; the fact that
it calls another block or for that matter another method doesn't matter.]

Your C++ example using bind() required 53 characters;
the Smalltalk equivalent (just write a block and have it do *explicitly*
whatever call you have in mind) required only 35 characters.  It's easier
*without* bind().




More information about the Squeak-dev mailing list