|
1 | 1 | package stryker4s.extension |
2 | 2 |
|
3 | | -import cats.data.{Chain, State} |
| 3 | +import cats.Eq |
4 | 4 | import cats.syntax.all.* |
5 | | -import cats.{Eq, Eval} |
6 | 5 | import mutationtesting.Location |
7 | 6 | import mutationtesting.cats.* |
8 | 7 |
|
@@ -134,21 +133,19 @@ object TreeExtensions { |
134 | 133 | )(collectFn: PartialFunction[Tree, C => T]): Seq[T] = { |
135 | 134 | val collectFnLifted = collectFn.lift |
136 | 135 | val buildContextLifted = buildContext.lift |
137 | | - |
138 | | - def traverse(tree: Tree): State[Eval[Option[C]], Chain[T]] = |
139 | | - State.get[Eval[Option[C]]].flatMap { inherited => |
140 | | - // The context for this node and its descendants |
141 | | - val contextEval = Eval.defer(buildContextLifted(tree).fold(inherited)(_.some.pure[Eval])).memoize |
142 | | - |
143 | | - // Only read the context when this node actually collects something. |
144 | | - val collected = collectFnLifted(tree).foldMap(collect => Chain.fromOption(contextEval.value.map(collect))) |
145 | | - |
146 | | - // Each child starts from this node's context, independent of its siblings. |
147 | | - tree.children.foldMapM(child => State.set(contextEval) *> traverse(child)).map(collected ++ _) |
148 | | - } |
| 136 | + val builder = Vector.newBuilder[T] |
| 137 | + |
| 138 | + def traverse(tree: Tree, inherited: () => Option[C]): Unit = { |
| 139 | + // The context for this node and its descendants, only computed when a node actually collects something |
| 140 | + lazy val context: Option[C] = buildContextLifted(tree).orElse(inherited()) |
| 141 | + collectFnLifted(tree).foreach(collect => context.foreach(c => builder += collect(c))) |
| 142 | + val contextFn = () => context |
| 143 | + tree.children.foreach(traverse(_, contextFn)) |
| 144 | + } |
149 | 145 |
|
150 | 146 | // Traverse the tree, starting with an empty context |
151 | | - traverse(tree).runA(none.pure[Eval]).value.toVector |
| 147 | + traverse(tree, () => None) |
| 148 | + builder.result() |
152 | 149 | } |
153 | 150 |
|
154 | 151 | } |
|
0 commit comments