Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Created June 15, 2015 07:22
Show Gist options
  • Save joanmolinas/5b6607ead208d3ad447d to your computer and use it in GitHub Desktop.
Save joanmolinas/5b6607ead208d3ad447d to your computer and use it in GitHub Desktop.
//: ## Exercices from https://www.weheartswift.com/higher-order-functions-map-filter-reduce-and-more/
//: This are my solutions.
import UIKit
//: * 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(f(x))
func applyTwice(f : (Float -> Float), x: Float) -> Float {
return f(x)
}
let r = applyTwice({x in x + 1}, x: 55)
applyTwice({$0 + 1}, x: 55)
print(r)
//: * 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
func applyKTimes(f:(Float -> Float),x:Float,k:Int) -> Float {
return k > 0 ? applyKTimes(f, x: f(x), k: k - 1) : x
}
let r1 = applyKTimes({$0 + 1}, x: 1, k: 3)
print(r1)
//: * Using applyKTimes write a function that raises x to the kth power
func pow(x :Float, k: Int) -> Float {
return applyKTimes({x * $0}, x: 1.0, k: k)
}
print(pow(2, k: 4))
//: * 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
struct User{
var name : String?
var age : Int?
}
var users = [User]()
var u1 = User(name: "Joan", age: 20)
var u2 = User(name: "Cristina", age: 17)
var u3 = User(name: "Paula", age: 13)
var u4 = User(name: "Anna", age: 46)
users.append(u1)
users.append(u2)
users.append(u3)
users.append(u4)
let names = users.map({$0.name})
print(names)
//: * Given an array of of dictionaries containing keys for “name” and “age” write a map function that returns an array of users created from it
var dicts = [Dictionary<String, String>]()
dicts.append(["Age" : "20", "Name" : "Joan"])
dicts.append(["Age" : "17", "Name" : "Cristina"])
dicts.append(["Age" : "13", "Name" : "Paula"])
dicts.append(["Age" : "46", "Name" : "Anna"])
dicts.append(["Age" : "53", "Name" : "Joan"])
var usersFromDict = dicts.map({User(name: $0["Name"]!, age:Int($0["Age"]!))
})
print(usersFromDict)
//: * Given an array of numbers write a filter method that only selects odd integers
var numbers = [1,2,3,4,5,6,7,8,9,10,20,12,13,14,15,16,17,18,19,11]
var odds = numbers.filter({$0%2 != 0})
print(odds)
//: * Given an array of strings write a filter function that selects only strings that can be converted to Ints
var strings = [String]()
strings.append("1")
strings.append("abc")
strings.append("35")
strings.append("a343b")
var ints = strings.filter({Int($0) != nil})
print(ints)
//: * Given an array of UIViews write a filter function that selects only those views that are a subclass of UILabel
var v1 = UIView()
var v2 = UIView()
var v3 = UILabel()
var v4 = UIImageView()
var v5 = UILabel()
var views = [v1,v2,v3,v4,v5]
var labels = views.filter({$0.isKindOfClass(UILabel)})
print(labels)
//: * Write a reduce function that takes an array of strings and returns a single string consisting of the given strings separated by newlines
//: Use `strings` array, used in before exercice
var allInOneLine = strings.reduce(String(), combine: {"\($0)\n\($1)"})
print(allInOneLine)
//: * Write a reduce function that finds the largest element in an array of Ints
//: Use `numbers` array, used in before exercice
var maxInt = numbers.reduce(Int(), combine: {max($0 , $1)})
print(maxInt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment