adambl4
08/16/2016, 10:43 PMdstarcev
08/16/2016, 10:57 PMdstarcev
08/16/2016, 10:59 PMchristophsturm
08/17/2016, 11:07 AMbamdmux
08/24/2016, 8:42 AMbamdmux
08/24/2016, 8:43 AMfun <T> Collection<T>.splitPairs(): List<Pair<T, T>> = withIndex()
.partition { it.index % 2 == 0 }
.let { it.first.zip(it.second) { a, b -> a.value to b.value } }
bamdmux
08/24/2016, 8:43 AMcleiter
08/24/2016, 9:17 AMfun <T> List<T>.splitPairs2(): List<Pair<T, T>> =
mapIndexedNotNull { i, item ->
if (i % 2 == 0 && i < lastIndex) item to this[i + 1] else null
}
but i think your solution is fine for collectionsbamdmux
08/24/2016, 10:07 AMdeepanshu
08/28/2016, 11:35 AMval value = StringBuilder("Variable1: $var1\n" +
"Variable2: $var2\n" +
"size: ${myList.size}\n")
for (item in myList) {
val name = item.name
val type = item.type
val value = item.var1 - item.var2
value.append("\t$name : $type $value\n")
}
return value.toString()
When I decompile the .class
file this created, I see that there are too many StringBuilders being created. Any idiomatic way to avoid that?vmironov
08/28/2016, 11:40 AMStringBuilder
at the same time, e.g. value.append("\t$name : $type $value\n”)
may be replaced with:
value.append("\t")
value.append(name)
value.append(" : ")
value.append(type)
value.append(" ")
value.append(value)
value.append("\n”)
The code is a bit less readable, but it prevents the compiler from creating useless StringBuilder
objectsdeepanshu
08/28/2016, 11:53 AMStringBuilder
objects created in the loop, it also creates one StringBuilder
, each for the line VariableN: $varN\n
. Seems that concatenation and interpolation also don’t play nicelydanielgomezrico
09/01/2016, 3:18 AMval channelMap = mutableMapOf(
"title" to setup.channelTitle,
channelIdKey to setup.channelId)
val userChannelsMap = mutableMapOf<String, Any>(
channelIdKey to setup.channelId,
"title" to setup.channelTitle)
val messageMap = mutableMapOf(
"text" to text,
channelIdKey to setup.channelId,
"updatedTimeStamp" to ServerValue.TIMESTAMP)
val usersInfoMap = mapOf("videoOwnerName" to setup.ownerName,
"videoOwnerId" to setup.ownerId,
"requesterId" to setup.requesterId,
"requesterName" to setup.requesterName)
arrayOf(channelMap, userChannelsMap, messageMap).forEach { it.putAll(usersInfoMap) }
But I get:
MutableMap<String, Any> restricts the use of putAll(map)is there any way to do it?
danielgomezrico
09/01/2016, 3:18 AMcypher121
09/01/2016, 3:42 AMcleiter
09/01/2016, 7:21 AMbamdmux
09/01/2016, 7:59 AMcleiter
09/01/2016, 8:37 AMchannelIdKey
bamdmux
09/01/2016, 8:50 AMdanielgomezrico
09/01/2016, 2:52 PMMutableMap<String, Any>
danielgomezrico
09/01/2016, 2:52 PMdanielgomezrico
09/01/2016, 2:52 PMdanielgomezrico
09/01/2016, 2:52 PMjcomo
09/01/2016, 11:35 PMdrfriendless
09/03/2016, 1:43 PMoshai
09/17/2016, 8:07 PMoshai
09/17/2016, 8:08 PMoshai
09/17/2016, 8:08 PMvoddan
09/18/2016, 4:50 AMvoddan
09/18/2016, 4:56 AMcompanion
may have any name, not just $Companion
(which is the default)