From 2a57e46310d51d3811453902e0cca5a25958753b Mon Sep 17 00:00:00 2001 From: shimesaba9 Date: Sat, 5 Dec 2015 00:35:37 +0900 Subject: [PATCH] Add uniq() to SequenceType --- stdlib/public/core/Sequence.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/stdlib/public/core/Sequence.swift b/stdlib/public/core/Sequence.swift index 70f35449c56b1..c0096af62d4ff 100644 --- a/stdlib/public/core/Sequence.swift +++ b/stdlib/public/core/Sequence.swift @@ -583,6 +583,18 @@ extension SequenceType where Generator.Element : Equatable { return split(maxSplit, allowEmptySlices: allowEmptySlices, isSeparator: { $0 == separator }) } + + /// Return an `Array` containing the elements of `self` + @warn_unused_result + public func uniq() -> [Generator.Element] { + return reduce(Array()) { (arr, element) -> [Generator.Element] in + if arr.contains({ $0 == element }) { return arr } + + var newArr = arr + newArr.append(element) + return newArr + } + } } extension SequenceType {