valfold = g1.fold(0)(MyNode.op)import scalax.collection.Graph
import scalax.collection.GraphEdge._
case class MyNode(id: Int, op: Function1[Int, Int])
case class MyEdge(
val nodeFrom: MyNode,
val nodeTo: MyNode
) extends DiEdge(NodeProduct(nodeFrom, nodeTo))
object App1 extends Scastie {
val f = (_: Int) + 1
val g = (_: Int) * 2
val h = (_: Int) - 3
val n1 = MyNode(1, f)
val n2 = MyNode(2, g)
val n3 = MyNode(3, h)
val e1 = MyEdge(n1, n2)
val e2 = MyEdge(n2, n3)
val nset = Set(n1, n2, n3)
val eset = Set(e1, e2)
val g1 = Graph.from(nset, eset)
println(g1.isAcyclic)
println(g1.isComplete)
val fold = g1.fold(0)(MyNode.op)
}
g1.nodes.foldLeft(0) { case (cum, n) => cum + n.op(???) }
I inserted ??? because it's not clear what you intend to pass to op.
val res = g1.nodes.foldLeft(0) { case (cum, n) => cum + n.op(1) }I'm not able to get this to compile with scala 2.12.10. I have version 1.13.1.
Val g1 had its type inferred as Graph[MyNode], GraphEdge.Diedge rather than MyEdge.
I'm not clear why though, as eset is a set of MyEdge.
The fold then refuses to compile because it doesn't find op.
Any ideas here? I'll try and upgrade to scala 13.x... But this may not be so easy in my project.
Then, I replaced tamplers fold line, with his final post.
The worksheet refuses to compile it.
If I insert the type annotation using intellij, it claims the type of n is:
GraphPredef.Param[MyNode, DiEdge].
I think its type should be MyNode.
Oh no! I realised I was trying to fold g1...
Not g1.nodes...
Oops - User issue :-)! Problem solved. Thanks for the quick response.