Swift: Reduce with closure -
code:
var treasures: [treasure] = [] treasures = [treasurea, treasureb, treasurec, treasured, treasuree] let recttodisplay = self.treasures.reduce(mkmaprectnull) { (maprect: mkmaprect, treasure: treasure) -> mkmaprect in // 2 let treasurepointrect = mkmaprect(origin: treasure.location.mappoint, size: mkmapsize(width: 0, height: 0)) // 3 return mkmaprectunion(maprect, treasurepointrect) }
in code above, running reduce function on treasures
array, 2 parameters passed in closure: (maprect: mkmaprect, treasure: treasure)
. how closure know second parameter element treasures
array , first parameter result of closure returns?
is default second parameter passed in closure element array that's executing reduce function?
swift's array class has definition of reduce
looks this:
func reduce<t>(initial: t, fn: (t, t) -> t) -> t { var val = initial e in self { val = fn(val, e) } return e }
that say, definition of reduce
dictates order in parameters passed closure provide.
note actual definition of swift's reduce
more complicated 1 provided above, example above basic gist.
Comments
Post a Comment