Skip to content

Functions

Utilities for working with blocks and functions.

Caches the result of a function based on its arguments.

_MemoizeBlock fibonacci = _.memoize(^id(N* n) {
return n.I < 2 ? n : N.I([fibonacci(N.I(n.I - 1)) intValue] +
[fibonacci(N.I(n.I - 2)) intValue]);
});

Invokes a block after a specified wait time (in milliseconds).

_.delay(^{
NSLog(@"logged later");
}, 1000);
// => logs "logged later" after 1 second

Like delay, but executes on a background queue.

_.delayBackground(^{
[self performExpensiveOperation];
}, 1000);
// => executes on background thread after 1 second

Defers invoking the block until the current call stack has cleared.

_.defer(^{
NSLog(@"deferred");
});
// => logs "deferred" after current execution

Like defer, but executes on a background queue.

_.deferBackground(^{
[self performExpensiveOperation];
});
// => executes on background thread after current call stack clears

Creates a throttled version that only invokes at most once per wait period.

_ThrottledBlock throttled = _.throttle(^{
[self updatePosition];
}, 100);

Creates a debounced version that delays invoking until after wait milliseconds have elapsed since the last call.

_DebouncedBlock debounced = _.debounce(^{
[self calculateLayout];
}, 300);

Creates a version that can only be called once.

_OnceBlock initialize = _.once(^id{
return [self createApplication];
});

Creates a version that only runs after being called N times.

_AfterBlock renderNotes = _.after(notes.count, ^{
[self render];
});
for (Note* note in notes) {
[note asyncSave:^{ renderNotes(); }];
}

Wraps a block inside another block.

S* (^hello)(S*) = ^S*(S* name) {
return [NSString stringWithFormat:@"hello: %@", name];
};
_WrapBlock wrapped = _.wrap(hello, ^id(id func, ...) {
return [NSString stringWithFormat:@"before, %@, after",
((S*(^)(S*))func)(@"moe")];
});
wrapped();
// => "before, hello: moe, after"

Returns the composition of a list of functions.

N* (^greet)(S*) = ...;
N* (^exclaim)(S*) = ...;
_ComposeBlock welcome = _.compose(greet, exclaim);
welcome(@"moe");
// => "hi: MOE!"