Skip to content

Commit eccae90

Browse files
committed
ListMap is reverse oriented, so use that for foldRight
1 parent c4011e6 commit eccae90

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

library/src/scala/collection/immutable/ListMap.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ object ListMap extends ImmutableMapFactory[ListMap] {
4444

4545
@SerialVersionUID(-8256686706655863282L)
4646
private object EmptyListMap extends ListMap[Any, Nothing]
47+
48+
@tailrec private def foldRightInternal[A, B, Z](map: ListMap[A, B], prevValue: Z, op: ((A, B), Z) => Z): Z = {
49+
if (map.isEmpty) prevValue
50+
else foldRightInternal(map.init, op(map.last, prevValue), op)
51+
}
4752
}
4853

4954
/**
@@ -127,6 +132,7 @@ sealed class ListMap[A, +B] extends AbstractMap[A, B]
127132
protected def value: B = throw new NoSuchElementException("value of empty map")
128133
protected def next: ListMap[A, B] = throw new NoSuchElementException("next of empty map")
129134

135+
override def foldRight[Z](z: Z)(op: ((A, B), Z) => Z): Z = ListMap.foldRightInternal(this, z, op)
130136
override def stringPrefix = "ListMap"
131137

132138
/**

0 commit comments

Comments
 (0)