Skip to content
This repository has been archived by the owner on Jan 5, 2020. It is now read-only.

Commit

Permalink
Update sources with hash files
Browse files Browse the repository at this point in the history
  • Loading branch information
draveness committed Apr 7, 2017
1 parent e5a2612 commit ab578ad
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 34 deletions.
71 changes: 71 additions & 0 deletions Sources/Hash+Access.swift
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
}
}
14 changes: 7 additions & 7 deletions Sources/Hash+Bool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public extension Hash {
/// Returns `true` if the given key is present in hsh.
///
/// let hash = ["a": 100, "b": 200]
/// hash.hasKey("a") #=> true
/// hash.hasKey("c") #=> false
/// hash.has(key: "a") #=> true
/// hash.has(key: "c") #=> false
///
/// - Parameter key: A string represent key in hsh.
/// - Returns: A bool value.
func hasKey(_ key: Key) -> Bool {
func has(key: Key) -> Bool {
return reduce(false) { (result, element) in
return result || key == element.key
}
Expand All @@ -48,7 +48,7 @@ public extension Hash {
/// - Parameter key: A string represent key in hsh.
/// - Returns: A bool value.
func isMember(_ key: Key) -> Bool {
return hasKey(key)
return has(key: key)
}
}

Expand All @@ -57,12 +57,12 @@ public extension Hash where Value: Equatable {
/// Returns `true` if the given value is present in hsh.
///
/// let hash = ["a": 100, "b": 200]
/// hash.hasValue(100) #=> true
/// hash.hasValue(2000) #=> false
/// hash.has(value: 100) #=> true
/// hash.has(value: 2000) #=> false
///
/// - Parameter key: A string represent value in hsh.
/// - Returns: A bool value.
func hasValue(_ value: Value) -> Bool {
func has(value: Value) -> Bool {
return reduce(false) { (result, element) in
return result || value == element.value
}
Expand Down
63 changes: 63 additions & 0 deletions Sources/Hash+Enumeration.swift
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
}
}
24 changes: 0 additions & 24 deletions Sources/Hash+Mutate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,4 @@ import Foundation

// MARK: - Mutate
public extension Hash {
/// A mutating version of `Hash#merge(otherHash:closure:)`
///
/// let h1 = ["a": 100, "b": 200]
/// let h2 = ["b": 254, "c": 300]
///
/// h1.merge(h2) #=> ["a": 100, "b": 254, "c": 300]))
/// h1 #=> ["a": 100, "b": 254, "c": 300]))
///
/// - Parameters:
/// - otherHash: Another hash instance.
/// - closure: A closure returns a new value if duplicate happens.
/// - Returns: Self
@discardableResult mutating func merged(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value> {
self = self.merge(otherHash, closure: closure)
return self
}

/// Removes all key-value pairs from hsh.
///
/// - Returns: Self with empty hash.
@discardableResult mutating func clear() -> Hash<Key, Value> {
self = [:]
return self
}
}
74 changes: 74 additions & 0 deletions Sources/Hash+Transform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,78 @@ public extension Hash {
}
return map
}

/// An alias to `Hash#merge(otherHash:)` methods.
///
/// - Parameters:
/// - otherHash: Another hash instance.
/// - closure: A closure returns a new value if duplicate happens.
/// - Returns: A new hash containing the contents of both hash.
func update(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value> {
return merge(otherHash, closure: closure)
}

/// A mutating version of `Hash#merge(otherHash:closure:)`
///
/// let h1 = ["a": 100, "b": 200]
/// let h2 = ["b": 254, "c": 300]
///
/// h1.merge(h2) #=> ["a": 100, "b": 254, "c": 300]))
/// h1 #=> ["a": 100, "b": 254, "c": 300]))
///
/// - Parameters:
/// - otherHash: Another hash instance.
/// - closure: A closure returns a new value if duplicate happens.
/// - Returns: Self
@discardableResult mutating func merged(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value> {
self = self.merge(otherHash, closure: closure)
return self
}

/// An alias to `Hash#merged(otherHash:)` methods.
///
/// - Parameters:
/// - otherHash: Another hash instance.
/// - closure: A closure returns a new value if duplicate happens.
/// - Returns: Self
@discardableResult mutating func updated(_ otherHash: Hash<Key, Value>, closure: ((Key, Value, Value) -> Value)? = nil) -> Hash<Key, Value> {
return merged(otherHash, closure: closure)
}

/// Removes all key-value pairs from hsh.
///
/// var hash = ["a": 100]
/// hash.clear() #=> [:]
/// hash #=> [:]
///
/// - Returns: Self with empty hash.
@discardableResult mutating func clear() -> Hash<Key, Value> {
self = [:]
return self
}

/// An alias to `Hash#removeValue(forKey:)`.
///
/// var hash = ["a": 100, "b": 200]
/// hash.delete("a") #=> 100
/// hash #=> ["b": 200]
///
/// - Parameter key: A key of hash.
/// - Returns: Corresponding value or nil.
@discardableResult mutating func delete(_ key: Key) -> Value? {
return self.removeValue(forKey: key)
}

/// Replaces the contents of hsh with the contents of otherHash.
///
/// var hash = ["a": 100, "b": 200]
/// hash.replace(["c": 300]) #=> ["c": 300]
/// hash #=> ["c": 300]
///
/// - Parameter otherHash: Another hash instance.
/// - Returns: Self
@discardableResult mutating func replace(_ otherHash: Hash<Key, Value>) -> Hash<Key, Value> {
self = otherHash
return self
}
}
12 changes: 9 additions & 3 deletions Sources/Sequence+Enumeration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public extension Sequence {
/// }
///
/// - Parameter closure: An closure will passed to forEach method
func each(_ closure: (Iterator.Element) -> Void) {
/// - Returns: Self
@discardableResult func each(_ closure: (Iterator.Element) -> Void) -> Self {
forEach(closure)
return self
}

/// Iterate the receiver arrays with index which produced by a `for...in` and
Expand All @@ -29,10 +31,12 @@ public extension Sequence {
/// }
///
/// - Parameter closure: A closure which accepts a index and an element
func eachWithIndex(_ closure: (Int, Iterator.Element) -> Void) {
/// - Returns: Self
@discardableResult func eachWithIndex(_ closure: (Int, Iterator.Element) -> Void) -> Self {
for (index, item) in self.enumerated() {
closure(index, item)
}
return self
}

/// Iterate the receiver arrays with index which produced by a `for...in` and
Expand Down Expand Up @@ -62,7 +66,9 @@ public extension Sequence {
/// }
///
/// - Parameter closure: A closure will eventually passed to forEach method
func reverseEach(_ closure: (Iterator.Element) -> Void) {
/// - Returns: Self
@discardableResult func reverseEach(_ closure: (Iterator.Element) -> Void) -> Self {
self.reversed().each(closure)
return self
}
}

0 comments on commit ab578ad

Please sign in to comment.