Boost Bind

last modified: May 27, 2009

A member of the BoostLibraries. See http://boost.org/libs/bind/bind.html. The bind library allows one to produce new functions from old functions. For example, given a binary function that multiplies integers together:

int mul(int a, int b) {
        return a * b;
},

One could produce a new unary function that multiplies integers by 7:

boost::bind(mul, 7, _1);

The _1 is a placeholder for the first argument to the new function. The result of this expression is a FunctorObject that behaves exactly like the unary function, lambda x: 7*x.

A notable area of application is in callbacks. One can use bind to supply context to a function, or more often, to provide an instance to a class member function reference:

class Foo { public: void bar(int xyz) { }, },;
Foo f;
boost::bind(&Foo::bar, f, _1);

How is this done in SmallTalk? If I have a binary block [ :a :b | a * b ], how can I transform it into a unary block by binding one of the arguments?

E.g. what would you have to add as "BlockContext>>bindFirst" or "BlockContext>>bindSecond" in order to achieve this? Or is there another way of doing this in SmallTalk?

-- Edouard

I would like to know the answer too. Anyone?

So, would this be sort of like partial-evaluation in a functional language, except with more control?

Doing it the normal way would probably do (can't remember exact syntax);

binBlock := [:a :b| a * b].
unBlock := [:a | binBlock doWith: a and: 12 ].
orBlock := [:b | binBlock doWith: 3 and: b ].

-- Tiogshi


CategoryBoost CategoryCpp


Loading...