In Javascript, you'll often see things like this:
for (var i=0; i<5; ++i) {
var someString = strings[i];
button.onclick = (function (str) {
return function (event) {
alert(str);
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> | |
| <script type="text/javascript" src="http://static.stackmob.com/js/json2-min.js"></script> | |
| <script type="text/javascript" src="http://static.stackmob.com/js/underscore-1.3.0-min.js"></script> | |
| <script type="text/javascript" src="http://static.stackmob.com/js/backbone-0.5.3-min.js"></script> | |
| <script type="text/javascript" src="http://static.stackmob.com/js/stackmob-js-0.1.1-min.js"></script> | |
| <script type="text/javascript"> |
| -(UIImage *)sidebar:(HSImageSidebarView *)sidebar imageForIndex:(NSUInteger)anIndex { | |
| return [myArrayOfImages objectAtIndex:anIndex]; | |
| } | |
| - (NSUInteger)countOfImagesInSidebar:(HSImageSidebarView *)sidebar { | |
| return [myArrayOfImages count]; | |
| } |
| // This is just a toy example, but it demonstrates the semantic | |
| // overloading of the 'return' keyword when blocks are in play. | |
| // Note that I'm not saying this is confusing, per se - I'm just | |
| // saying that it adds cognitive load as I look at the method and | |
| // try to figure out which 'return' statements actually represent | |
| // an early return. | |
| - (Person *)findPersonWithName:(NSString *)name { | |
| NSArray *families; | |
| if (name.length == 0) { |
| NSView *view; | |
| // view.hidden == YES right now | |
| // view.alpha = 0.0 right now; | |
| [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) { | |
| context.duration = 0; | |
| view.hidden = NO; | |
| }]; |
| import sys | |
| import random | |
| if __name__ == "__main__": | |
| cardcount = int(sys.argv[1]) | |
| drawcount = 8 | |
| cards_not_seen = set(range(cardcount)) |
| #import <Foundation/Foundation.h> | |
| #import <CoreData/CoreData.h> | |
| @interface CDAsynchronousArray : NSArray | |
| // Provides access to the full count of objects. May be slow, e.g. if the underlying data store | |
| // is only providing paginated access to objects. May not even be available, as in a search | |
| // engine result set. | |
| - (void)accessCountWithHandler:(void (^)(NSUInteger count, NSError *))countHandler; |
| - (void)testAddRemove | |
| { | |
| if (arc4random() % 2) { | |
| NSLog(@"A"); | |
| [_other addWithDispatchBlock: ^{ | |
| NSLog(@"self: %@", self); // <-- warning about capturing 'self' | |
| }]; | |
| } | |
| else { | |
| NSLog(@"B"); |
| Rand => 237 | |
| Perrin => 153 | |
| Egwene => 130 | |
| Mat => 115 | |
| Elayne => 83 | |
| Nynaeve => 61 | |
| Min => 35 | |
| Faile => 28 | |
| Aviendha => 23 | |
| Galad => 22 |