Skip to content

Instantly share code, notes, and snippets.

@norio-nomura
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norio-nomura/bc00e3ad6b4331c6a862 to your computer and use it in GitHub Desktop.
Save norio-nomura/bc00e3ad6b4331c6a862 to your computer and use it in GitHub Desktop.
Implementing flatMap() of Swift 1.2 beta 3 by myself
// Playground - noun: a place where people can play
import Foundation
// MARK: https://gist.github.com/norio-nomura/bc00e3ad6b4331c6a862
extension Array {
func myFlatMap<U>(@noescape transform: (T) -> [U]) -> [U] {
var r = Array<U>()
for e in self {
r += transform(e)
}
return r
}
}
extension ArraySlice {
func myFlatMap<U>(@noescape transform: (T) -> ArraySlice<U>) -> ArraySlice<U> {
var r = ArraySlice<U>()
for e in self {
r += transform(e)
}
return r
}
}
extension ContiguousArray {
func myFlatMap<U>(@noescape transform: (T) -> ContiguousArray<U>) -> ContiguousArray<U> {
var r = ContiguousArray<U>()
for e in self {
r += transform(e)
}
return r
}
}
extension ImplicitlyUnwrappedOptional {
func myFlatMap<U>(@noescape f: (T) -> U!) -> U! {
switch self {
case .None:
return nil
case .Some(let x):
return f(x)
}
}
}
extension Optional {
func myFlatMap<U>(@noescape f: (T) -> U?) -> U? {
switch self {
case .None:
return nil
case .Some(let x):
return f(x)
}
}
}
func myFlatMap<S : SequenceType, T>(source: S, @noescape transform : (S.Generator.Element) -> [T]) -> [T] {
var r = Array<T>()
for e in source {
r += transform(e)
}
return r
}
func myFlatMap<C : CollectionType, T>(source: C, transform: (C.Generator.Element) -> [T]) -> [T] {
var r = Array<T>()
for e in source {
r += transform(e)
}
return r
}
public func myFlatMap<T, U>(x: T?, @noescape f: (T) -> U?) -> U? {
if let x = x {
return f(x)
} else {
return nil
}
}
// MARK: http://swiftdoc.org/news/2015/03/updated-for-swift-1.2b3/
let numbers = [1, 2, 3, 4]
let expanded = numbers.myFlatMap { Array(count: $0, repeatedValue: $0) }
expanded.dynamicType
// MARK: http://airspeedvelocity.net/2015/03/13/changes-to-the-swift-standard-library-in-1-2-betas-2-and-3/
// an array of arrays
let arr = [[1,2,3],[4,5,6]]
// .first returns an optional of the first element of the array
// (optional because the array could be empty, in which case it's nil)
let fst = arr.first // fst is now [Int]?, an optional array of ints
// now, if we want to find the index of the value 2, we could use map and find
let idx = fst.map { find($0, 2) }
let idx2 = fst.myFlatMap { find($0, 2) }
let innerFst = fst.myFlatMap(first)
idx2.dynamicType
innerFst.dynamicType
// MARK: http://ericasadun.com/2015/03/12/xcode-6-3-wheeeee-beta-threeeeee/
var x = [["a", "b", "c"], ["d", "e"], ["f", "g", "h"]]
println(x.map{count($0)}) // returns [3, 2, 3]
println(x.myFlatMap {$0}) // returns [a, b, c, d, e, f, g, h]
println(x.myFlatMap {$0 + ["XX"]}) // returns [a, b, c, XX, d, e, XX, f, g, h, XX]
var optionalArray: [Int?] = [5, nil, 3, nil, 4, 6, 7, nil, nil, 8]
println(optionalArray.myFlatMap {(x : Int?) -> [Int] in
return x == nil ? [] : [x!]
}) // returns [5, 3, 4, 6, 7, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment