[Swift] Day04: exercise class, swiftday04 exercise class

Source: Internet
Author: User

[Swift] Day04: exercise class, swiftday04 exercise class
Exercise notes

Solve the last 13 Exercise questions in Higher Order Functions: Map, Filter, Reduce and more-Part 1 today, it's mainly about closures (maybe it's wrong. I obviously want to see the collection content ...).

Write a function applyTwice (f :( Float-> Float), x: Float)-> Float that takes a function f and a float x and aplies f to x twice I. e. f (x ))
func applyTwice(f:(Float -> Float),x:Float) -> Float {    return f(f(x))}
Write a function applyKTimes (f :( Float-> Float), x: Float, k: Int)-> Float that takes a function f and a float x and aplies f to x k times
// recursive versionfunc applyKTimes(f:(Float -> Float), x:Float, k:Int) -> Float{    return k > 0 ? applyKTimes(f, f(x), k - 1) : x}// unrolled by handfunc applyKTimes(f:(Float -> Float),x:Float,k:Int) -> Float {    var y : Float = x    for _ in 0..<k {        y = f(y)    }    return y}
Using applyKTimes write a function that raises x to the kth power
func getKthPower(x:Float, k:Int) -> Float{    return applyKTimes( {x * $0}, 1, k) // {x * $0} => {p in return x * p}}getKthPower(2.0, 3) // 8.0
Given an array of Users which have properties name: String and age: Int write a map function that returns an array of strings consisting of the user's names
class User {    var name: String?    var age : Int?    init (name: String, age:Int) {        self.name = name        self.age = age    }}var user1 = User(name: "WHY1", age: 22)var user2 = User(name: "WHY2", age: 23)var user3 = User(name: "WHY3", age: 24)var user4 = User(name: "WHY4", age: 25)var users = [user1,user2,user3,user4]var names: [String] = []users.map({(user:User) in names.append(user.name!)})println(names) // [WHY1, WHY2, WHY3, WHY4]
Given an array of dictionaries containing keys for "name" and "age" write a map function that returns an array of users created from it
var users = [["name":"WHY1","age":"22"],["name":"WHY2","age":"23"],             ["name":"WHY3","age":"24"],["name":"WHY4","age":"25"]]var result = users.map({ (userDic:[String:String]) -> User  in    return User(name: userDic["name"]!, age:userDic["age"]!.toInt()!)})
Given an array of numbers write a filter method that only selects odd integers
var nums = [1,2,4,8,23,45,89,127]var odds = nums.filter({ $0 % 2 == 0 }) // 2 4 8
Given an array of strings write a filter function that selects only strings that can be converted to Ints
var strs = ["2333","1223","callmewhy","callherhh"]var intables = strs.filter({ $0.toInt() != nil }) // ["2333", "1223"]
Given an array of UIViews write a filter function that selects only those views that are a subclass of UILabel
import UIKitvar view1 = UIView()var view2 = UIView()var view3 = UILabel()var view4 = UIView()var views = [view1,view2,view3,view4]var labels = views.filter({ $0.isKindOfClass(UILabel) }) // view3
Write a reduce function that takes an array of strings and returns a single string consisting of the given strings separated by newlines
var strs = ["str1","str2","str3","str4"]var str = strs.reduce("", combine: { "\($0)\n\($1)" })println(str)
Write a reduce function that finds the largest element in an array of Ints
var ints = [1,2,3,4,5,6]var maxValue = ints.reduce(Int.min, { max($0, $1) })    // 6
You cocould implement a mean function using the reduce operation {$0 + $1/Float (array. count)}. Why is this a bad idea?
var array = [1,2,3,4,6]var mean = array.reduce(0, combine: {$0 + Float($1) / Float(array.count)}) // make division 5 times
There's a problem you encounter when trying to implement a parallel version of reduce. What property shocould the operation have to make this easier?
// TODO
Implement Church Numerals in Swift (This is a difficult and open ended exercise)
// TODO
References
  • Higher Order Functions: Map, Filter, Reduce and more-Part 1
  • How to write a function applies f to x k times?
  • Raise x to the Kth power?
  • Correct way to find max in an Array in Swift
  • Swift equivalent for MIN and MAX macros

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.