2015-12-16 7 views
7

mam tej linii kodu SWIFT:Możliwe jest użycie koncepcji redukcji tablic z szybkiej w celu c?

let graphPoints:[Int] = [4, 2, 6, 4, 5, 8, 3] 

let average = graphPoints.reduce(0, combine: +)/graphPoints.count 

Możliwe jest „przetłumaczyć” tej linii kodu w Objective C kod?

Nie jest dla mnie jasne, jak działa koncepcja kombajnu redukcyjnego. Czytałem o tym, ale wciąż nie jest jasne.

wziąłem kod z tego poradnika: http://www.raywenderlich.com/90693/modern-core-graphics-with-swift-part-2

Proszę o pomoc. Dzięki.

+0

jaki typ robi 'tablicę graphPoints' zawierać? –

+1

Zgadnij: 'average = [graphView.graphPoints valueForKeyPath: @" @ sum.self "]' (lub może '@ avg' zamiast' @ sum'). – Larme

+0

array: var graphPoints: [Int] = [4, 2, 6, 4, 5, 8, 3], zawiera obiekty int. – Adela

Odpowiedz

7

powiedzmy masz jakieś NSNumber s przechowywane w NSArray można użyć tego operatora zbierania KVC:

NSArray *someNumbers = @[@0, @1.1, @2, @3.4, @5, @6.7]; 
NSNumber *average = [someNumbers valueForKeyPath:@"@avg.self"]; 
+0

Dziękuję. Działa. :) – Adela

+0

jesteś całkowicie mile widziany :) –

1

Funkcja reduce nie jest standardem w Objective-C. Możesz go jednak wdrożyć jako rozszerzenie NSArray.

W twoim przypadku masz tablicę Int w Swift. Nie możesz tego mieć w Objective-C, potrzebujesz tablicy NSNumber.

Oto implementacja reduce który powinien działać w Twoim przypadku:

@implementation NSArray (Helpers) 

- (NSInteger)reduceInt:(NSInteger)initial combine:(NSInteger (^)(NSInteger acum, NSInteger element))block { 
    if (!self) { 
     return initial; 
    } 

    NSInteger acum = initial; 
    for (id element in self) { 
     if ([element isKindOfClass:[NSNumber class]]) { 
      acum = block(acum, [(NSNumber *)element integerValue]); 
     } 
    } 

    return acum; 
} 

@end 

Można używać go następnie z tablicy, coś takiego:

NSArray *a = @[@1, @2, @3]; 
NSInteger result = [a reduceInt:0 combine:^NSInteger(NSInteger acum, NSInteger element) { 
    return acum + element; 
}]; 
+0

Myślę, że 'redu' nie ma wiele sensu bez generycznych typów. – Sulthan

+0

@Sulthan Cóż, możesz mieć implementację' reduce' z ' identyfikator jest na tyle ogólny, aby użyć go w swoim kodzie, oczywiście posiadanie generycznych byłoby lepsze. –

0

jak tłumaczyć zredukować do ObjC (lub lepiej powiedzieć, jak rozwiązać swój "średni problem" w Celu C), doskonale odpowiedział André Slotta. szybka redukcja to znacznie więcej. Postaram się odpowiedzieć na drugą część pytania, w jaki sposób koncepcja pracuje w szybkim

func reduce<T>(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows -> T 

zwraca wynik z wielokrotnie nazywając połączyć ze skumulowaną wartością inicjowane do początkowej i każdego elementu siebie, w obróć, czyli zwróć kombajn (połącz (... połącz (połącz (początkowy, własny [0]), własny [1]), ... własny [liczba-2]), własny [liczba-1]).

let arr: Array<Int> = [1,2,3,4,5] 
let sum = arr.reduce(0) { (sum, i) -> Int in 
    return sum + i 
} 
print(sum) // 15 

// this is an quasi equivalent of 
var sum1 = 0 // ..... reduce(0).... 
arr.forEach { (elementValue) -> Void in 
    sum1 = sum1 + elementValue // ...{ return sum + i } 
} 
print(sum1) // 15 reduce function will return accumulated inital value 

// reduce is part of SequenceType protocol, that is why 

let arr1 = ["H","e","l","l","o"," ","w","o","r","l","d"] 
let str = arr1.reduce("") { (str, s) -> String in 
    str + s 
} 
// works the same way 
print(str) // "Hello world" 

// let have a litle bit more complex example, to see how powerful, useful and easy to use reduce can be 
let dict = arr1.reduce([:]) { (var dict, s) -> Dictionary<Int,String> in 
    let i = dict.count 
    dict.updateValue(s, forKey: i+1) 
    return dict 
} 
print(dict) // [11: "d", 10: "l", 2: "e", 4: "l", 9: "r", 5: "o", 6: " ", 7: "w", 3: "l", 1: "H", 8: "o"] 
0

Napisz rozszerzenie NSArray

- (NSInteger)reduceStart:(NSInteger)start combine:(NSInteger(^)(NSInteger x, NSInteger y))combine { 
    for (NSNumber* n in self) { 
     if ([n isKindOfClass:[NSNumber class]]) { 
      start = combine (start, n.integerValue); 
     } 
    } 
    return start; 
} 

naprawić wszystkie błędy, które zrobiłem, i to wszystko. Po prostu mniej elastyczny niż Swift.

0

Dla Objective-C, chciałbym dodać wyższego rzędu-functions do tej listy odpowiada: https://github.com/fanpyi/Higher-Order-Functions

#import <Foundation/Foundation.h> 
typedef id (^ReduceBlock)(id accumulator,id item); 
@interface NSArray (HigherOrderFunctions) 
-(id)reduce:(id)initial combine:(ReduceBlock)combine; 
@end 

#import "NSArray+HigherOrderFunctions.h" 
@implementation NSArray (HigherOrderFunctions) 
-(id)reduce:(id)initial combine:(ReduceBlock)combine{ 
    id accumulator = initial; 
    for (id item in self) { 
     accumulator = combine(accumulator, item); 
    } 
    return accumulator; 
} 
@end 

przykład:

NSArray *numbers = @[@5,@7,@3,@8]; 
    NSNumber *sum = [numbers reduce:@0 combine:^id(id accumulator, id item) { 
     return @([item intValue] + [accumulator intValue]); 
    }]; 
    NSNumber *multiplier = [numbers reduce:@1 combine:^id(id accumulator, id item) { 
     return @([item intValue] * [accumulator intValue]); 
    }]; 
    NSLog(@"sum=%@,multiplier=%@",sum,multiplier); 
Powiązane problemy