GHFunctionCode
根据python语言特点,编写的几个仿照python的api
list模块
list_t ----> NSArray *
mlist_t ----> NSMutableArray *
[目前支持的api]
- join(str)->str l
- append(id)->void m
- extend(list_t)->void m
- insert(id,int)->void m
- pop()->id m
- remove(id)->void m
- sort(BOOL(^)(id,id))->void m
- reverse()->void m
- swap(int,int)->void m
str模块
str_t ----> NSString *
c_str ----> const char *
[目前支持的api]
- split(str)->list
- replace(c_str,c_str)->str
- strip()->str
- lower()->str
- upper()->str
- startswith(c_str)->BOOL
- endswith(c_str)->BOOL
- encode(c_str)->NSData *
- find(c_str)->NSUInteger
- rfind(c_str)->NSUInteger
- title()->str
- splitlines(str)->list
dict模块
dict_t ----> NSDictionary *
mdict_t ----> NSMutableDictionary *
[目前支持的api]
- copy_()->dict_t d
- has_key(c_str)->BOOL d
- keys()->list_t d
- values()->list_t d
- items()->list_t d
- fromkeys(list_t,id)->dict_t d
- clear()->void m
- pop(c_str)->id m
- popitem()->list_t m
- update(dict_t)->void m
obj模块
obj_t ----> NSObject *
[目前支持的api]
- add(id)->id
- sub(id)->id
- mul(id)->id
- div(id)->id
- and(id)->id
- or(id)->id
- pow(int)->id
- constains(id)->BOOL
- is(id)->BOOL
- In(list_t)->BOOL
类型转换函数
- str(id)->str_t
- list(id)->list_t
- dict(id)->dict_t
- mlist(id)->mlist_t
- mdict(id)->mdict_t
集合通用函数
- sum(id)->id
- len(id)->NSUInteger
函数式
-
map( id(^)(id) ,list_t)->list_t
-
filter( BOOL(^)(id) ,list_t)->list_t
-
reduce( id(^)(id,id) ,list_t)->id
以及基本类型的 -
map_f_int
-
filter_f_int
-
reduce_f_int
-
map_f_float
-
filter_f_float
-
reduce_f_float
-
map_f_char
-
filter_f_char
-
reduce_f_char
-
map_f_double
-
filter_f_double
-
reduce_f_double
语法糖
- range(int,int,int)->list_t
-(void)testBaseType
{
int alen = 7;
int a[] = {100,10,1,30,22,21,9};
printf("------filter-------\n");
int r_size = 0;
int*b = filter_f_int(^BOOL(int value) {
return value > 10;
}, a, alen,&r_size);
for (int i = 0; i < r_size; ++i) {
printf("%d\n",b[i]);
}
free(b);
printf("------map-------\n");
b = map_f_int(^int(int value) {
return value+5;
}, a, alen);
for (int i = 0; i < alen; ++i) {
printf("%d\n",b[i]);
}
free(b);
printf("------reudce-------\n");
int c = reduce_f_int(^int(int value, int value2) {
return value*value2;
}, a, alen);
printf("%d\n",c);
}
-(void)testStr
{
str_t str = @"abcdef";
str = str.upper();
str = str.lower();
str = @"i have a dream";
str = str.title();
list_t list = str.split(" ");
NSLog(@"%@",list);
NSLog(@"%d",str.startswith("I"));
NSLog(@"%d",str.endswith("Dream"));
int pos = str.find("A");
str = @" abc ";
str = str.strip();
str = str.replace("a","x");
}
-(void)testList
{
list_t arrlist = @[@(1),@(2),@(3),@(4),@(5)];
NSLog(@"%@",sum(arrlist));
NSLog(@"%ld",len(arrlist));
str_t joinStr = arrlist.join(",");
NSLog(@"%@",joinStr);
mlist_t ml = mlist(arrlist);
ml.append(@(9));
ml.extend(arrlist);
ml.insert(@(22),1);
ml.remove(@(2));
ml.pop();
ml.reverse();
ml.sort(NULL);
ml.swap(3,5);
list_t a = @[@(1),@(2),@(3)];
list_t b = @[@(3),@(5),@(6)];
list_t c = a.add(b);
list_t d = a.sub(b);
list_t e = a.and(b);
list_t f = a.or(b);
}
-(void)testDict
{
dict_t d = dict(nil);
dict_t d2 = d.copy_();
d2 = @{@"a":@(5)};
BOOL haskey = d2.has_key("a");
list_t keys = d2.keys();
list_t values = d2.values();
list_t items = d2.items();
dict_t d3 = d2.fromkeys(@[@"d",@"e",@"f"],nil);
mdict_t md = mdict(nil);
md[@"hello"] = @"你好";
md[@"abc"] = @"唉比赛";
md[@"hey"] = @"嗨";
id value = md.pop("abc");
list_t l = md.popitem();
md[@"abc"] = @"唉比赛";
md[@"hey"] = @"嗨";
dict_t md2 = md.copy_();
md.clear();
md.update(md2);
}
-(void)testTypeTrans
{
list_t zip_arr = zip(@[@"a",@"b",@"c"],@[@(1),@(2),@(3)],nil);
NSLog(@"%@",zip_arr);
dict_t dict_value = dict(zip_arr);
NSLog(@"%@",dict_value);
str_t string = str(dict_value);
NSLog(@"%@",string);
list_t array = list(string);
NSLog(@"%@",array);
str_t oct_v = oct(57);
str_t hex_v = hex(57);
str_t bin_v = bin(57);
int x = ord('A');
char y = chr(x);
}
-(void)testRange{
for (number_t i in range(3)) {
NSLog(@"%@",i);
}
NSLog(@"\n");
for (number_t i in range(1,11)) {
NSLog(@"%@",i);
}
NSLog(@"\n");
for (number_t i in range(5,1,-1)) {
NSLog(@"%@",i);
}
NSLog(@"\n");
}
-(void)testfunctionCode
{
list_t arrlist = @[@(1),@(2),@(3),@(4),@(5)];
list_t maplist = map(^id(number_t obj) {
return [NSString stringWithFormat:@"file_name_%d",[obj intValue]];
}, arrlist);
NSLog(@"%@",maplist);
list_t filterlist = filter(^BOOL(number_t obj) {
return [obj intValue]%2;
}, arrlist);
NSLog(@"%@",filterlist);
id result = reduce(^id(id obj, id obj2) {
return [NSString stringWithFormat:@"%@*%@",obj,obj2];
}, arrlist);
NSLog(@"%@",result);
}
https://github.com/900116/GHFunctionCode