2015-12-16 7 views
7

私はスウィフトのコードのこの行を持っている:目的のcでswiftからarray reduceという概念を使うことは可能ですか?

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

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

Objective Cのコードにこのコードの行を「翻訳」することは可能でしょうか?

コンバインのコンセプトがどのように機能するかは私にはあまり明らかではありません。私はそれについて読むが、まだ不明である。

私はこのチュートリアルからコードを取った:助けてくださいhttp://www.raywenderlich.com/90693/modern-core-graphics-with-swift-part-2

。ありがとう。

+0

どのタイプ 'graphPoints'配列を行います含む? –

+1

推測: 'average = [graphView.graphPoints valueForKeyPath:@" @ sum。 – Larme

+0

配列は:var graphPoints:[Int] = [4,2,6,4,5,8,3]、intオブジェクトを含んでいます。 – Adela

答えて

7

のは、あなたがこのKVCコレクション演算子を使用することができますNSArrayに保存されているいくつかのNSNumber秒を持っているとしましょう:

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

ありがとうございます:) – Adela

+0

あなたは完全に歓迎です:) –

1

reduce機能はObjective-Cのでは標準ではありません。あなたはNSArrayの拡張としてそれを実装することができます。

あなたの場合、SwiftにはIntの配列があります。 Objective-Cではこれを使用できません。NSNumberの配列が必要です。ここで

あなたのケースで動作するはずreduceの実装です:

@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 

あなたは、あなたの配列で、このような何かにそれを使用することができますにObjCに削減変換する方法

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

私は、 'reduce'はジェネリック型なしでは意味がないと思っています。 – Sulthan

+0

@Sulthanよく、' reduce'を ' id's、それはあなたのコードで使用するのに十分一般的です。もちろん、ジェネリックスを持つ方が良いでしょう。 –

0

を(あるいは、Objective Cの「平均的な問題」を解決する方法を伝えること)は、AndréSlottaによって完全に答えられました。迅速な削減はそれ以上のものです。私は、戻り結果を繰り返し 初期と自己の各要素に初期化累積値と組み合わせる呼び出しの 概念は迅速

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

をどのように機能するか、あなたの質問の後半部分に答えることをしようとします(自己(0)、 self [1])、... self [count-2])、self [count-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

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; 
} 

は、私が行ったすべての過ちを修正し、それはそれです。スウィフトより柔軟性が低いObjective-Cのために

0

、私は答えのこのリストに高階関数を追加します。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 

例:

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); 
関連する問題