Functions
Utilities for working with blocks and functions.
memoize
Section titled “memoize”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 seconddelayBackground
Section titled “delayBackground”Like delay, but executes on a background queue.
_.delayBackground(^{ [self performExpensiveOperation];}, 1000);// => executes on background thread after 1 secondDefers invoking the block until the current call stack has cleared.
_.defer(^{ NSLog(@"deferred");});// => logs "deferred" after current executiondeferBackground
Section titled “deferBackground”Like defer, but executes on a background queue.
_.deferBackground(^{ [self performExpensiveOperation];});// => executes on background thread after current call stack clearsthrottle
Section titled “throttle”Creates a throttled version that only invokes at most once per wait period.
_ThrottledBlock throttled = _.throttle(^{ [self updatePosition];}, 100);debounce
Section titled “debounce”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"compose
Section titled “compose”Returns the composition of a list of functions.
N* (^greet)(S*) = ...;N* (^exclaim)(S*) = ...;
_ComposeBlock welcome = _.compose(greet, exclaim);welcome(@"moe");// => "hi: MOE!"