@effekt-lang/effekt 0.75.0 → 0.77.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/effekt CHANGED
Binary file
@@ -347,9 +347,10 @@ extern def infixStar(x: Int, y: Int) at {}: Int =
347
347
  chez "(* ${x} ${y})"
348
348
  llvm "%z = mul %Int ${x}, ${y} ret %Int %z"
349
349
 
350
+ /// Truncating signed integer division (quotient rounded towards zero)
350
351
  extern def infixSlash(x: Int, y: Int) at {}: Int =
351
- js "Math.floor(${x} / ${y})"
352
- chez "(floor (/ ${x} ${y}))"
352
+ js "Math.trunc(${x} / ${y})"
353
+ chez "(quotient ${x} ${y})"
353
354
  llvm "%z = sdiv %Int ${x}, ${y} ret %Int %z"
354
355
 
355
356
  extern def infixMinus(x: Int, y: Int) at {}: Int =
@@ -8,7 +8,7 @@ record Map[K, V](tree: internal::Tree[K, V], compare: (K, K) => Ordering at {})
8
8
  /// Create a new empty map using a pure, first-class comparison function.
9
9
  ///
10
10
  /// O(1)
11
- def empty[K, V](compare: (K, K) => Ordering at {}): Map[K, V] =
11
+ def empty[K, V](?compare: (K, K) => Ordering at {}): Map[K, V] =
12
12
  Map(internal::empty(), compare)
13
13
 
14
14
  /// Create a new empty map using a generic comparison function.
@@ -34,7 +34,7 @@ def nonEmpty[K, V](m: Map[K, V]): Bool = internal::nonEmpty(m.tree)
34
34
  /// Create a new map containing the mapping from `k` to `v` and a pure, first-class comparison function.
35
35
  ///
36
36
  /// O(1)
37
- def singleton[K, V](k: K, v: V, compare: (K, K) => Ordering at {}): Map[K, V] =
37
+ def singleton[K, V](k: K, v: V, ?compare: (K, K) => Ordering at {}): Map[K, V] =
38
38
  Map(internal::singleton(k, v), compare)
39
39
 
40
40
  /// Create a new map containing the mapping from `k` to `v` using a generic comparison function.
@@ -91,7 +91,7 @@ def getOrElse[K, V](m: Map[K, V], k: K) { default: => V }: V =
91
91
  def contains[K, V](m: Map[K, V], k: K): Bool =
92
92
  internal::get(m.tree, m.compare, k) match {
93
93
  case None() => false
94
- case Some(v) => true
94
+ case Some(_) => true
95
95
  }
96
96
 
97
97
  /// Get minimum in the map `m`.
@@ -156,15 +156,26 @@ def filter[K, V](m: Map[K, V]) { shouldKeep: V => Bool }: Map[K, V] =
156
156
  def foreach[K, V](m: Map[K, V]) { action: (K, V) => Unit }: Unit =
157
157
  internal::foreach(m.tree) {action}
158
158
 
159
+ /// Traverse all keys and their associated values in map `m` in *reverse*order,
160
+ /// running the function `action` on a key and its associated value.
161
+ ///
162
+ /// Law: `m.foreachReversed { action } === m.toList.reverse.foreach { action }`
163
+ ///
164
+ /// O(N)
165
+ ///
166
+ /// TODO: Support {Control} for early exits.
167
+ def foreachReversed[K, V](m: Map[K, V]) { action: (K, V) => Unit }: Unit =
168
+ internal::foreachReversed(m.tree) {action}
169
+
159
170
  /// Convert a map `m` into a list of (key, value) pairs.
160
171
  ///
161
172
  /// O(N)
162
173
  def toList[K, V](m: Map[K, V]): List[(K, V)] = {
163
174
  var acc = Nil()
164
- m.foreach { (k, v) =>
175
+ m.foreachReversed { (k, v) =>
165
176
  acc = Cons((k, v), acc)
166
177
  }
167
- acc.reverse
178
+ acc
168
179
  }
169
180
 
170
181
  /// Get a list of keys of the map `m`.
@@ -172,10 +183,10 @@ def toList[K, V](m: Map[K, V]): List[(K, V)] = {
172
183
  /// O(N)
173
184
  def keys[K, V](m: Map[K, V]): List[K] = {
174
185
  var acc = Nil()
175
- m.foreach { (k, _v) =>
186
+ m.foreachReversed { (k, _v) =>
176
187
  acc = Cons(k, acc)
177
188
  }
178
- acc.reverse
189
+ acc
179
190
  }
180
191
 
181
192
  /// Get a list of values of the map `m`.
@@ -183,10 +194,10 @@ def keys[K, V](m: Map[K, V]): List[K] = {
183
194
  /// O(N)
184
195
  def values[K, V](m: Map[K, V]): List[V] = {
185
196
  var acc = Nil()
186
- m.foreach { (_k, v) =>
197
+ m.foreachReversed { (_k, v) =>
187
198
  acc = Cons(v, acc)
188
199
  }
189
- acc.reverse
200
+ acc
190
201
  }
191
202
 
192
203
  /// Create a map from a list of (key, value) pairs and a pure, first-class comparison function.
@@ -195,7 +206,7 @@ def values[K, V](m: Map[K, V]): List[V] = {
195
206
  ///
196
207
  /// O(N) if the list is sorted by key,
197
208
  /// O(N log N) otherwise
198
- def fromList[K, V](pairs: List[(K, V)], compare: (K, K) => Ordering at {}): Map[K, V] =
209
+ def fromList[K, V](pairs: List[(K, V)], ?compare: (K, K) => Ordering at {}): Map[K, V] =
199
210
  Map(internal::fromList(pairs, compare), compare)
200
211
 
201
212
  /// Create a map from a list of (key, value) pairs and a generic comparison function.
@@ -253,7 +264,7 @@ def getIndex[K, V](m: Map[K, V], n: Int): Option[(K, V)] =
253
264
  /// Uses an explicit pure, first-class comparison function.
254
265
  ///
255
266
  /// O(???)
256
- def difference[K, V](m1: Map[K, V], m2: Map[K, V], compare: (K, K) => Ordering at {}) = {
267
+ def difference[K, V](m1: Map[K, V], m2: Map[K, V], ?compare: (K, K) => Ordering at {}) = {
257
268
  val newTree = internal::difference(m1.tree, m2.tree, compare)
258
269
  Map(newTree, compare)
259
270
  }
@@ -273,7 +284,7 @@ def difference[K, V](m1: Map[K, V], m2: Map[K, V]) = {
273
284
  /// Uses an explicit pure, first-class comparison function.
274
285
  ///
275
286
  /// O(???)
276
- def union[K, V](m1: Map[K, V], m2: Map[K, V], compare: (K, K) => Ordering at {}) { combine: (K, V, V) => V }: Map[K, V] = {
287
+ def union[K, V](m1: Map[K, V], m2: Map[K, V], ?compare: (K, K) => Ordering at {}) { combine: (K, V, V) => V }: Map[K, V] = {
277
288
  val newTree = internal::union(m1.tree, m2.tree, compare) {combine}
278
289
  Map(newTree, compare)
279
290
  }
@@ -283,7 +294,7 @@ def union[K, V](m1: Map[K, V], m2: Map[K, V], compare: (K, K) => Ordering at {})
283
294
  /// Uses an explicit pure, first-class comparison function.
284
295
  ///
285
296
  /// O(???)
286
- def union[K, V](m1: Map[K, V], m2: Map[K, V], compare: (K, K) => Ordering at {}) { combine: (V, V) => V }: Map[K, V] =
297
+ def union[K, V](m1: Map[K, V], m2: Map[K, V], ?compare: (K, K) => Ordering at {}) { combine: (V, V) => V }: Map[K, V] =
287
298
  union(m1, m2, compare) { (k, v1, v2) => combine(v1, v2) }
288
299
 
289
300
  /// Construct a new map which contains the elements of both `m1` and `m2`.
@@ -297,7 +308,7 @@ def union[K, V](m1: Map[K, V], m2: Map[K, V]): Map[K, V] =
297
308
  /// Construct a new map which combines all elements that are in both `m1` and `m2` using the `combine` function.
298
309
  ///
299
310
  /// O(???)
300
- def intersection[K, A, B, C](m1: Map[K, A], m2: Map[K, B], compare: (K, K) => Ordering at {}) { combine: (K, A, B) => C }: Map[K, C] = {
311
+ def intersection[K, A, B, C](m1: Map[K, A], m2: Map[K, B], ?compare: (K, K) => Ordering at {}) { combine: (K, A, B) => C }: Map[K, C] = {
301
312
  val newTree = internal::intersection(m1.tree, m2.tree, compare) {combine}
302
313
  Map(newTree, compare)
303
314
  }
@@ -305,7 +316,7 @@ def intersection[K, A, B, C](m1: Map[K, A], m2: Map[K, B], compare: (K, K) => Or
305
316
  /// Construct a new map which combines all elements that are in both `m1` and `m2` using the `combine` function.
306
317
  ///
307
318
  /// O(???)
308
- def intersection[K, A, B, C](m1: Map[K, A], m2: Map[K, B], compare: (K, K) => Ordering at {}) { combine: (A, B) => C }: Map[K, C] =
319
+ def intersection[K, A, B, C](m1: Map[K, A], m2: Map[K, B], ?compare: (K, K) => Ordering at {}) { combine: (A, B) => C }: Map[K, C] =
309
320
  m1.intersection[K, A, B, C](m2, compare) { (k, v1, v2) => combine(v1, v2) }
310
321
 
311
322
  /// Construct a new map which combines all elements that are in both `m1` and `m2`.
@@ -315,6 +326,47 @@ def intersection[K, A, B, C](m1: Map[K, A], m2: Map[K, B], compare: (K, K) => Or
315
326
  def intersection[K, A, B](m1: Map[K, A], m2: Map[K, B]): Map[K, A] =
316
327
  m1.intersection[K, A, B, A](m2, m1.compare) { (k, v1, v2) => v1 }
317
328
 
329
+
330
+
331
+ // Streaming
332
+ // ---------
333
+
334
+ /// Turns a `map` into a producer of a push stream
335
+ /// of `(key, value)` pairs by emitting each contained *in order*.
336
+ def each[K, V](map: Map[K, V]): Unit / emit[(K, V)] =
337
+ map.foreach { (k, v) => do emit((k, v)) }
338
+
339
+ /// Turns a `map` into a producer of a push stream
340
+ /// of its keys by emitting each contained *in order*.
341
+ def eachKey[K, V](map: Map[K, V]): Unit / emit[K] =
342
+ map.foreach { (k, _v) => do emit(k) }
343
+
344
+ /// Turns a `map` into a producer of a push stream
345
+ /// of its values by emitting each contained.
346
+ def eachValue[K, V](map: Map[K, V]): Unit / emit[V] =
347
+ map.foreach { (_k, v) => do emit(v) }
348
+
349
+ /// Collects `(key, value)` pairs from a push stream producer `stream` into a new `map`
350
+ /// using the provided `compare` function, discarding the result of the producer.
351
+ def collect[K, V](?compare: (K, K) => Ordering at {}) { stream: () => Unit / emit[(K, V)] }: Map[K, V] =
352
+ returning::collect[K, V, Unit](compare){stream}.second
353
+
354
+ namespace returning {
355
+ /// Collects `(key, value)` pairs from a push stream producer `stream` into a new `map`,
356
+ /// using the provided `compare` function, returning the result of the producer as well as the collected map.
357
+ def collect[K, V, R](?compare: (K, K) => Ordering at {}) { stream: () => R / emit[(K, V)] }: (R, Map[K, V]) =
358
+ var map = map::empty[K, V](compare)
359
+ val r = try {
360
+ stream()
361
+ } with emit[(K, V)] { case (k, v) =>
362
+ map = map.put(k, v)
363
+ resume(())
364
+ }
365
+ (r, map)
366
+ }
367
+
368
+ // Internal tree representation
369
+
318
370
  /// Please don't directly use:
319
371
  /// - the `Tree` type
320
372
  /// - its internal constructors `Bin` & `Tip`,
@@ -527,6 +579,27 @@ namespace internal {
527
579
  go(m)
528
580
  }
529
581
 
582
+ /// Traverse all keys and their associated values in tree `m` in *reverse* order,
583
+ /// running the function `action` on a key and its associated value.
584
+ ///
585
+ /// Law: `m.foreachReversed { action } === m.toList.reverse.foreach { action }`
586
+ ///
587
+ /// O(N)
588
+ ///
589
+ /// TODO: Support {Control} for early exits.
590
+ def foreachReversed[K, V](m: Tree[K, V]) { action: (K, V) => Unit }: Unit = {
591
+ def go(m: Tree[K, V]): Unit = {
592
+ m match {
593
+ case Tip() => ()
594
+ case Bin(_, k, v, l, r) =>
595
+ go(r)
596
+ action(k, v)
597
+ go(l)
598
+ }
599
+ }
600
+ go(m)
601
+ }
602
+
530
603
  /// Create a tree from a list of (key, value) pairs.
531
604
  /// If the list contains more than one value for the same key,
532
605
  /// only the last value is used in the tree.
@@ -572,11 +645,11 @@ namespace internal {
572
645
  case Nil() => (Tip(), [], [])
573
646
  case Cons((k, v), rest) =>
574
647
  if (level == 1) {
575
- val singleton = Bin(1, k, v, Tip(), Tip())
648
+ val one = singleton(k, v)
576
649
  if (notOrdered(k, rest)) {
577
- (singleton, [], rest)
650
+ (one, [], rest)
578
651
  } else {
579
- (singleton, rest, [])
652
+ (one, rest, [])
580
653
  }
581
654
  } else {
582
655
  val res = create(level.bitwiseShr(1), pairs)
@@ -584,7 +657,8 @@ namespace internal {
584
657
  case (_, Nil(), _) => res
585
658
  case (l, Cons((k2, v2), Nil()), zs) => (l.putMax(k2, v2), [], zs)
586
659
  case (l, Cons((k2, v2), rest2), _) =>
587
- val xs = Cons((k2, v2), rest2) // @-pattern
660
+ // @-pattern
661
+ val xs = Cons((k2, v2), rest2)
588
662
 
589
663
  if (notOrdered(k2, rest2)) { (l, [], xs) }
590
664
  else {
@@ -608,7 +682,7 @@ namespace internal {
608
682
  cr match {
609
683
  case (r, xs, Nil()) => go(level.bitwiseShl(1), link(k, v, l, r), xs)
610
684
  case (r, Nil(), ys) => insertMany(link(k, v, l, r), ys)
611
- case _ => panic("create: go: cannot happen, invariant broken!")
685
+ case _ => <{ "create: go: cannot happen, invariant broken!" }>
612
686
  }
613
687
  }
614
688
  }
@@ -763,9 +837,8 @@ namespace internal {
763
837
  val ratio = 2
764
838
  val delta = 3
765
839
 
766
- def bin[K, V](k: K, v: V, l: Tree[K, V], r: Tree[K, V]): Tree[K, V] = {
840
+ def bin[K, V](k: K, v: V, l: Tree[K, V], r: Tree[K, V]): Tree[K, V] =
767
841
  Bin(l.size() + r.size() + 1, k, v, l, r)
768
- }
769
842
 
770
843
  def balance[K, V](k: K, v: V, l: Tree[K, V], r: Tree[K, V]): Tree[K, V] = {
771
844
  /*
@@ -778,10 +851,8 @@ namespace internal {
778
851
  t2 t3 t1 t2
779
852
  */
780
853
  def singleL[A, B](k1: A, v1: B, t1: Tree[A, B], m: Tree[A, B]): Tree[A, B] = {
781
- m match {
782
- case Bin(_, k2, v2, t2, t3) => bin(k2, v2, bin(k1, v1, t1, t2), t3)
783
- case _ => panic("impossible: singleL: Tip")
784
- }
854
+ val Bin(_, k2, v2, t2, t3) = m else <{ "impossible: singleL: Tip" }>
855
+ bin(k2, v2, bin(k1, v1, t1, t2), t3)
785
856
  }
786
857
 
787
858
  /*
@@ -794,10 +865,8 @@ namespace internal {
794
865
  t1 t2 t1 t2 t3
795
866
  */
796
867
  def singleR[A, B](k1: A, v1: B, m: Tree[A, B], t3: Tree[A, B]): Tree[A, B] = {
797
- m match {
798
- case Bin(_, k2, v2, t1, t2) => bin(k2, v2, t1, bin(k1, v1, t2, t3))
799
- case _ => panic("impossible: singleR: Tip")
800
- }
868
+ val Bin(_, k2, v2, t1, t2) = m else <{ "impossible: singleR: Tip" }>
869
+ bin(k2, v2, t1, bin(k1, v1, t2, t3))
801
870
  }
802
871
 
803
872
  /*
@@ -812,11 +881,8 @@ namespace internal {
812
881
  t2 t3
813
882
  */
814
883
  def doubleL[A, B](k1: A, v1: B, t1: Tree[A, B], m: Tree[A, B]): Tree[A, B] = {
815
- m match {
816
- case Bin(_, k2, v2, Bin(_, k3, v3, t2, t3), t4) =>
817
- bin(k3, v3, bin(k1, v1, t1, t2), bin(k2, v2, t3, t4))
818
- case _ => panic("impossible: doubleL: Tip")
819
- }
884
+ val Bin(_, k2, v2, Bin(_, k3, v3, t2, t3), t4) = m else <{ "impossible: doubleL: Tip" }>
885
+ bin(k3, v3, bin(k1, v1, t1, t2), bin(k2, v2, t3, t4))
820
886
  }
821
887
 
822
888
  /*
@@ -831,12 +897,8 @@ namespace internal {
831
897
  t2 t3
832
898
  */
833
899
  def doubleR[A, B](k1: A, v1: B, m: Tree[A, B], t4: Tree[A, B]): Tree[A, B] = {
834
- m match {
835
- case Bin(_, k2, v2, t1, Bin(_, k3, v3, t2, t3)) =>
836
- bin(k3, v3, bin(k2, v2, t1, t2), bin(k1, v1, t3, t4))
837
- case _ =>
838
- panic("impossible: doubleR: Tip")
839
- }
900
+ val Bin(_, k2, v2, t1, Bin(_, k3, v3, t2, t3)) = m else <{ "impossible: doubleR: Tip" }>
901
+ bin(k3, v3, bin(k2, v2, t1, t2), bin(k1, v1, t3, t4))
840
902
  }
841
903
 
842
904
  def rotateL[A, B](k: A, v: B, l: Tree[A, B], r: Tree[A, B]): Tree[A, B] = {
@@ -859,7 +921,7 @@ namespace internal {
859
921
  if ((sizeL + sizeR) <= 1) { Bin(sizeCombined, k, v, l, r) }
860
922
  else if (sizeR > (delta * sizeL)) { rotateL(k, v, l, r) }
861
923
  else if (sizeL > (delta * sizeR)) { rotateR(k, v, l, r) }
862
- else { Bin(sizeCombined, k, v, l, r)}
924
+ else { Bin(sizeCombined, k, v, l, r) }
863
925
  }
864
926
 
865
927
  record MaxView[K, V](k: K, v: V, m: Tree[K, V])
@@ -963,13 +1025,13 @@ namespace internal {
963
1025
  // Section: prettyprinting for trees:
964
1026
 
965
1027
  /// Works only on the backends that support `genericShow` (JavaScript)!
966
- def prettyTree[K, V](m: Tree[K, V]): String = {
1028
+ def prettyTree[K, V](m: Tree[K, V]) { ?key::show: K => String } { ?value::show: V => String }: String = {
967
1029
  // Helper function to recursively build the string representation of the tree
968
1030
  def go(t: Tree[K, V], prefix: String, isTail: Bool): String = {
969
1031
  t match {
970
1032
  case Tip() => ""
971
1033
  case Bin(_, k, v, l, r) =>
972
- val pair = k.show ++ " → " ++ v.show
1034
+ val pair = key::show(k) ++ " → " ++ value::show(v)
973
1035
  val currentLine = prefix ++ (if (isTail) "└── " else "├── ") ++ pair ++ "\n"
974
1036
 
975
1037
  val newPrefix = prefix ++ (if (isTail) " " else "│ ")
@@ -984,47 +1046,11 @@ namespace internal {
984
1046
  go(m, "", true)
985
1047
  }
986
1048
 
987
- def prettyPairs[K, V](list: List[(K, V)]) { showLeft: K => String } { showRight: V => String }: String = {
1049
+ def prettyPairs[K, V](list: List[(K, V)]) { ?key::show: K => String } { ?value::show: V => String }: String = {
988
1050
  val res: String =
989
- list.map { case (k, v) => showLeft(k) ++ " → " ++ showRight(v) }
1051
+ list.map { case (k, v) => key::show(k) ++ " → " ++ value::show(v) }
990
1052
  .join(", ")
991
1053
 
992
1054
  "[" ++ res ++ "]"
993
1055
  }
994
- }
995
-
996
-
997
- // Streaming
998
- // ---------
999
-
1000
- /// Turns a `map` into a producer of a push stream
1001
- /// of `(key, value)` pairs by emitting each contained *in order*.
1002
- def each[K, V](map: Map[K, V]): Unit / emit[(K, V)] =
1003
- map.foreach { (k, v) => do emit((k, v)) }
1004
-
1005
- /// Turns a `map` into a producer of a push stream
1006
- /// of its keys by emitting each contained *in order*.
1007
- def eachKey[K, V](map: Map[K, V]): Unit / emit[K] =
1008
- map.foreach { (k, _v) => do emit(k) }
1009
-
1010
- /// Turns a `map` into a producer of a push stream
1011
- /// of its values by emitting each contained.
1012
- def eachValue[K, V](map: Map[K, V]): Unit / emit[V] =
1013
- map.foreach { (_k, v) => do emit(v) }
1014
-
1015
- /// Collects `(key, value)` pairs from a push stream producer `stream` into a new `map`
1016
- /// using the provided `compare` function, discarding the result of the producer.
1017
- def collect[K, V](compare: (K, K) => Ordering at {}) { stream: () => Unit / emit[(K, V)] }: Map[K, V] =
1018
- returning::collect[K, V, Unit](compare){stream}.second
1019
-
1020
- namespace returning {
1021
- /// Collects `(key, value)` pairs from a push stream producer `stream` into a new `map`,
1022
- /// using the provided `compare` function, returning the result of the producer as well as the collected map.
1023
- def collect[K, V, R](compare: (K, K) => Ordering at {}) { stream: () => R / emit[(K, V)] }: (R, Map[K, V]) =
1024
- try {
1025
- (stream(), empty(compare))
1026
- } with emit[(K, V)] { case (k, v) =>
1027
- val (r, map) = resume(());
1028
- (r, map.put(k, v))
1029
- }
1030
- }
1056
+ }
@@ -9,7 +9,7 @@ record Set[A](tree: internal::Tree[A, Unit], compare: (A, A) => Ordering at {})
9
9
  /// Create a new empty set using a pure, first-class comparison function.
10
10
  ///
11
11
  /// O(1)
12
- def empty[A](compare: (A, A) => Ordering at {}): Set[A] =
12
+ def empty[A](?compare: (A, A) => Ordering at {}): Set[A] =
13
13
  Set(internal::empty(), compare)
14
14
 
15
15
  /// Create a new empty set using a generic comparison function.
@@ -36,7 +36,7 @@ def nonEmpty[A](s: Set[A]): Bool = internal::nonEmpty(s.tree)
36
36
  /// Requires a pure, first-class comparison function.
37
37
  ///
38
38
  /// O(1)
39
- def singleton[A](element: A, compare: (A, A) => Ordering at {}): Set[A] =
39
+ def singleton[A](element: A, ?compare: (A, A) => Ordering at {}): Set[A] =
40
40
  Set(internal::singleton(element, ()), compare)
41
41
 
42
42
  /// Create a new set containing a single `element` and a generic comparison function.
@@ -81,7 +81,7 @@ def toMap[K, V](keys: Set[K]) { valueOf: K => V }: Map[K, V] = {
81
81
  ///
82
82
  /// O(N log N)
83
83
  /// O(N) if the list is sorted
84
- def fromList[A](list: List[A], compare: (A, A) => Ordering at {}): Set[A] = {
84
+ def fromList[A](list: List[A], ?compare: (A, A) => Ordering at {}): Set[A] = {
85
85
  val tree: internal::Tree[A, Unit] = internal::fromList(list.map { k => (k, ()) }, compare)
86
86
  Set(tree, compare)
87
87
  }
@@ -105,15 +105,23 @@ def fromListGeneric[A](list: List[A]): Set[A] = {
105
105
  def foreach[A](s: Set[A]) { action: A => Unit }: Unit =
106
106
  internal::foreach(s.tree) { (k, _v) => action(k) }
107
107
 
108
+ /// Traverse all elements in *reverse* order, running the function `action` on each element.
109
+ ///
110
+ /// Law: `s.foreachReversed { action } === s.toList.reverse.foreach { action }`
111
+ ///
112
+ /// O(N)
113
+ def foreachReversed[A](s: Set[A]) { action: A => Unit }: Unit =
114
+ internal::foreachReversed(s.tree) { (k, _v) => action(k) }
115
+
108
116
  /// Create a list from a given set.
109
117
  ///
110
118
  /// O(N)
111
119
  def toList[A](s: Set[A]): List[A] = {
112
120
  var acc = Nil()
113
- s.foreach { a =>
121
+ s.foreachReversed { a =>
114
122
  acc = Cons(a, acc)
115
123
  }
116
- acc.reverse
124
+ acc
117
125
  }
118
126
 
119
127
  /// Check if a predicate holds for all elements in a given set.
@@ -189,7 +197,7 @@ def deleteMany[A](s: Set[A], list: List[A]): Set[A] = {
189
197
  /// Uses an explicit comparison function.
190
198
  ///
191
199
  /// O(???)
192
- def difference[A](s1: Set[A], s2: Set[A], compare: (A, A) => Ordering at {}): Set[A] = {
200
+ def difference[A](s1: Set[A], s2: Set[A], ?compare: (A, A) => Ordering at {}): Set[A] = {
193
201
  val newTree = internal::difference(s1.tree, s2.tree, compare)
194
202
  Set(newTree, compare)
195
203
  }
@@ -206,7 +214,7 @@ def difference[A](s1: Set[A], s2: Set[A]): Set[A] =
206
214
  /// Uses an explicit comparison function.
207
215
  ///
208
216
  /// O(???)
209
- def union[A](s1: Set[A], s2: Set[A], compare: (A, A) => Ordering at {}): Set[A] = {
217
+ def union[A](s1: Set[A], s2: Set[A], ?compare: (A, A) => Ordering at {}): Set[A] = {
210
218
  val newTree = internal::union(s1.tree, s2.tree, compare) { (_k, _v1, _v2) => () }
211
219
  Set(newTree, compare)
212
220
  }
@@ -222,7 +230,7 @@ def union[A](s1: Set[A], s2: Set[A]): Set[A] =
222
230
  /// Uses an explicit comparison function.
223
231
  ///
224
232
  /// O(???)
225
- def intersection[A](s1: Set[A], s2: Set[A], compare: (A, A) => Ordering at {}): Set[A] = {
233
+ def intersection[A](s1: Set[A], s2: Set[A], ?compare: (A, A) => Ordering at {}): Set[A] = {
226
234
  val newTree = internal::intersection(s1.tree, s2.tree, compare) { (_k, _v1, _v2) => () }
227
235
  Set(newTree, compare)
228
236
  }
@@ -245,17 +253,20 @@ def each[A](set: Set[A]): Unit / emit[A] =
245
253
 
246
254
  /// Collects elements emitted by a push stream producer `stream` into a new set
247
255
  /// using provided `compare` function, discarding the result of the producer.
248
- def collect[A](compare: (A, A) => Ordering at {}) { stream: () => Unit / emit[A] }: Set[A] =
256
+ def collect[A](?compare: (A, A) => Ordering at {}) { stream: () => Unit / emit[A] }: Set[A] =
249
257
  returning::collect[A, Unit](compare){stream}.second
250
258
 
251
259
  namespace returning {
252
260
  /// Collects elements emitted by a push stream producer `stream` into a new set
253
261
  /// using provided `compare` function, returning the result of the producer as well as the collected set.
254
- def collect[A, R](compare: (A, A) => Ordering at {}) { stream: () => R / emit[A] }: (R, Set[A]) =
255
- try {
256
- (stream(), empty(compare))
262
+ def collect[A, R](?compare: (A, A) => Ordering at {}) { stream: () => R / emit[A] }: (R, Set[A]) = {
263
+ var set = set::empty(compare)
264
+ val r = try {
265
+ stream()
257
266
  } with emit[A] { (v) =>
258
- val (r, set) = resume(());
259
- (r, set.insert(v))
267
+ set = set.insert(v)
268
+ resume(())
260
269
  }
270
+ (r, set)
271
+ }
261
272
  }
@@ -1,9 +1,9 @@
1
1
  <!doctype html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>Eclipse Public License 2.0 (EPL) | The Eclipse Foundation</title>
2
- <meta property="og:title" content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=twitter:title content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta property="og:description" content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=twitter:description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=author content="Wayne Beaton"><link href=/favicon.ico rel=icon type=image/x-icon><meta property="og:image" content="https://www.eclipse.org/images/og-image.jpg"><meta name=twitter:image content="https://www.eclipse.org/images/og-image.jpg"><meta name=twitter:card content="summary"><meta name=twitter:site content="@eclipsefdn"><meta name=twitter:creator content="@eclipsefdn"><meta property="og:url" content="https://www.eclipse.org/legal/epl-2.0/"><meta property="og:type" content="website"><meta property="og:site_name" content="Eclipse Foundation"><meta name=keywords content="epl,epl-2.0,legal,foundation,eclipse,eclipse public license,license,licenses"><meta name=generator content="Hugo 0.144.2"><link rel=canonical href=https://www.eclipse.org/legal/epl-2.0/><link rel=alternate href=https://www.eclipse.org//legal/epl-2.0/index.xml type=application/rss+xml title><script>(function(e,t,n,s,o){e[s]=e[s]||[],e[s].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var a=t.getElementsByTagName(n)[0],i=t.createElement(n),r=s!="dataLayer"?"&l="+s:"";i.async=!0,i.src="https://www.googletagmanager.com/gtm.js?id="+o+r,a.parentNode.insertBefore(i,a)})(window,document,"script","dataLayer","GTM-5WLCZXC")</script><script>var tableClasses="table"</script><link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel=stylesheet type=text/css> <script type="module" crossorigin src="/public/assets/legal/epl-2.0/index-D2WCCf76.js"></script>
3
- <link rel="modulepreload" crossorigin href="/public/assets/main-nLcZo0k3.js">
2
+ <meta property="og:title" content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=twitter:title content="Eclipse Public License 2.0 (EPL) | The Eclipse Foundation"><meta name=description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta property="og:description" content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=twitter:description content="The Eclipse Foundation provides our global community of individuals and organisations with a mature, scalable, and business-friendly environment for open source …"><meta name=author content="Wayne Beaton"><link href=/favicon.ico rel=icon type=image/x-icon><meta property="og:image" content="https://www.eclipse.org/images/og-image-template_hu_cdf3b1afdf89db6f.png"><meta name=twitter:image content="https://www.eclipse.org/images/og-image-template_hu_cdf3b1afdf89db6f.png"><meta name=twitter:card content="summary"><meta name=twitter:site content="@eclipsefdn"><meta name=twitter:creator content="@eclipsefdn"><meta property="og:url" content="https://www.eclipse.org/legal/epl-2.0/"><meta property="og:type" content="website"><meta property="og:site_name" content="Eclipse Foundation"><meta name=keywords content="epl,epl-2.0,legal,foundation,eclipse,eclipse public license,license,licenses"><meta name=generator content="Hugo 0.144.2"><link rel=canonical href=https://www.eclipse.org/legal/epl-2.0/><link rel=alternate href=https://www.eclipse.org//legal/epl-2.0/index.xml type=application/rss+xml title><script>(function(e,t,n,s,o){e[s]=e[s]||[],e[s].push({"gtm.start":(new Date).getTime(),event:"gtm.js"});var a=t.getElementsByTagName(n)[0],i=t.createElement(n),r=s!="dataLayer"?"&l="+s:"";i.async=!0,i.src="https://www.googletagmanager.com/gtm.js?id="+o+r,a.parentNode.insertBefore(i,a)})(window,document,"script","dataLayer","GTM-5WLCZXC")</script><script>var tableClasses="table"</script><link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel=stylesheet type=text/css> <script type="module" crossorigin src="/public/assets/legal/epl-2.0/index-Br5FsgwB.js"></script>
3
+ <link rel="modulepreload" crossorigin href="/public/assets/main-DMD1QOmo.js">
4
4
  <link rel="modulepreload" crossorigin href="/public/assets/get-body-content-D33PtpOf.js">
5
5
  <link rel="modulepreload" crossorigin href="/public/assets/generate-body-content-BZFBJ1cI.js">
6
- <link rel="stylesheet" crossorigin href="/public/assets/main-MI5_Cdt5.css">
6
+ <link rel="stylesheet" crossorigin href="/public/assets/main-BSGkhNuL.css">
7
7
  </head><body><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5WLCZXC" height=0 width=0 style=display:none;visibility:hidden></iframe></noscript><a href=#main class=skip-link>Skip to content</a><div class=eclipsefdn-featured-story-banner data-publish-target=eclipse_org></div><header class="header-wrapper header-wrapper" id=header><div class="navigation-bar dark"><div class=container><div class=navigation-bar-inner><div class=navigation-bar-left><a class="focus-highlight focus-inverse" href=/><img class=navigation-bar-brand src=https://www.eclipse.org/eclipse.org-common/themes/solstice/public/images/logo/eclipse-foundation-white-orange.svg alt="Eclipse Foundation"></a></div><nav class="navigation-bar-nav hidden-xs hidden-sm"><ul><li><button type=button class="btn btn-link" data-target=join-us-menu>Join us</button></li><li><button type=button class="btn btn-link" data-target=what-we-do-menu>What we do</button></li><li><button type=button class="btn btn-link" data-target=resources-menu>Resources</button></li><li><button type=button class="btn btn-link" data-target=about-us-menu>About us</button></li></ul></nav><div class=navigation-bar-right><div class="navigation-bar-controls hidden-xs hidden-sm"><ul><li class=dropdown><a class="dropdown-toggle link-unstyled focus-highlight" tabindex=0 id=search-dropdown-toggle data-toggle=dropdown href=# aria-label=Search><i class="fa-solid fa-magnifying-glass" aria-hidden=true></i></a><form class="search-bar dropdown-menu dropdown-menu-right" aria-labelledby=search-dropdown-toggle action=https://www.eclipse.org/home/search/ method=get><input class=search-bar-input name=q placeholder=Search>
8
8
  <button class="btn btn-link" type=submit>
9
9
  <i class="fa-solid fa-arrow-right"></i></button></form></li><li class=dropdown><a class="dropdown-toggle focus-highlight" id=user-menu href=# data-toggle=dropdown aria-haspopup=true aria-expanded=false tabindex=0><i class="fa-regular fa-user" aria-hidden=true></i></a><ul class="dropdown-menu dropdown-menu-right" aria-labelledby=user-menu><li><a href=https://accounts.eclipse.org/user><i class="fa-solid fa-user margin-right-10" aria-hidden=true></i>