- Turning an eager, pure computation into a lazy one:
RACSequence
,RACSignal
- Side-effecting work that is safe to perform multiple times:
RACSignal
,RACCommand
- Side-effecting work that is safe to perform multiple times serially, but not concurrently:
RACCommand
,RefCount()
RACScheduler
would seem to apply here, but execution could become interleaved and thus not serial
- Side-effecting work that should only be performed once, then memoized:
RACMulticastConnection
with aRACReplaySubject
,RACSequence
- Enabling/disabling a UI based on whether work can be performed:
RACCommand
,RACReplaySubject
- Replaying is a Good Idea™ because this information may arrive before the UI is ready for it
- Updating a UI with information about work in progress:
RACCommand
,RACSubject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define coroutine(ARGS) \ | |
__block unsigned long ext_coroutine_line_ = 0; \ | |
\ | |
return ^(ARGS){ \ | |
for (;; ext_coroutine_line_ = 0) \ | |
switch (ext_coroutine_line_) \ | |
default: | |
#define return_coroutine \ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Left, typename Right> | |
struct Intersect | |
{ | |
// This will fail to compile if there's no specialization for Intersect<Right, Left>, | |
// thereby verifying that we've handled all combinations. | |
typename Intersect<Right, Left>::Result operator() (const Left &lhs, const Right &rhs) const | |
{ | |
return Intersect<Right, Left>()(rhs, lhs); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
try myFunctionWhichOnlyThrowsOneErrorType() | |
} catch let err as ThatOneErrorType { | |
handleAppropriately() | |
} catch let err { | |
preconditionFailure("Unexpected error: \(err)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Asynchronously loads UIImages from the given URLs, but throttles the loading | |
// to keep memory and CPU usage sane. | |
- (RACSignal *)loadImagesAtURLs:(NSArray *)imageURLs { | |
// Map each URL to a signal of work. The result is a signal of work signals. | |
return [[imageURLs.rac_signal | |
map:^(NSURL *imageURL) { | |
return [[[[NSURLConnection | |
// Load the URL asynchronously. | |
rac_sendAsynchronousRequest:[NSURLRequest requestWithURL:imageURL]] | |
reduceEach:^(NSURLResponse *response, NSData *data) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reads until the given stream is exhausted, creating and subscribing to a new | |
// signal using `writer` for each chunk of data that is read. | |
// | |
// Sends completed or error. | |
- (RACSignal *)readStream:(Stream *)stream writingWithGenerator:(RACSignalGenerator *)writer { | |
return [[[RACSignal | |
create:^(id<RACSubscriber> subscriber) { | |
while (!subscriber.disposable.disposed && stream.hasData) { | |
NSError *error = nil; | |
NSData *data = [stream readData:&error]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (id)initWithEnabled:(RACSignal *)enabled provider:(RACSignalProvider *)provider { | |
if (self = [super init]) { | |
_enabled = [enabledSignal replayLast]; | |
_decoratedProvider = [[[RACSignalProvider alloc] | |
initWithBlock:^(id input) { | |
return [[self.enabled take:1] flattenMap:^(NSNumber *enabled) { | |
if (enabled.boolValue) { | |
return [RACSignal return:input]; | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__block void (^myRecursiveBlock)(int); | |
id myBlock = ^(int x) { | |
if (x > 5) { | |
// Release the block from its own implicit retain cycle. | |
myRecursiveBlock = nil; | |
} else { | |
myRecursiveBlock(x + 1); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Control/Monad/Trans/Resource.hs:613:24: | |
Could not deduce (m ~ IO) | |
from the context (MonadBaseControl IO m) | |
bound by the type signature for | |
resourceForkIO :: MonadBaseControl IO m => | |
ResourceT m () -> ResourceT m ThreadId | |
at Control/Monad/Trans/Resource.hs:601:19-81 | |
`m' is a rigid type variable bound by | |
the type signature for | |
resourceForkIO :: MonadBaseControl IO m => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define self \ | |
( \ | |
_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wunused-value\"") self, \ | |
/* Depends on _cmd being a const copy, like other captured variables. */ \ | |
_Pragma("clang diagnostic pop") (*(_cmd = _cmd, &self)) \ | |
) | |
// Replace libextobjc's @weakify with one that's aware of the "self" macro. | |
#undef ext_weakify_ | |
#define ext_weakify_(INDEX, CONTEXT, VAR) \ |