aimozg
02/07/2017, 6:43 PMset.all { it.test }
to be equivalent to !set.any { !it.test }
in all cases. Found more information here: https://en.wikipedia.org/wiki/Vacuous_truth and http://math.stackexchange.com/questions/202452/why-is-predicate-all-as-in-allset-true-if-the-set-is-emptymarcinmoskala
02/17/2017, 6:28 PMvar columns = listOf<MutableList<EventRect>>()
for (eventRect in collisionGroup) {
val properColumn = columns.firstOrNull { column -> !isEventsCollide(eventRect.event, column[column.size - 1].event) }
if(properColumn != null) {
properColumn.add(eventRect)
} else {
columns = columns.plusElement(mutableListOf(eventRect))
}
}
is pretty common pattern. Like groupBy, but being in the group depend on other elements in this group. Like elements that are close enough to themselfazabost
02/22/2017, 10:27 AMclass EnumStringConverter<E extends Enum> implements Converter<E, String>
which I must provide for this annotation:
public @interface Convert {
Class<? extends Converter> value();
}
When I do it like this:
@get:Convert(EnumStringConverter::class)
I get:
Error:(95, 19) error: constructor EnumStringConverter in class EnumStringConverter<E> cannot be applied to given types;
required: Class
found: no arguments
reason: actual and formal argument lists differ in length
where E is a type-variable:
E extends Enum declared in class EnumStringConverter
And I don't know how to properly provide the type.
Could you help please?azabost
02/22/2017, 3:18 PMevanchooly
02/28/2017, 6:48 PMelizarov
03/05/2017, 7:22 AMelizarov
03/06/2017, 8:30 AMval list = … // some immutable list here
val updated = list.mutate {
add(”…”) // efficiently perform a series of mutating operations
add(”…”)
}
where mutate
is a helper function that “opens” and efficiently rebuilds immutable structure:
fun <T> List<T>.mutate(block: MutableList<T>.() -> Unit): List<T>
You should have a similar API for immutable data classes:
val person = Person(first = “John”, last = “Smith”) // immutable person object with lots of props
val updated = person.mutate {
age = 31
occuptation = “Manager”
}
It is considerably more readable than the solution with lenses.elizarov
03/06/2017, 10:15 AM::
to refer to functions and properties and you don’t have a do notation, and it would have been very inefficient, because you don’t have an advantage of purity to optimize back-to-back updates into one. However, Kotlin is an imperative language, so you should not be needing a do notation to start with, and you can directly express a sequence of updates to the the same object, without the need to rely on the smart optimizer to make it efficient.groostav
03/06/2017, 7:18 PMcopy
function is effectively a persister (read: local factory) function to make data classes persistent isn't it?
data class Thingy(val x: Double, val y: Int)
val first = Thingy(1.0, 42)
val second = first.copy(x = 2.0)
you dont get any sharing but you do get persistence.benleggiero
03/07/2017, 4:20 PMtreelzebub
03/15/2017, 8:15 PMinternal typealias
?
Error:(107, 91) Cannot access 'ValidationErrors': it is internal in ‘[module]’
usages are all also in [module]
kirillrakhman
03/24/2017, 10:38 AMbeholder
03/24/2017, 12:17 PMAndreas Sinz
03/25/2017, 4:50 PMlouiscad
03/28/2017, 11:50 AMval newList = list.minusIndex(i)
mg6maciej
03/28/2017, 12:04 PMmg6maciej
03/30/2017, 9:52 AMIndexedValue
or do I just call it .mapIndexed(::IndexedValue)
?orangy
03/31/2017, 1:09 PMsubList
is a view. If original list changes, then sublist also changes.ilya.gorbunov
04/03/2017, 2:52 PMcedric
04/03/2017, 8:58 PMcopyRecursively
is changing the target from a directory to a file:
fun main(args: Array<String>) {
val from = File("/tmp/foo").apply { writeText("I'm a file") }
val to = File("/tmp/to").apply {
deleteRecursively()
mkdirs()
}
from.copyRecursively(to, overwrite = true)
if (! to.isDirectory) {
throw AssertionError("Should be a directory")
}
}
miha-x64
04/06/2017, 7:47 PMQuadruple<A, B, C, D>
, Option<T>
, Either<L, R>
, fun Quadruple<*, *, *, *>.sendIntoOuterSpace()
and many others could be implemented... outside of stdlib. 🙃
@benleggiero why do you think Triple should have all that Pair has? For example, I ignore existence of both in most cases 😒imple_smile:
@mbstavola can you show any use-cases? I just wonder why do you need this.sreich
04/06/2017, 8:28 PMmg6maciej
04/07/2017, 7:04 AMlistOf(A, B)
=> listOf(emptyList(), listOf(A), listOf(B), listOf(A, B))
marcinmoskala
04/09/2017, 9:56 AMMapWithDefault
2) MapWithDefault
3) MapWithDefault
with default from secondDmitry Kandalov
04/12/2017, 9:45 PMfun Throwable.stackTrace(): String
in stdlib?
(e.g. https://github.com/JetBrains/kotlin/blob/0d1c803f71667ded0b313491d9e0c50511d63cbf/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/incremental/jvmUtils.kt#L6-L6)enleur
04/21/2017, 11:56 AMList<Pair<String, Double?>>
Map<String,Double>
pairs.filter { it.second != null }.toMap()
returns Map<String, Double?>
louiscad
04/22/2017, 7:51 PMMutableLazy
implementation is here: https://gist.github.com/LouisCAD/9b4ef46c12f1f6fe9b45658a8eff194cmg6maciej
04/23/2017, 7:40 PMpublic operator fun <T> Iterable<T>.minus(element: T): List<T>
be defined with element as nullable?
public operator fun <T> Iterable<T>.minus(element: T?): List<T>
I.e. if you call listOf<String>(...) - null as String?
it would just return the same List<String>
and not List<String?>
as it currently does.mg6maciej
04/23/2017, 10:20 PMArray<T>
?ilya.gorbunov
04/24/2017, 3:23 PMilya.gorbunov
04/24/2017, 3:23 PMmg6maciej
04/24/2017, 3:25 PMList
usage with this, but the API is not convinient. I even told you a while ago what issues I had 🙂kotlin.List
will work. It needs to be a separate List
interface like this one: https://github.com/vavr-io/vavr/blob/master/vavr/src/main/java/io/vavr/collection/List.javailya.gorbunov
04/24/2017, 3:28 PMmg6maciej
04/24/2017, 3:32 PMImmutableList
and then was hitting things like take
or drop
not defined on this interface.List
.List
as superinterface for ImmutableList
and then users would clearly see what's possible and what's not.ilya.gorbunov
04/24/2017, 3:39 PMmg6maciej
04/24/2017, 3:43 PMtake
on ImmutableList
simply needs to return ImmutableList
. There is no other option to connect it all with data class
copy and friends.toImmutableList()
after every such call.this.copy(someList = this.someList + elem)
or val (six, rest) = this.someList.splitAt(6); this.copy(someList = six)
, so all functions on ImmutableList
should return the same type.