Created
May 1, 2020 06:45
-
-
Save dneprDroid/99c0c2a85073abeb91a7192556651393 to your computer and use it in GitHub Desktop.
BehaviorRelay+Collection.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
extension BehaviorRelay where Element: RangeReplaceableCollection & MutableCollection, Element.Index == Int { | |
func append(element: Element.Element) { | |
var array = self.value | |
array.append(element) | |
self.accept(array) | |
} | |
func append<S: Sequence>(contentsOf collection: S) where S.Element == Element.Element { | |
var array = self.value | |
array.append(contentsOf: collection) | |
self.accept(array) | |
} | |
func changeElement(at index: Element.Index, _ clb: (inout Element.Element) -> Void) { | |
var array = self.value | |
clb(&array[index]) | |
self.accept(array) | |
} | |
func changeElement(where finder: (Element.Element) -> Bool, | |
_ clb: (inout Element.Element) -> Void) { | |
var i: Element.Index? | |
for (index, element) in self.value.enumerated() { | |
if finder(element) { | |
i = index | |
break | |
} | |
} | |
guard let index = i else { return } | |
self.changeElement(at: index, clb) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment