Last active
February 23, 2019 13:16
-
-
Save arnyigor/7ff4d4e630801de28a2bcbe3847f1c54 to your computer and use it in GitHub Desktop.
Custom utils
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
/** | |
* return items from second array wich not includes in first by custom diff by predicate | |
* @param first ArrayList of T | |
* @param second Collection of T | |
* @param predicate function equals | |
*/ | |
fun <T> arraysDiff(first: ArrayList<T>, second: ArrayList<T>, predicate: (firstOf:T, secondOf:T) -> Boolean): ArrayList<T> { | |
val result = arrayListOf<T>() | |
for (f in first) { | |
var equal = false | |
for (s in second) { | |
if (predicate.invoke(f, s)) { | |
equal = true | |
break | |
} | |
} | |
if (!equal) { | |
result.add(f) | |
} | |
} | |
return result | |
} | |
/** | |
* Универсальная функция окончаний | |
* @param [count] число | |
* @param [zero_other] слово с окончанием значения [count] либо ноль,либо все остальные варианты включая от 11 до 19 (слов) | |
* @param [one] слово с окончанием значения [count]=1 (слово) | |
* @param [two_four] слово с окончанием значения [count]=2,3,4 (слова) | |
*/ | |
fun getTermination(count: Int, zero_other: String, one: String, two_four: String, concat: Boolean = true): String { | |
if (count % 100 in 11..19) { | |
return if (concat) "$count $zero_other" else " $zero_other" | |
} | |
return when (count % 10) { | |
1 -> if (concat) "$count $one" else one | |
2, 3, 4 -> if (concat) "$count $two_four" else two_four | |
else -> if (concat) "$count $zero_other" else zero_other | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment