Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #93 from hiltonc/master
Browse files Browse the repository at this point in the history
Ex.cached for 1-arg functions.
  • Loading branch information
pNre committed Mar 19, 2015
2 parents 87c8a9a + c1bd78f commit 6843fc5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
31 changes: 26 additions & 5 deletions ExSwift/ExSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ public class ExSwift {
}
}

/**
Creates a wrapper for function that caches the result of function's invocations.
:param: function Function with one parameter to cache
:returns: Wrapper function
*/
public class func cached <P: Hashable, R> (function: P -> R) -> (P -> R) {
var cache = [P:R]()

return { (param: P) -> R in
let key = param

if let cachedValue = cache[key] {
return cachedValue
} else {
let value = function(param)
cache[key] = value
return value
}
}
}

/**
Creates a wrapper for function that caches the result of function's invocations.
Expand All @@ -154,19 +176,18 @@ public class ExSwift {
var cache = [P:R]()

return { (params: P...) -> R in

let adaptedFunction = unsafeBitCast(function, Function.self)
let adaptedHash = unsafeBitCast(hash, Hash.self)

let key = adaptedHash(params)

if let cachedValue = cache[key] {
return cachedValue
} else {
let value = adaptedFunction(params)
cache[key] = value
return value
}

cache[key] = adaptedFunction(params)

return cache[key]!
}
}

Expand Down
25 changes: 25 additions & 0 deletions ExSwiftTests/ExSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ class ExSwiftTests: XCTestCase {
XCTAssertEqual(helloWorld(), "Hello World")
}

func testOneArgCached () {
var calls = 0

// Slow Fibonacci
var fib: ((Int) -> Int)!
fib = { (n: Int) -> Int in
calls++

if n <= 1 {
return n
}
return fib(n - 1) + fib(n - 2)
}

let fibonacci = Ex.cached(fib)

// This one is computed (fib is called 465 times)
fibonacci(12)
XCTAssertEqual(465, calls)

// The results is taken from the cache (fib is not called)
fibonacci(12)
XCTAssertEqual(465, calls)
}

func testCached () {
var calls = 0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Name | Signatures
**`once`**|`once <P, T> (function: P -> T) -> (P -> T?)`<br>`once <T> (call: Void -> T) -> (Void -> T?)`
**`partial`**|`partial <P, T> (function: (P...) -> T, _ parameters: P...) -> ((P...) -> T?)`
**`bind`**|`bind <P, T> (function: (P...) -> T, _ parameters: P...) -> (() -> T)`
**`cached`**|`cached <P, R> (function: (P...) -> R) -> ((P...) -> R)`<br>`cached <P, R> (function: (P...) -> R, hash: ((P...) -> P)) -> ((P...) -> R)`
**`cached`**|`cached <P, R> (function: P -> R) -> (P -> R)`<br>`cached <P, R> (function: (P...) -> R) -> ((P...) -> R)`<br>`cached <P, R> (function: (P...) -> R, hash: ((P...) -> P)) -> ((P...) -> R)`

#### Operators ####
Name | Signatures
Expand Down

0 comments on commit 6843fc5

Please sign in to comment.