class Counter {
add(amount) {
// Code I write
}
}
const counter = new Counter();
// Call add method.
counter.add(1);
Objective-C classes allow sending messages
// Send add message.
[counter add:@1];
// Here is the identifier for this message.
SEL messageIdentifier = @selector(add:);
// Send add message.
[counter performSelector:@selector(add:) withObject:@1];
// Create message ready to send.
// Yes, it is a fair amount of code…
NSMethodSignature *signature = [Counter
instanceMethodSignatureForSelector:@selector(add:)];
NSInvocation *sender = [NSInvocation
invocationWithMethodSignature:signature];
NSNumber *amount = @1;
[sender setArgument:&amount atIndex:2];
[sender setTarget:counter];
// Now, let’s send the add message twice.
[sender invoke];
[sender invoke];
Adding sent numbers
// This is our message generator:
function* GenerateNumbers() {
yield 1; // These
yield 2; // are
yield 3; // our messages
}
// This is our message processor:
function adder(messageGenerator) {
let total = 0;
for (const message of messageGenerator()) {
total += message;
}
return total;
}
adder(GenerateNumbers);
// 6