SwiftImpl // Patch RangeReplaceableCollection.deduplicated().

This commit is contained in:
ShikiSuen 2023-12-18 12:16:47 +08:00
parent 1e0ec83dda
commit 1dcdc21411
1 changed files with 10 additions and 0 deletions

View File

@ -33,6 +33,16 @@ public extension StringLiteralType {
// Extend the RangeReplaceableCollection to allow it clean duplicated characters.
// Ref: https://stackoverflow.com/questions/25738817/
public extension RangeReplaceableCollection where Element: Hashable {
/// 使 NSOrderedSet class
var classDeduplicated: Self {
NSOrderedSet(array: Array(self)).compactMap { $0 as? Element.Type } as? Self ?? self
// Bug KeyValuePaired
// var set = Set<Element>()
// return filter { set.insert($0).inserted }
}
///
/// - Remark: class class Identifiable
var deduplicated: Self {
var set = Set<Element>()
return filter { set.insert($0).inserted }