Can I map multiple value? `flowNode.nextNodes.addA...
# getting-started
f
Can I map multiple value?
flowNode.nextNodes.addAll(target.outgoing.toList().map { it.target }
, I would like to return it.target and it.description can I write
flowNode.nextNodes.addAll(target.outgoing.toList().map { it.target,it.description }
?
k
Please surround your code with `.
You may be looking for
flatMap
.
f
mmm, I thought that flatMap is used to map multiple values for example multiple targets, but I would return target and description
k
What do you want the result to be? A list of pairs? A map? A list that contains both the `target`s and `description`s?
f
a list of pairs
(target,description)
k
In that case use
.map { it.target to it.description }
.
👍 1
f
thank you, so when I want to associate value inside a pair, I can use the infix function to
k
It's technically just a normal
infix
function, but yes.
f
😉 It’s my first day using Kotlin, sorry
k
No problem!
f
what do you thing about writing that kind of stuff :
val outgoingResult = target.outgoing.toList().map { it.target to it.extensionElements?.elementsQuery?.filterByType(CamundaInputOutput::class.java) }.map({ (it,flow) ->parseNode(it) to flow })
it’s seems to be hard to understand for someone else
what kind of coding style should I use? all on one line? or maybe split into variables
k
I'd put the
parseNode
in the original map, there's no reason for it to be separated.
The code itself looks fine to me, just format it correctly. I'd go with one of these:
Copy code
outgoingResult = target.outgoing.toList().map {
    parseNode(it.target) to it.extensionElements?.elementsQuery?.filterByType(CamundaInputOutput::class.java)
}

outgoingResult = target.outgoing.toList().map {
    Pair(
            parseNode(it.target),
            it.extensionElements?.elementsQuery?.filterByType(CamundaInputOutput::class.java)
    )
}
👍 1
f
yes 🙂 it’s better now
I need to find my style 🙂
I use this `it.extensionElements?.elementsQuery?.filterByType(CamundaInputOutput::class.java)?.list()?.map { it.camundaInputParameters?.map { it.textContent } }?.first()`multiple times, what is the best way to mutualise this part of code?
create a function?
or there is something better?
k
x?.let{ ... }
can help eliminate a bunch of those ?'s in that code.
You can also just inline maps,
.map{ f(it) }.map { g(x) }
is the same as
.map(g(f(it))
.
If you still want to create a function for it there are extension functions: https://kotlinlang.org/docs/reference/extensions.html
f
ok, for the let I can use only one let to replace all ?
I need to put it at the end?
k
Sure as long as the ?'s in your code all come from one of the first things being null.
f
thank you
.map { it.camundaInputParameters?.map { it.textContent } }
no seems to be similar to `.map { it.camundaInputParameters}?.map { it.textContent } ``
I don’t understand why
It should be the same no?
I have no compilation issue by adding a first between both `map { it.camundaInputParameters}?.first()?.map { it->it.textContent } ``
k
They are mostly the same, yes.
There may be some null-related differences but that just depends on where you place the ?'s.
👍 1