码迷,mamicode.com
首页 > 移动开发 > 详细

ios swift reduce Method

时间:2014-07-01 00:27:22      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:des   http   width   2014   os   art   

Swift’s API includes many functions and instance methods that reflect its functional programming heritage. A prime example is called reduce.  You can reduce a collection of values down to just one value, such as calculating the sum of every person’s age in an array of Person objects. Using reduce eliminates the need for writing many loops in a program. Here is an annotated code listing that shows a loop and the equivalent usage of reduce to add together the age of every person in an array.

bubuko.com,布布扣

There are three matching parts between the two code snippets. The green box in both snippets shows how an initial value is defined, in this case zero. The yellow underline shows how each approach iterates the array of Person objects. The red underline is where the sum is accumulated. In the reduce code, the first closure argument ($0) is the running total, which starts at zero, and the second parameter ($1) references a Person object in the people array.

ios swift reduce Method,布布扣,bubuko.com

ios swift reduce Method

标签:des   http   width   2014   os   art   

原文地址:http://www.cnblogs.com/tinkl/p/3816708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!