josephivie
05/23/2019, 4:28 PMfun <T> Iterable<T>.forEachBetween(item: (T)->Unit, between: ()->Unit)
which runs the between block between each call of item, but doesn't get called at the very beginning or endelizarov
05/23/2019, 4:38 PMjosephivie
05/23/2019, 4:58 PMjoinToString, but I'm doing it with UI components. For example, placing dividers between components. Or for another example, when I'm writing something that emits large amounts of text using Appendable and want to directly use append instead of joining stuff to a string first.josephivie
05/23/2019, 5:01 PMelizarov
05/23/2019, 5:08 PMforEachBetween function?elizarov
05/23/2019, 5:09 PMjoinToString.josephivie
05/23/2019, 5:20 PM//UI Example
fun _LinearLayout.addButtons() {
val actions: List<Pair<String, () -> Unit>> = fromSomewhereElse()
actions.forEachBetween(
item = {
button(text = it.first) {
setOnClickListener { it.second() }
}.lparams(matchParent, wrapContent)
},
between = {
view { backgroundColor = Color.GRAY }.lparams(matchParent, 1)
}
)
}
//Query builder example
class SQLQuery(val stringBuilder: StringBuilder = StringBuilder(), val values: ArrayList<Any?> = ArrayList()): Appendable by stringBuilder {
fun addValue(value: Any?){
stringBuilder.append("\$" + values.size.toString())
values.add(value)
}
}
fun buildingQuery(){
val columns = listOf("a" to 1, "b" to 2, "c" to 3)
val query = SQLQuery()
columns.forEachBetween(
item = {
query.append("SET ${it.first} = ")
query.addValue(it.second)
},
between = {
query.append(", ")
}
)
}