【翻訳】関数型プログラミング入門 | POSTD の「パイプラインを使う」をSwiftで書いてみた。
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 my_find<C : CollectionType where C.Generator.Element : Equatable>(domain: C, value: C.Generator.Element) -> C.Index? { | |
for idx in indices(domain) { | |
if domain[idx] == value { | |
return idx | |
} | |
} | |
return nil | |
} |
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 | |
import QuartzCore | |
func benchmark<T>(block: () -> T) -> String { | |
let measure = {(begin: CFTimeInterval) -> CFTimeInterval in block(); return CACurrentMediaTime() - begin} | |
let result = String(format: "Total Runtime: %g sec", measure(CACurrentMediaTime())) | |
println(result) | |
return result | |
} |
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 | |
func enumCast<C1, C2: RawRepresentable where C2.RawValue == C1>(rawValue: C1?) -> C2? { | |
if let rawValue = rawValue { | |
return C2(rawValue: rawValue) | |
} else { | |
return nil | |
} | |
} |
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
// 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) |
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 | |
func dates(#matchingComponents: NSDateComponents, var startDate: NSDate = NSDate(), endDate: NSDate = NSDate.distantFuture() as! NSDate) -> SequenceOf<NSDate> { | |
return SequenceOf<NSDate> { _ -> GeneratorOf<NSDate> in | |
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! | |
calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0) | |
return GeneratorOf<NSDate> { | |
if let nextDate = calendar.nextDateAfterDate(startDate, matchingComponents: matchingComponents, options: .MatchStrictly) | |
where nextDate.compare(endDate) != .OrderedDescending { | |
startDate = nextDate |
NSObjectのサブクラスを定義するスコープとdynamicの有無によるKVC/KVO挙動の違い
- ファイルスコープ
- dynamicなし、KVC使える、KVO使えない。
- dynamicあり、KVC/KVO使える。
- funcスコープ
- dynamicなし、KVC/KVO使えない。
- dynamicあり、KVC/KVO使える。
ファイルスコープはNSObjectではなく、そのサブクラスをサブクラス化してるような挙動。
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
class C { | |
func f<T>(v: T) -> String { | |
return "C(\(v))" | |
} | |
func callf() -> String{ | |
return f("test") | |
} | |
} |
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 | |
class C {} | |
// support own class without Printable | |
extension String { | |
init(stringInterpolationSegment expr: C) { | |
self.init(format: "C(%d)", ObjectIdentifier(expr).hashValue) | |
} | |
} |
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 | |
import XCTest | |
import Himotoki | |
class PerformanceTests: XCTestCase { | |
func testRecursion() { | |
let JSON = deepNestedJSON(0) | |
self.measureBlock { | |
var t: Test? = decode(JSON!) |