OCTotallyLazy 是一个框架,它为 Objective C 集合对象添加了功能行为,以及一个名为 Sequence 的懒加载集合对象。它是 Dan Bodart 的 TotallyLazy Java 库的局部移植版本,可在此处找到:http://code.google.com/p/totallylazy/
目前,查找完整行为的地方是最好的测试类,请查看 https://github.com/stuartervine/OCTotallyLazy/blob/master/src/test-unit/SequenceTest.m
import <OCTotallyLazy/OCTotallyLazy.h>
NSArray
drop:
dropWhile:
filter:
find:
flatMap:
flatten
fold: with:
foreach:
isEmpty
groupBy:
grouped:
head
headOption
join:
map:
mapWithIndex:
merge:
partition:
reduce:
reverse
splitAt:
splitOn:
splitWhen:
tail
take:
takeWhile:
takeRight:
toString
toString:
toString: separator: end:
zip:
zipWithIndex
asSequence
asSet
asDictionary
映射(Sequence,NSArray,部分在 NSSet,NSDictionary)
[sequence(@"one", @"two", @"three", nil) map:^(NSString *item){
return [item uppercaseString];
}]
// returns sequence(@"ONE", @"TWO", @"THREE", nil)
[@[@"one", @[@"two"], @"three"] flatMap:^(NSString *item){
return [item uppercaseString];
}];
// returns array(@"ONE", @"TWO", @"THREE", nil)
过滤(Sequence,NSArray,NSSet,NSDictionary)
[sequence(@"1", @"12", @"123", @"1234", nil) filter:^(NSString *item){
return item.length > 2;
}]
//returns sequence(@"123", @"1234", nil)
选项
[Option option:@"something"];
//Outputs [Some some:@"something"];
[Option option:nil];
//Outputs [None none];
[[Option option:@"something"] map:^(NSString *item){
return [item uppercaseString];
}];
//Outputs [Some some:@"SOMETHING"];
[Option option:nil] map:^(NSString *item){
return [item uppercaseString];
}];
//Outputs [None none];
上面的示例仍然相当嘈杂。也存在简写语法。在框架导入上方包含以下内容:
#define TL_SHORTHAND
#define TL_LAMBDA
#define TL_LAMBDA_SHORTHAND
import <OCTotallyLazy/OCTotallyLazy.h>
然后你可以做一些有趣的事情,比如:
[sequence(num(1), num(2), num(3), nil) find:not(eqTo(num(1))]; //outputs [Some some:num(2)];
详尽
[sequence(@"bob", @"fred", @"wilma", nil) map:^(NSString *item){return [item uppercaseString];}] //outputs sequence(@"BOB", @"FRED", @"WILMA", nil)
更为合理
[sequence(@"bob", @"fred", @"wilma", nil) map:lambda(s, [s uppercaseString])] //outputs sequence(@"BOB", @"FRED", @"WILMA", nil)
有点疯狂(但有点像 scala)
[sequence(@"bob", @"fred", @"wilma", nil) map:_([_ uppercaseString])] //outputs sequence(@"BOB", @"FRED", @"WILMA", nil)
所以我有点厌烦使用那些说“只是将我们的源代码包含到您的项目中”或“将我们的 Xcode 项目附加到您的项目中”的“库”。因此,要使用这个