This repository has been archived by the owner on Jan 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
draveness
committed
Apr 7, 2017
1 parent
e5a2612
commit ab578ad
Showing
6 changed files
with
224 additions
and
34 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Hash+Access.swift | ||
// RbSwift | ||
// | ||
// Created by Draveness on 07/04/2017. | ||
// Copyright © 2017 draveness. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - Access | ||
public extension Hash { | ||
/// Returns an array of optinal value with given keys. | ||
/// | ||
/// let hash = ["cat": "feline", "dog": "canine", "cow": "bovine"] | ||
/// hash.values(at: "cat", "dog") #=> ["feline", "canine"] | ||
/// hash.values(at: "catcat", "dog") #=> [nil, "canine"] | ||
/// | ||
/// - Parameter keys: An array of keys. | ||
/// - Returns: An optioanl value of array. | ||
func values(at keys: Key...) -> [Value?] { | ||
return values(at: keys) | ||
} | ||
|
||
/// Returns an array of optinal value with given keys. | ||
/// | ||
/// let hash = ["cat": "feline", "dog": "canine", "cow": "bovine"] | ||
/// hash.values(at: ["cat", "dog"]) #=> ["feline", "canine"] | ||
/// hash.values(at: ["catcat", "dog"]) #=> [nil, "canine"] | ||
/// | ||
/// - Parameter keys: An array of keys. | ||
/// - Returns: An optioanl value of array. | ||
func values(at keys: [Key]) -> [Value?] { | ||
return keys.map { self[$0] } | ||
} | ||
|
||
/// Searches through the hash with key using `subscript`. Returns the key-value tuple | ||
/// or nil if no match is found. | ||
/// | ||
/// let hash = [1: "one", 2: "two", 3: "three"] | ||
/// hash.assoc(1) #=> (1, "one") | ||
/// hash.assoc(42) #=> nil | ||
/// | ||
/// - Parameter key: An key of type `Key` | ||
/// - Returns: A key-value tuple or `nil`. | ||
func assoc(_ key: Key) -> (Key, Value)? { | ||
guard let value = self[key] else { return nil } | ||
return (key, value) | ||
} | ||
} | ||
|
||
// MARK: - Access | ||
public extension Hash where Value: Equatable { | ||
/// Searches through the hash with value using `subscript`. Returns the key-value tuple | ||
/// or nil if no match is found. | ||
/// | ||
/// let hash = [1: "one", 2: "two", 3: "three"] | ||
/// hash.rassoc("one") #=> (1, "one") | ||
/// hash.rassoc("forty-two") #=> nil | ||
/// | ||
/// - Parameter key: An key of type `Key` | ||
/// - Returns: A key-value tuple or `nil`. | ||
func rassoc(_ value: Value) -> (Key, Value)? { | ||
for (k, v) in self { | ||
if value == v { | ||
return (k, v) | ||
} | ||
} | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// Hash+Enumeration.swift | ||
// RbSwift | ||
// | ||
// Created by draveness on 07/04/2017. | ||
// Copyright © 2017 draveness. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - Enumeration | ||
public extension Hash { | ||
/// Calls block once for each key in hsh, passing the key-value pair as parameters. | ||
/// An alias to `Sequence#each(closure:)` method. | ||
/// | ||
/// let hash = ["a": 100, "b": 200] | ||
/// var result: [String: Int] = [:] | ||
/// hash.eachPair { | ||
/// result[$0] = $1 + 100 | ||
/// }) #=> ["a": 100, "b": 200] | ||
/// | ||
/// result #=> ["a": 200, "b": 300] | ||
/// | ||
/// - Parameter closure: An closure accepts key-value pair as parameters. | ||
/// - Returns: Self. | ||
@discardableResult func eachPair(closure: (Key, Value) -> Void) -> Hash<Key, Value> { | ||
return self.each(closure) | ||
} | ||
|
||
/// Calls block once for each key in hsh, passing the key as parameters. | ||
/// | ||
/// let hash = ["a": 100, "b": 200] | ||
/// var result: [String] = [] | ||
/// hash.eachKey { | ||
/// result.append($0) | ||
/// }) #=> ["a", "b"] | ||
/// | ||
/// result #=> ["a": 100, "b": 200] | ||
/// | ||
/// - Parameter closure: An closure accepts key as parameters. | ||
/// - Returns: Self. | ||
@discardableResult func eachKey(closure: (Key) -> Void) -> Hash<Key, Value> { | ||
self.keys.reverseEach(closure) | ||
return self | ||
} | ||
|
||
/// Calls block once for each key in hsh, passing the value as parameters. | ||
/// | ||
/// let hash = ["a": 100, "b": 200] | ||
/// var result: [Int] = [] | ||
/// hash.eachValue { | ||
/// result.append($0) | ||
/// }) #=> [100, 200] | ||
/// | ||
/// result #=> ["a": 100, "b": 200] | ||
/// | ||
/// - Parameter closure: An closure accepts value as parameters. | ||
/// - Returns: Self. | ||
@discardableResult func eachValue(closure: (Value) -> Void) -> Hash<Key, Value> { | ||
self.values.reverseEach(closure) | ||
return self | ||
} | ||
} |
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
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
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