美文网首页
Swift 基础 —— 关于数组操作

Swift 基础 —— 关于数组操作

作者: 落夏简叶 | 来源:发表于2018-10-29 15:52 被阅读4次
一、 自定义数组的累加操作,并返回一个数组。

和reduce类似,只不过reduce返回的是一个元素,而不是数组。

// reduce 用法
let nums = [1, 2, 3, 4]
let sumNums = nums.reduce(1, +) //11

  1. 首先自定义:
extension Array {
    /// 返回所有元素合并到一个数组中,保留合并时的每一步的值
    ///
    /// - Parameters:
    ///   - initialResult: 初始值
    ///   - nextParticalResult: 操作
    /// - Returns: 累加的过程数组
    func accumulate<Result>(_ initialResult: Result, _ nextParticalResult: (Result, Element) -> Result) -> [Result] {
        var running = initialResult
        return map { (next) in
            running = nextParticalResult(running, next)
            return running
        }
    }
}

  1. 使用
let nums = [1, 2, 3, 4]
let accumulateNums = nums.accumulate(1, +)  // [2, 4, 7, 11]

  1. nums.accumulate(1, +) 来源
// accumulate的第二个参数是一个闭包,所以可以写成尾随闭包形式。也可以不使用尾随闭包
let accumulateNums = nums.accumulate(1) { (num1, num2) -> Int in
            return num1 + num2
 }

// nums数组的操作后的返回类型可以自动推断,可以省略返回类型
let accumulateNums = nums.accumulate(1) { (num1, num2) in
            num1 + num2
}

// 使用默认参数
let accumulateNums = nums.accumulate(1) { $0 + $1 }

// 如果有两个参数,那么对这两个参数进行操作,可以省略掉参数。此时不能使用尾随闭包,比较清晰
let accumulateNums = nums.accumulate(1, +)
二、 检查数组内是否所有元素都满足某个条件
  1. 定义
/// 返回是否数组内所有元素满足条件
    ///
    /// - Parameter predicate: 条件
    /// - Returns: 是否数组内所有元素满足条件
    func all(matching predicate:(Element) -> Bool) -> Bool {
        return !contains { !predicate($0) }
    }

  1. 用法
 let nums = [1, 2, 3, 4]
 let isBiggerThan0 = nums.all { $0 > 0} // true
三、map函数用reduce的实现
  1. 实现方式

首先参考一下reduce的实现方式

func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> Result {
        var result = initialResult
        for x in self {
            result = nextPartialResult(Result, x)
        }
        return result
    }

再看map的实现就容易了。map 返回的是一个数组。即用一个初始为空的数组,每次加上数组中元素变换过的元素,返回的是一个数组。

func map2<T>(_ transform: (Element) -> T) ->[T] {
        return reduce([]) {
            $0 + [transform($1)]
        }
    }
// reduce 两个参数,一个初始值,一个闭包,闭包中有两个参数,累计的result 和 array中的element。
//  result = []
// for x in array
// result = result + [transfrom(x)]  // 这里$1 表示的是array中的元素
  1. 使用
let nums = [1, 2, 3, 4]
let reduceNum = nums.map2 { $0 * $0 }

相关文章

  • Swift 基础 —— 关于数组操作

    一、 自定义数组的累加操作,并返回一个数组。 和reduce类似,只不过reduce返回的是一个元素,而不是数组。...

  • 迟到的Swift入门 - 数组操作

    Swift数组操作 1. 数组的日常操作 1.0 声明数组 初始化空数组 初始化默认值的数组 2. 数组基本操作 ...

  • swift学习——基础语法

    一、swift基础语法 1.变量与常量定义 2.数据类型定义 3.字符串的操作 4.数组操作 5.字典操作

  • Swift中的数组

    最近开始学习总结关于Swift的知识,今天先从最基础的数组开始。对比OC的数组方法来学习,这样更容易对比学习 初始...

  • Swift 基础 —— 关于字典操作

    一、将自定义的键值对序列合并到字典中 二、用键值对序列创建字典

  • 关于swift数组

    今天在对swift数组进行操作的时候,发现swift数组中元素的拷贝竟是直接的地址拷贝!当时,我需要新建一个数组,...

  • swift 数组操作

    数组: *懒加载 *数组加载元素homeListArray.append(item)*数组遍历

  • swift 数组操作

    stride函数[https://www.jianshu.com/p/aa6d9d873b5a]

  • swift 数组操作

    1.便利数组 2.便利除了数组中第一个元素以外的元素,使用dropFirst 3.便利除了数组中后3个元素(根据需...

  • Swift5.x-基本的操作(中文文档)

    引言 继续学习Swift文档,从上一章节:基础,我们学习了Swift基础,现在,我们学习Swift的基本操作。这一...

网友评论

      本文标题:Swift 基础 —— 关于数组操作

      本文链接:https://www.haomeiwen.com/subject/bmibtqtx.html