Choosing the last element of a list

You can use last, which returns the last element or throws a NoSuchElementException, if the list is empty. scala> List(1, 2, 3).last res0: Int = 3 If you do not know if the list is empty or not, you may consider using lastOption, which returns an Option. scala> List().lastOption res1: Option[Nothing] = None scala> List(1, … Read more

Kotlin prepend element

I think the easiest would be to write: var list = listOf(2,3) println(list) // [2, 3] list = listOf(1) + list println(list) // [1, 2, 3] There is no specific tail implementation, but you can call .drop(1) to get the same. You can make this head\tail more generic by writing these extension properties: val <T> … Read more

Does Kotlin have an “enumerate” function like Python?

Iterations in Kotlin: Some Alternatives Like already said, forEachIndexed is a good way to iterate. Alternative 1 The extension function withIndex, defined for Iterable types, can be used in for-each: val ints = arrayListOf(1, 2, 3, 4, 5) for ((i, e) in ints.withIndex()) { println(“$i: $e”) } Alternative 2 The extension property indices is available … Read more

How to filter a list in-place with Kotlin?

Just use .retainAll { … } or .removeAll { … }, both accepting a predicate, to filter it in-place: items.retainAll { shouldRetain(it) } items.removeAll { shouldRemove(it) } Note that items should be a MutableList<T> for that, not just List<T>, which is a read-only list in Kotlin and thus does not expose any mutating functions (see: … Read more

Scala Partition/Collect Usage

collect (defined on TraversableLike and available in all subclasses) works with a collection and a PartialFunction. It also just so happens that a bunch of case clauses defined inside braces are a partial function (See section 8.5 of the Scala Language Specification [warning – PDF]) As in exception handling: try { … do something risky … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)