美文网首页
函数式编程练习

函数式编程练习

作者: peerben | 来源:发表于2016-03-13 09:45 被阅读57次

问题描述
楼主的问题是,根据字典中某一个key进行排序,按照顺序输出结果,见下面代码:
PS:帖子中代码是不能运行的,因为product1的类型编译器推断不出来,必须显式指定为:[String: Any],跟帖中定义为[String: AnyObject] 也是错的。因为value都是值类型的,不能用AnyObject来表示。
// 输入

var product1: [String: Any] = ["name": "milk", "price": 3.2]
var product2: [String: Any] = ["name": "bread", "price": 2.9]
var product3: [String: Any] = ["name": "meat", "price": 4.1]
var product4: [String: Any] = ["name": "sweets", "price": 1.0]

// 输出

var priceArray = [1.0,2.9,3.2,4.1]
var nameArray = ["sweets","bread","milk","meat"]

问题解答
解法一:传统的思路来解决

let product11: [String: Any] = ["name": "milk","price": 3.2]
let product21: [String: Any] = ["name": "bread","price": 2.9]
let product31: [String: Any] = ["name": "meat","price": 4.1]
let product41: [String: Any] = ["name": "sweets", "price": 1.0]

var tempDictArray = [[String: Any]]()
tempDictArray.append(product11)
tempDictArray.append(product21)
tempDictArray.append(product31)
tempDictArray.append(product41)

func priceSort(dict1: [String: Any], dict2: [String: Any]) -> Bool {
    let price1 = dict1["price"] as? Float ?? 0
    let price2 = dict2["price"] as? Float ?? 0
    return price1 < price2
}

tempDictArray = tempDictArray.sort(priceSort)

var priceArray1 = [Float]()
var nameArray1 = [String]()

for item in tempDictArray {
    
    let price = item["price"] as? Float ?? 0
    let name = item["name"] as! String
    
    priceArray1.append(price)
    nameArray1.append(name)
}
priceArray1    //[1, 2.9, 3.2, 4.1]
nameArray1     //["sweets", "bread", "milk", "meat"]

解法二:推荐解法
1、使用 struct 替代字典,更加直观和自然

struct Product {
let name: String
let price: Double

    init?(dict:[String:Any]) {
        guard
            let name = dict["name"] as? String,
            let price = dict["price"] as? Double else {
                return nil
        }
        self.name = name
        self.price = price
    }
}

2、定义输入
let productsDict = [product11, product21, product3, product4]
3、排序得到结构体数组
let products = productsDict.flatMap { Product(dict: $0) }.sort{ $0.price < $1.price }
4、输出结果
let prices2 = products.map { $0.price }
let names2 = products.map { $0.name }
解法三:与第二种大同小异,有一些细节的差异,大家可以参考一下:
// Define a structure to hold your data.
// The protocol CustomStringConvertible allows you to format the
// data as a nice string for printing.

struct Product1: CustomStringConvertible {
    var name: String
    var price: Double
    var description: String { return "name: \(name), price: \(price)" }
    
    init?(info: [String: Any]) {
        guard let name = info["name"] as? String,
            let price = info["price"] as? Double else { return nil }
        self.name = name
        self.price = price
    }
}


var products1 = [Product1(info: product1), Product1(info: product2), Product1(info: product3), Product1(info: product4)].flatMap {$0}

products1.sortInPlace { $0.price < $1.price }

products1.forEach { print($0) }

let priceArray2 = products1.map { $0.price }  // [1.0, 2.9, 3.2, 4.1]
let nameArray2 = products1.map { $0.name }    // ["sweets", "bread", "milk", "meat"]

我的解法
var product1: [String: Any] = ["name": "milk", "price": 3.2]
var product2: [String: Any] = ["name": "bread", "price": 2.9]
var product3: [String: Any] = ["name": "meat", "price": 4.1]
var product4: [String: Any] = ["name": "sweets", "price": 1.0]

var products = [product1, product2, product3, product4]
let sortedProducts = products.sort { (A, B) -> Bool in
    let priceA = A["price"] as! Double
    let priceB = B["price"] as! Double
    
    return priceA < priceB
}

let priceArray = sortedProducts.map { (product) -> Double in
    return product["price"] as! Double
}
print(priceArray)

let nameArray = sortedProducts.map { (product) -> String in
    return product["name"] as! String
}
print(nameArray)

相关文章

  • RxSwift初探(1)

    一、前提:函数响应式编程思想 简单来说 函数响应式编程 = 函数式编程 + 响应式编程 (1)函数式 函数式编程是...

  • iOS 函数编程 & 链式编程

    函数式(链式)编程 函数式编程概念 函数式编程是种编程范式 函数式编程 Functional Programmin...

  • 函数式编程(一)—— 前置知识

    为什么要学函数式编程? 什么是函数式编程?函数式编程和面向对象编程的不同对于函数式编程思维方式的理解: 函数式编程...

  • 二十二、函数式编程、面向协议式编程

    函数式编程(Functional Prigramming) 函数式编程 函数式编程(Functional Prig...

  • Python中的函数式编程

    函数式编程 说到函数式编程,我们的重点在于为什么要采用函数式编程,而不是什么是函数式编程。 函数式编程的优点:一、...

  • RxSwift-初探

    函数响应式编程 一:函数式 函数式编程简称FP(Functional Programming),函数式编程就是一种...

  • 《Kotlin入门实战》CH5 | 函数与函数式编程

    函数与函数式编程 函数式编程与命令式编程最大的不同是:函数式编程的焦点在于数据的映射,命令式编程(imperati...

  • 函数响应式编程思想 & RxSwift 核心逻辑(一)

    函数响应式编程思想 函数响应式编程思想即是将函数式编程和响应式编程相结合。 函数式编程 顾名思义,就是像函数一样的...

  • RxJava系列|RxJava简介(一)

    函数响应式编程 函数式编程是一种编程范式。 常见的编程范式有:命令式编程、函数式编程和逻辑式编程。 面向对象就是一...

  • 函数式编程练习

    问题描述楼主的问题是,根据字典中某一个key进行排序,按照顺序输出结果,见下面代码:PS:帖子中代码是不能运行的,...

网友评论

      本文标题:函数式编程练习

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