Skip to content

Instantly share code, notes, and snippets.

@aziis98
Created December 6, 2016 21:34
Show Gist options
  • Save aziis98/7bd480f1e6efa4b7392afa8fe9ef276b to your computer and use it in GitHub Desktop.
Save aziis98/7bd480f1e6efa4b7392afa8fe9ef276b to your computer and use it in GitHub Desktop.
/**
* Created by aziis98 on 29/11/2016.
* Copyright 2016 Antonio De Lucreziis
*/
fun <T> split(source: List<T>, predicate: (T, T) -> Boolean): List<List<T>> {
fun _split(predicate: (T, T) -> Boolean, acc: List<List<T>>, source: List<T>): Pair<List<List<T>>, List<T>> {
if (acc.isEmpty()) {
val a = source[0]
val b = source[1]
val xs = source.subList(2, source.size)
return if (predicate(a, b)) {
listOf(listOf(a, b))
}
else {
listOf(listOf(a), listOf(b))
} to xs
}
else {
val (a, xs) = source.pairHead()
return if (predicate(acc.last().last(), a)) {
(acc.init() + listOf(acc.last() + listOf(a)))
}
else {
acc + listOf(listOf(a))
} to xs
}
}
fun __split(predicate: (T, T) -> Boolean, acc: List<List<T>>, source: List<T>): List<List<T>> =
if (source.isEmpty())
acc
else
_split(predicate, acc, source).let { (acc, source) -> __split(predicate, acc, source) }
return __split(predicate, listOf(), source)
}
fun stdGluer(a: Char, b: Char) = arrayOf(
a.isLetter() && b.isLetter(),
a.isDigit() && b.isDigit(),
a.isDigit() && b == '.',
a == '.' && b.isDigit()
).any { it }
fun tokenizeText(source: String, predicate: (Char, Char) -> Boolean = ::stdGluer): List<String> {
return split(source.toList(), predicate)
.map { String(it.toCharArray()) }
}
fun main(args: Array<String>) {
val test = "15.0 + 74 / 2"
println(tokenizeText(test).filter(String::isNotBlank))
}
fun <T> List<T>.init() = subList(0, size - 1)
fun <T> List<T>.tail() = subList(1, size)
fun <T> List<T>.pairHead() = first() to subList(1, size)
fun boolOr(vararg conditions: Boolean) = conditions.any()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment