Hi, can one accomplish this ```flowNodes.forEach {...
# announcements
p
Hi, can one accomplish this
Copy code
flowNodes.forEach { initialNode ->
        initialNode.succeedingNodes.list().forEach { nextNode ->
            graph.addArc(initialNode to nextNode)
        }
    }
in a more fluent way? Seems like I'm missing something. The use case would be the following: I have an initial Node 1 with the succeeding nodes 2 and 3. The target is to add 1 to 2 and 1 to 3 to the graph. thanks a lot!
j
maps chain maybe?
and maybe a flatMap for list()
well the deep initialNode breaks the chain I want to do :/
p
The latter is exactly the problem where I also had to stop. 😞 Anyway, thanks for the input!
p
Maybe something like:
Copy code
flowNodes.flatMap { initialNode ->
  initialNode.succeedingNodes.list()
    .map { initialNode to it } 
}.forEach(graph::addArc)
I’m assuming that glowNodes is a list. It’s basically the same but allows to have two separate operations which means you can move things around more easily. If everything here under your control? Would be good to see more of the code
If you want something even easier to read:
Copy code
private fun Node.succeedinPairs() = 
  succeedingNodes.list().map { this to it } 

// then
flowNodes.flatMap(Node::succeedinPairs)
  .onEach(graph::addArc)
Always good to split things into smaller (and easier to understand) parts 🙂
p
I actually played around with it and had a comparable solution to your second one, @pablisco!
🎉 1
Main difference was that I was not using the method references. Looking at your solution, I should finally dig into it..
👍 1
p
Depending on your application you may want to use lambdas instead as method references can be (or used to) a bit heavier as they create an extra class