Last active
June 4, 2016 06:10
-
-
Save to4iki/e3eb3375089cd02075993c57be27439a to your computer and use it in GitHub Desktop.
Implement array function used foldl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array { | |
var empty: Bool { | |
return count == 0 | |
} | |
var nonEmpty: Bool { | |
return !empty | |
} | |
var head: Element? { | |
return first | |
} | |
var tail: [Element]? { | |
guard nonEmpty else { return nil } | |
return Array(dropFirst()) | |
} | |
/// stack.push | |
func push(elem: Element) -> [Element] { | |
return self + [elem] | |
} | |
/// stack.pop | |
func pop() -> [Element] { | |
return Array(dropLast()) | |
} | |
/// queue.enqueue | |
func unshift(elem: Element) -> [Element] { | |
return [elem] + self | |
} | |
} | |
func foldl<A, B>(acc: A, xs: [B], f: (A, B) -> A) -> A { | |
if xs.empty { | |
return acc | |
} else { | |
return foldl(f(acc, xs.head!), xs: xs.tail!, f: f) | |
} | |
} | |
func sum(xs: [Int]) -> Int { | |
return foldl(0, xs: xs, f: (+)) | |
} | |
func maximum(xs: [Int]) -> Int { | |
return foldl(0, xs: xs, f: max) | |
} | |
func fac(n: Int) -> Int { | |
return foldl(1, xs: Array(1...n), f: (*)) | |
} | |
func reverse<T>(xs: [T]) -> Array<T> { | |
return foldl(Array<T>(), xs: xs) { (acc: [T], x: T) -> [T] in | |
acc.unshift(x) | |
} | |
} | |
func filter<T>(xs: [T], predicate: T -> Bool) -> [T] { | |
return foldl(Array<T>(), xs: xs) { (acc: [T], x: T) -> [T] in | |
if predicate(x) { | |
return acc.push(x) | |
} else { | |
return acc | |
} | |
} | |
} | |
func map<A, B>(xs: [A], f: A -> B) -> [B] { | |
return foldl(Array<B>(), xs: xs) { (acc: [B], x: A) -> [B] in | |
acc.push(f(x)) | |
} | |
} | |
func flatMap<A, B>(xs: [A], f: A -> [B]) -> [B] { | |
return foldl(Array<B>(), xs: xs) { (acc: [B], x: A) -> [B] in | |
acc + f(x) | |
} | |
} | |
/// ---- test | |
let arr = Array(1...5) | |
reverse(arr) | |
filter(arr) { | |
$0 % 2 == 0 | |
} | |
map(arr) { | |
"\($0)" | |
} | |
flatMap(arr) { (n: Int) -> [Int] in | |
[n, n+n] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func foldr<A, B>(xs: [A], acc: B, f: (A, B) -> B) -> B { | |
if xs.isEmpty { | |
return acc | |
} else { | |
return f(xs.head!, foldr(xs.tail!, acc: acc, f: f)) | |
} | |
} | |
foldl(0, xs: arr, f: +) | |
foldr(arr, acc: 0, f: +) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment