From 58a58741c69e33b6edc5d3710434ad53f06ac0be Mon Sep 17 00:00:00 2001 From: Tomasz Szulc Date: Sat, 1 Aug 2015 15:10:53 +0200 Subject: [PATCH] Create operator for getting unique values of two arrays --- .../SharedExpressionsProcessor.swift | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/Swifternalization/SharedExpressionsProcessor.swift b/Swifternalization/SharedExpressionsProcessor.swift index 696fae8..c132939 100644 --- a/Swifternalization/SharedExpressionsProcessor.swift +++ b/Swifternalization/SharedExpressionsProcessor.swift @@ -38,7 +38,6 @@ class SharedExpressionsProcessor { :param: preferedLanguageExpressions Expressions from expressions.json :param: baseLanguageExpressions Expressions from base section of expression.json. - :returns: array of shared expressions for Base and preferred language. */ class func processSharedExpression(preferedLanguage: CountryCode, preferedLanguageExpressions: [SharedExpression], baseLanguageExpressions: [SharedExpression]) -> [SharedExpression] { @@ -53,25 +52,15 @@ class SharedExpressionsProcessor { and this is good as base expression. 2. He forgot to define such expression for prefered language. */ - var uniqueBaseExpressions = baseLanguageExpressions - if preferedLanguageExpressions.count > 0 { - uniqueBaseExpressions = baseLanguageExpressions.filter({ - let pref = $0 - return preferedLanguageExpressions.filter({$0.identifier == pref.identifier}).count == 0 - }) - } + let uniqueBaseExpressions = baseLanguageExpressions [SharedExpression] { + /* + "Get Unique" operator. It helps in getting unique shared expressions from two arrays. + Content of `lhs` array will be checked in terms on uniqueness. The operator does + check is there is any shared expression in `lhs` that is presented in `rhs`. + If element from `lhs` is not in `rhs` then this element is correct and is returned + in new array which is a result of this operation. + */ + var result = lhs + if rhs.count > 0 { + result = lhs.filter({ + let tmp = $0 + return rhs.filter({$0.identifier == tmp.identifier}).count == 0 + }) + } + return result +}