swift 函数 数组指针_swift应该具有的简单数组函数

swift 函数 数组指针

I really like Swift. It was my first real language and I am still with it today. But I feel like I write some functions in almost every project I do. So here are some of the extensions I think Swift should have(Array Edition).

我真的很喜欢Swift。 这是我的第一门真正的语言,而我至今仍在使用。 但是我觉得我几乎在我所做的每个项目中都编写了一些函数。 所以这是我认为Swift应该具有的一些扩展(数组版)。

All of these will be made for array’s of Double’s, but can easily be changed to any other number type.

所有这些都是针对Double的数组制作的,但可以轻松更改为任何其他数字类型。

So here is the first one: The average. I really don't know why this isn’t implemented. It is so simple and useful.

所以这是第一个:平均值。 我真的不知道为什么没有实现。 它是如此简单和有用。

extension Array where Element == Double {
    func average() -> Double {
        let sum = self.reduce(0, +)
        return sum/Double(self.count)
    }
}

And once we have the average we should also implement the median. It sorts the whole array and return:

一旦有了平均值,我们也应该采用中位数。 它对整个数组进行排序并返回:

Either the number at the count (-1 as array’s are zero-indexed) divided by 2, if the count is odd

如果计数为奇数,则将计数中的数字(-1作为数组的零索引)除以2

Or the average of the two numbers in the middle of the array, if the count is even. This is quite nice, because we can use our previously programmed average function.

如果计数为偶数,则为数组中间的两个数字的平均值。 这非常好,因为我们可以使用以前编程的平均值函数。

Here is the code:

这是代码:

extension Array where Element == Double {
    func median() -> Double {
        let sorted = self.sorted()
        if sorted.count % 2 == 0 {
            return [sorted[(sorted.count / 2)],sorted[(sorted.count / 2) - 1]].average()
        } else {
            return Double(sorted[(sorted.count - 1) / 2])
        }
    }
}

The last function is called the mode. It returns an array of the numbers that appear most often in the given array.

最后一个功能称为模式。 它返回最常出现在给定数组中的数字数组。

First we count the occurrences of all the numbers in the array and populate a dictionary where the number from the array is the key and the value is the number of occurrences of that number.

首先,我们计算数组中所有数字的出现次数,并填充一个字典,其中字典中的数字是键,而值是该数字出现的次数。

After that we get the highest number in the dictionarie’s values and return an array of all the keys whose value matches the max.

之后,我们获得字典值中最大的数字,并返回其值与最大值匹配的所有键的数组。

extension Array where Element == Double {
    func mode() -> [Double] {
        var counterDict : [Double: Int] = [:]
        for d in self {
            if let oldVal = counterDict[d] {
                counterDict[d] = oldVal + 1
            }else{
                counterDict[d] = 1
            }
        }
        var returnArray : [Double] = []
        if let max = counterDict.values.max() {
            for pair in counterDict {
                if pair.value == max {
                    returnArray.append(pair.key)
                }
            }
        }
        return returnArray
    }
}

These are only three, but there probably are so many more. I would love to hear some function’s you think Swift or other languages are missing.

这些只有三个,但可能还有更多。 我很想听听您认为缺少Swift或其他语言的某些功能。

翻译自: https://medium.com/swlh/simple-array-functions-swift-should-have-d9011a858c9e

swift 函数 数组指针

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值