@effekt-lang/effekt 0.86.0 → 0.87.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 +0 -0
- package/libraries/common/array.effekt +9 -20
- package/libraries/common/buffer.effekt +1 -1
- package/libraries/common/bytearray.effekt +1 -1
- package/libraries/common/bytestream.effekt +10 -10
- package/libraries/common/dequeue.effekt +0 -8
- package/libraries/common/dictionary.effekt +3 -3
- package/libraries/common/effekt.effekt +5 -11
- package/libraries/common/exception.effekt +1 -1
- package/libraries/common/io/network.effekt +3 -3
- package/libraries/common/io/promise.effekt +1 -1
- package/libraries/common/json.effekt +3 -3
- package/libraries/common/list.effekt +1 -10
- package/libraries/common/map.effekt +5 -5
- package/libraries/common/option.effekt +0 -9
- package/libraries/common/queue.effekt +2 -2
- package/libraries/common/resizable_array.effekt +1 -1
- package/libraries/common/result.effekt +1 -1
- package/libraries/common/scanner.effekt +11 -11
- package/libraries/common/set.effekt +4 -4
- package/libraries/common/stream.effekt +13 -13
- package/libraries/common/string.effekt +4 -4
- package/licenses/mit license - mit-license.html +48 -89
- package/licenses/the bsd license - bsd-license.html +41 -88
- package/licenses/the mit license - mit.html +45 -92
- package/package.json +1 -1
package/bin/effekt
CHANGED
|
Binary file
|
|
@@ -348,7 +348,7 @@ def reverse[A](arr: Array[A]): Unit = {
|
|
|
348
348
|
/// O(N)
|
|
349
349
|
def reversed[A](arr: Array[A]): Array[A] = {
|
|
350
350
|
val size = arr.size()
|
|
351
|
-
val result = unsafeAllocate
|
|
351
|
+
val result = unsafeAllocate(size)
|
|
352
352
|
|
|
353
353
|
// Copy elements in reverse order to new array
|
|
354
354
|
each(0, size) { i =>
|
|
@@ -473,7 +473,7 @@ def sliced[A](arr: Array[A], start: Int, end: Int): Array[A] = {
|
|
|
473
473
|
/// O(min(N, M))
|
|
474
474
|
def zipped[A, B](this: Array[A], other: Array[B]): Array[(A, B)] = {
|
|
475
475
|
val len = min(this.size, other.size)
|
|
476
|
-
val result = unsafeAllocate
|
|
476
|
+
val result = unsafeAllocate(len)
|
|
477
477
|
each(0, len) { i =>
|
|
478
478
|
result.set(i, (this.get(i), other.get(i)))
|
|
479
479
|
}
|
|
@@ -486,7 +486,7 @@ def zipped[A, B](this: Array[A], other: Array[B]): Array[(A, B)] = {
|
|
|
486
486
|
/// O(min(N, M))
|
|
487
487
|
def zippedWith[A, B, C](this: Array[A], other: Array[B]) { f: (A, B) => C }: Array[C] = {
|
|
488
488
|
val len = min(this.size, other.size)
|
|
489
|
-
val result = unsafeAllocate
|
|
489
|
+
val result = unsafeAllocate(len)
|
|
490
490
|
each(0, len) { i =>
|
|
491
491
|
result.set(i, f(this.get(i), other.get(i)))
|
|
492
492
|
}
|
|
@@ -620,30 +620,19 @@ def sorted[A](arr: Array[A]) { ord: (A, A) => Ordering }: Array[A] = {
|
|
|
620
620
|
// Show Instances
|
|
621
621
|
// --------------
|
|
622
622
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
def showBuiltin[A](arr: Array[A]) { showA: A => String }: String = {
|
|
623
|
+
def Array::show[A](arr: Array[A]) { ?show: A => String }: String = {
|
|
626
624
|
var output = "Array("
|
|
627
625
|
val lastIndex = arr.size - 1
|
|
628
626
|
|
|
629
627
|
arr.foreachIndex { (index, a) =>
|
|
630
|
-
if (index == lastIndex) output = output ++
|
|
631
|
-
else output = output ++
|
|
628
|
+
if (index == lastIndex) output = output ++ show(a)
|
|
629
|
+
else output = output ++ show(a) ++ ", "
|
|
632
630
|
}
|
|
633
631
|
output = output ++ ")"
|
|
634
632
|
|
|
635
633
|
output
|
|
636
634
|
}
|
|
637
635
|
|
|
638
|
-
def showBuiltin(l: Array[Int]): String = showBuiltin(l) { e => show(e) }
|
|
639
|
-
def showBuiltin(l: Array[Double]): String = showBuiltin(l) { e => show(e) }
|
|
640
|
-
def showBuiltin(l: Array[Bool]): String = showBuiltin(l) { e => show(e) }
|
|
641
|
-
def showBuiltin(l: Array[String]): String = showBuiltin(l) { e => e }
|
|
642
|
-
|
|
643
|
-
def println(l: Array[Int]): Unit = println(showBuiltin(l))
|
|
644
|
-
def println(l: Array[Double]): Unit = println(showBuiltin(l))
|
|
645
|
-
def println(l: Array[Bool]): Unit = println(showBuiltin(l))
|
|
646
|
-
def println(l: Array[String]): Unit = println(showBuiltin(l))
|
|
647
636
|
|
|
648
637
|
// Streaming
|
|
649
638
|
// ---------
|
|
@@ -684,12 +673,12 @@ def feed[T, R](array: Array[T]) { driver: () => R / next[T] }: R = {
|
|
|
684
673
|
/// The provided capacity is a hint for the expected number of elements.
|
|
685
674
|
/// It must be strictly larger than zero.
|
|
686
675
|
def collect[A](capacity: Int) { stream: () => Unit / emit[A] }: Array[A] =
|
|
687
|
-
returning::collect
|
|
676
|
+
returning::collect(capacity){stream}.second
|
|
688
677
|
|
|
689
678
|
/// Collects elements emitted by a push stream producer `stream` into a new array,
|
|
690
679
|
/// discarding the result of the producer.
|
|
691
680
|
def collect[A] { stream: () => Unit / emit[A] }: Array[A] =
|
|
692
|
-
collect
|
|
681
|
+
collect(4){stream}
|
|
693
682
|
|
|
694
683
|
namespace returning {
|
|
695
684
|
|
|
@@ -713,6 +702,6 @@ namespace returning {
|
|
|
713
702
|
/// Collects elements emitted by a push stream producer `stream` into a new array,
|
|
714
703
|
/// returning the result of the producer as well as the collected array.
|
|
715
704
|
def collect[A, R] { stream: () => R / emit[A] }: (R, Array[A]) =
|
|
716
|
-
returning::collect
|
|
705
|
+
returning::collect(4){stream}
|
|
717
706
|
|
|
718
707
|
}
|
|
@@ -33,7 +33,7 @@ def emptyBuffer[T](capacity: Int): Buffer[T] at global = {
|
|
|
33
33
|
}
|
|
34
34
|
def arrayBuffer[T](initialCapacity: Int): Buffer[T] at global = {
|
|
35
35
|
// TODO allocate buffer (and array) into a region r.
|
|
36
|
-
val contents = array::unsafeAllocate
|
|
36
|
+
val contents = array::unsafeAllocate(initialCapacity)
|
|
37
37
|
val head = ref(0)
|
|
38
38
|
val tail = ref(0)
|
|
39
39
|
|
|
@@ -330,7 +330,7 @@ def feed[R](bytes: ByteArray) { driver: () => R / next[Byte] }: R = {
|
|
|
330
330
|
}
|
|
331
331
|
|
|
332
332
|
def collect(capacity: Int) { stream: () => Unit / emit[Byte] }: ByteArray =
|
|
333
|
-
returning::collect
|
|
333
|
+
returning::collect(capacity){stream}.second
|
|
334
334
|
|
|
335
335
|
def collect { stream: () => Unit / emit[Byte] }: ByteArray =
|
|
336
336
|
collect(4){stream}
|
|
@@ -103,11 +103,11 @@ def hex{ body: => Unit / { write, HexSplices } }: Unit / emit[Byte] = {
|
|
|
103
103
|
body()
|
|
104
104
|
}
|
|
105
105
|
with splice[String] { s =>
|
|
106
|
-
feed(s){ loop { do splice
|
|
106
|
+
feed(s){ loop { do splice(do next()) } }
|
|
107
107
|
resume(())
|
|
108
108
|
}
|
|
109
109
|
with splice[ByteArray] { ba =>
|
|
110
|
-
ba.foreach{ b => do splice
|
|
110
|
+
ba.foreach{ b => do splice(b) }
|
|
111
111
|
resume(())
|
|
112
112
|
}
|
|
113
113
|
}
|
|
@@ -115,15 +115,15 @@ def hex{ body: => Unit / { write, HexSplices } }: Unit / emit[Byte] = {
|
|
|
115
115
|
feed(s){
|
|
116
116
|
loop {
|
|
117
117
|
attempt {
|
|
118
|
-
val upper: Int = hexDigitValue(do next
|
|
119
|
-
val lower: Int = hexDigitValue(do next
|
|
120
|
-
do emit
|
|
118
|
+
val upper: Int = hexDigitValue(do next())
|
|
119
|
+
val lower: Int = hexDigitValue(do next())
|
|
120
|
+
do emit((16 * upper + lower).toByte)
|
|
121
121
|
} { () }
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
resume(())
|
|
125
125
|
}
|
|
126
|
-
with splice[Char] { c => do emit
|
|
126
|
+
with splice[Char] { c => do emit(c.toInt.toByte); resume(()) }
|
|
127
127
|
with splice[Byte] { b => do emit(b); resume(()) }
|
|
128
128
|
with splice[Unit] { u => resume(()) }
|
|
129
129
|
with splice[Int] { n => bytesBE(n); resume(()) }
|
|
@@ -140,7 +140,7 @@ def hex{ body: => Unit / { write, HexSplices } }: Unit / emit[Byte] = {
|
|
|
140
140
|
/// convert the given hex notation to an integer (big-endian)
|
|
141
141
|
def x{ body: => Unit / { write, HexSplices } }: Int = {
|
|
142
142
|
var res = 0
|
|
143
|
-
for
|
|
143
|
+
for{ hex{body} }{ v => res = res * 256 + v.toInt }
|
|
144
144
|
res
|
|
145
145
|
}
|
|
146
146
|
|
|
@@ -218,7 +218,7 @@ def collectBitsBE{ body: => Unit / emit[Bit] }: Int = {
|
|
|
218
218
|
|
|
219
219
|
/// split emitted bytes and emit the individual bits in big-endian bit order
|
|
220
220
|
def ungroupBytesBE{ body: => Unit / emit[Byte] }: Unit / emit[Bit] =
|
|
221
|
-
for
|
|
221
|
+
for{body}{ b => bitsBE(b) }
|
|
222
222
|
|
|
223
223
|
/// streaming negation in 2s-complement for little-endian bitstreams
|
|
224
224
|
def twoscomplementLE{ body: => Unit / emit[Bit] }: Unit / emit[Bit] = {
|
|
@@ -296,7 +296,7 @@ def bit{ body: => Unit / { write, BitSplices } }: Unit / emit[Bit] = {
|
|
|
296
296
|
with write { s =>
|
|
297
297
|
feed(s){
|
|
298
298
|
loop {
|
|
299
|
-
do next
|
|
299
|
+
do next() match {
|
|
300
300
|
case '0' => do emit(B0())
|
|
301
301
|
case '1' => do emit(B1())
|
|
302
302
|
case _ => ()
|
|
@@ -325,7 +325,7 @@ def bit{ body: => Unit / { write, BitSplices } }: Unit / emit[Bit] = {
|
|
|
325
325
|
resume(())
|
|
326
326
|
}
|
|
327
327
|
with splice[OfWidth[BE[Signed[Int]]]] { i =>
|
|
328
|
-
list::collect
|
|
328
|
+
list::collect{
|
|
329
329
|
if(i.raw.raw.raw < 0){
|
|
330
330
|
twoscomplementLE{ bitsLE(0 - i.raw.raw.raw, i.width) }
|
|
331
331
|
} else {
|
|
@@ -74,11 +74,3 @@ def popBack[R](dq: Dequeue[R]): Option[(R, Dequeue[R])] = dq match {
|
|
|
74
74
|
case Dequeue(f, fs, Cons(x, rest), rs) =>
|
|
75
75
|
Some((x, Dequeue(f, fs, rest, rs - 1).check))
|
|
76
76
|
}
|
|
77
|
-
|
|
78
|
-
// TODO: Replace when monomorphization is merged
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def println(l: Dequeue[Int]): Unit = println(show(l))
|
|
82
|
-
def println(l: Dequeue[Double]): Unit = println(show(l))
|
|
83
|
-
def println(l: Dequeue[Bool]): Unit = println(show(l))
|
|
84
|
-
def println(l: Dequeue[String]): Unit = println(show(l))
|
|
@@ -64,7 +64,7 @@ def insert[A](dictionary: Dictionary[A], key: ByteArray, value: A): Unit = {
|
|
|
64
64
|
// The four children of a `Branch4`, plus the rest of the key at `b`,
|
|
65
65
|
// as a full 256-slot array.
|
|
66
66
|
def densified(byte1: Byte, child1: Ref[Trie[A]], byte2: Byte, child2: Ref[Trie[A]], byte3: Byte, child3: Ref[Trie[A]], byte4: Byte, child4: Ref[Trie[A]], b: Byte, i: Int): Array[Trie[A]] = {
|
|
67
|
-
val children = array
|
|
67
|
+
val children = array(256, Empty())
|
|
68
68
|
children.set(byte1.toInt, child1.get)
|
|
69
69
|
children.set(byte2.toInt, child2.get)
|
|
70
70
|
children.set(byte3.toInt, child3.get)
|
|
@@ -221,8 +221,8 @@ def lookup[A](dictionary: Dictionary[A], key: ByteArray): Option[A] = {
|
|
|
221
221
|
|
|
222
222
|
/// Collect key-value pairs from a stream of Tagged elements into a fresh Dictionary.
|
|
223
223
|
def collect[A] { stream: () => Unit / emit[Tagged[A]] }: Dictionary[A] = {
|
|
224
|
-
val dictionary = empty
|
|
225
|
-
for
|
|
224
|
+
val dictionary = empty()
|
|
225
|
+
for { stream() } { case Tagged(bytes, value) =>
|
|
226
226
|
dictionary.insert(bytes, value)
|
|
227
227
|
}
|
|
228
228
|
dictionary
|
|
@@ -55,6 +55,9 @@ extern def println(value: String): Unit =
|
|
|
55
55
|
ret %Pos zeroinitializer ; Unit
|
|
56
56
|
"""
|
|
57
57
|
|
|
58
|
+
/// Prints any value according to a matching `show` at the call site
|
|
59
|
+
def default::println[A](value: A) { ?show: A => String }: Unit = println(show(value))
|
|
60
|
+
|
|
58
61
|
extern jsNode """
|
|
59
62
|
$effekt.readln = function readln$impl(callback) {
|
|
60
63
|
const readline = require('node:readline').createInterface({
|
|
@@ -76,13 +79,6 @@ extern def readln() at async: String =
|
|
|
76
79
|
ret void
|
|
77
80
|
"""
|
|
78
81
|
|
|
79
|
-
def println(value: Int): Unit = println(value.show)
|
|
80
|
-
def println(value: Unit): Unit = println(value.show)
|
|
81
|
-
def println(value: Double): Unit = println(value.show)
|
|
82
|
-
def println(value: Bool): Unit = println(value.show)
|
|
83
|
-
def println(value: Byte): Unit = println(value.show)
|
|
84
|
-
def println(value: Char): Unit = println(value.show)
|
|
85
|
-
|
|
86
82
|
extern def showBuiltin(value: Int) at {}: String =
|
|
87
83
|
js "'' + ${value}"
|
|
88
84
|
chez "(show-number ${value})"
|
|
@@ -128,8 +124,8 @@ extern def panic(msg: String) at io: Nothing =
|
|
|
128
124
|
ret %Pos zeroinitializer ; Unit
|
|
129
125
|
"""
|
|
130
126
|
|
|
131
|
-
/// A show function structurally generated by the compiler for any type.
|
|
132
|
-
extern def
|
|
127
|
+
/// A show function structurally generated by the compiler for any type, as a default.
|
|
128
|
+
extern def default::show[R](value: R) at {}: String =
|
|
133
129
|
js "undefined"
|
|
134
130
|
chez "(void)"
|
|
135
131
|
llvm "ret %Pos poison"
|
|
@@ -223,8 +219,6 @@ extern def genericCompareImpl[R](x: R, y: R) at {}: Int =
|
|
|
223
219
|
def genericCompare[R](x: R, y: R): Ordering =
|
|
224
220
|
genericCompareImpl(x, y).fromInt
|
|
225
221
|
|
|
226
|
-
def println(o: Ordering): Unit = println(o.show)
|
|
227
|
-
|
|
228
222
|
|
|
229
223
|
// Comparison ops
|
|
230
224
|
// ==============
|
|
@@ -105,4 +105,4 @@ def report[E](proxy: on[E]) { prog: => Unit / Exception[E] }: Unit =
|
|
|
105
105
|
report {prog}
|
|
106
106
|
|
|
107
107
|
def finalize[E, T](proxy: on[E]) { finalizer: => Unit } { prog: => T / Exception[E] }: T / Exception[E] =
|
|
108
|
-
finalize
|
|
108
|
+
finalize {finalizer} {prog}
|
|
@@ -51,7 +51,7 @@ extern type Address =
|
|
|
51
51
|
llvm "%Pos"
|
|
52
52
|
|
|
53
53
|
/// The text of the given `address`, such as `127.0.0.1` or `::1`.
|
|
54
|
-
extern def
|
|
54
|
+
extern def showBuiltin(address: Address) at {}: String =
|
|
55
55
|
jsNode "${address}"
|
|
56
56
|
llvm "ret %Pos ${address}"
|
|
57
57
|
|
|
@@ -59,7 +59,7 @@ extern def toString(address: Address) at {}: String =
|
|
|
59
59
|
/// Raises the error of the resolver, such as `EAI_NONAME`, if it has none.
|
|
60
60
|
def resolve(host: String): List[Address] / Exception[IOError] = {
|
|
61
61
|
val resolution = internal::checkResult(internal::resolve(host))
|
|
62
|
-
val addresses = list::collect
|
|
62
|
+
val addresses = list::collect { internal::addresses(resolution) }
|
|
63
63
|
internal::release(resolution)
|
|
64
64
|
addresses
|
|
65
65
|
}
|
|
@@ -130,7 +130,7 @@ namespace internal {
|
|
|
130
130
|
|
|
131
131
|
/// The addresses of a `resolution`, in order.
|
|
132
132
|
def addresses(resolution: Int): Unit / emit[Address] = {
|
|
133
|
-
with val i = for
|
|
133
|
+
with val i = for { range(0, addressCount(resolution)) }
|
|
134
134
|
do emit(addressAt(resolution, i))
|
|
135
135
|
}
|
|
136
136
|
|
|
@@ -23,7 +23,7 @@ namespace promise {
|
|
|
23
23
|
/// Spawns a task and immediately returns a promise that upon completion
|
|
24
24
|
/// of the task will resolve to its result.
|
|
25
25
|
def promise[T](task: () => T at {io, async, global}): Promise[T] = {
|
|
26
|
-
val p = promise::make
|
|
26
|
+
val p = promise::make();
|
|
27
27
|
spawn(box { p.resolve(task()) });
|
|
28
28
|
return p
|
|
29
29
|
}
|
|
@@ -99,9 +99,9 @@ def readQuotedString(): Unit / { Scan[Char], emit[Char], Exception[WrongFormat]
|
|
|
99
99
|
with expect("Unexpected end of input while reading a string")
|
|
100
100
|
skipWhitespace()
|
|
101
101
|
expect("Expected \"") { must { readIf('"') } }
|
|
102
|
-
while(must { do next
|
|
102
|
+
while(must { do next() } is c and c != '"') {
|
|
103
103
|
c match {
|
|
104
|
-
case '\\' => must { do next
|
|
104
|
+
case '\\' => must { do next() } match {
|
|
105
105
|
case '"' => do emit('\"')
|
|
106
106
|
case '\\' => do emit('\\')
|
|
107
107
|
case '/' => do emit('/')
|
|
@@ -332,7 +332,7 @@ namespace test {
|
|
|
332
332
|
println("")
|
|
333
333
|
|
|
334
334
|
// format with intermediate value
|
|
335
|
-
val j = feed
|
|
335
|
+
val j = feed("""{
|
|
336
336
|
"a": null,
|
|
337
337
|
"b": [true, false, false, true], "f": 12.532
|
|
338
338
|
} """){
|
|
@@ -958,15 +958,6 @@ def isSortedBy[A](list: List[A]) { lessOrEqual: (A, A) => Bool }: Bool = {
|
|
|
958
958
|
// Show Instances
|
|
959
959
|
// --------------
|
|
960
960
|
|
|
961
|
-
// TODO: Get rid of these after monomorphization is merged
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
def println(l: List[Int]): Unit = println(show(l))
|
|
965
|
-
def println(l: List[Double]): Unit = println(show(l))
|
|
966
|
-
def println(l: List[Char]): Unit = println(show(l))
|
|
967
|
-
def println(l: List[Bool]): Unit = println(show(l))
|
|
968
|
-
def println(l: List[String]): Unit = println(show(l))
|
|
969
|
-
|
|
970
961
|
// Streaming
|
|
971
962
|
// ---------
|
|
972
963
|
|
|
@@ -1016,7 +1007,7 @@ def feed[T, R](list: List[T]) { driver: () => R / next[T] }: R = {
|
|
|
1016
1007
|
/// Collects elements emitted by a push stream producer `stream` into a new list,
|
|
1017
1008
|
/// discarding the result of the producer.
|
|
1018
1009
|
def collect[A] { stream: () => Unit / emit[A] }: List[A] =
|
|
1019
|
-
returning::collect
|
|
1010
|
+
returning::collect{stream}.second
|
|
1020
1011
|
|
|
1021
1012
|
namespace returning {
|
|
1022
1013
|
/// Collects elements emitted by a push stream producer `stream` into a new list,
|
|
@@ -297,7 +297,7 @@ def union[K, V](m1: Map[K, V], m2: Map[K, V], ?compare: (K, K) => Ordering at {}
|
|
|
297
297
|
///
|
|
298
298
|
/// O(???)
|
|
299
299
|
def union[K, V](m1: Map[K, V], m2: Map[K, V]): Map[K, V] =
|
|
300
|
-
union
|
|
300
|
+
union(m1, m2, m1.compare) { (k, v1, v2) => v1 }
|
|
301
301
|
|
|
302
302
|
/// Construct a new map which combines all elements that are in both `m1` and `m2` using the `combine` function.
|
|
303
303
|
///
|
|
@@ -311,14 +311,14 @@ def intersection[K, A, B, C](m1: Map[K, A], m2: Map[K, B], ?compare: (K, K) => O
|
|
|
311
311
|
///
|
|
312
312
|
/// O(???)
|
|
313
313
|
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] =
|
|
314
|
-
m1.intersection
|
|
314
|
+
m1.intersection(m2, compare) { (k, v1, v2) => combine(v1, v2) }
|
|
315
315
|
|
|
316
316
|
/// Construct a new map which combines all elements that are in both `m1` and `m2`.
|
|
317
317
|
/// Left-biased: Always uses values from `m1` and the comparison function from `m1`.
|
|
318
318
|
///
|
|
319
319
|
/// O(???)
|
|
320
320
|
def intersection[K, A, B](m1: Map[K, A], m2: Map[K, B]): Map[K, A] =
|
|
321
|
-
m1.intersection
|
|
321
|
+
m1.intersection(m2, m1.compare) { (k, v1, v2) => v1 }
|
|
322
322
|
|
|
323
323
|
|
|
324
324
|
|
|
@@ -343,13 +343,13 @@ def eachValue[K, V](map: Map[K, V]): Unit / emit[V] =
|
|
|
343
343
|
/// Collects `(key, value)` pairs from a push stream producer `stream` into a new `map`
|
|
344
344
|
/// using the provided `compare` function, discarding the result of the producer.
|
|
345
345
|
def collect[K, V](?compare: (K, K) => Ordering at {}) { stream: () => Unit / emit[(K, V)] }: Map[K, V] =
|
|
346
|
-
returning::collect
|
|
346
|
+
returning::collect(compare){stream}.second
|
|
347
347
|
|
|
348
348
|
namespace returning {
|
|
349
349
|
/// Collects `(key, value)` pairs from a push stream producer `stream` into a new `map`,
|
|
350
350
|
/// using the provided `compare` function, returning the result of the producer as well as the collected map.
|
|
351
351
|
def collect[K, V, R](?compare: (K, K) => Ordering at {}) { stream: () => R / emit[(K, V)] }: (R, Map[K, V]) = {
|
|
352
|
-
val (r, tree) = internal::collect
|
|
352
|
+
val (r, tree) = internal::collect(compare) {stream}
|
|
353
353
|
(r, Map(tree, compare))
|
|
354
354
|
}
|
|
355
355
|
}
|
|
@@ -54,12 +54,3 @@ def option[A, E](proxy: on[E]) { p: => A / Exception[E] }: Option[A] =
|
|
|
54
54
|
// These functions should only be used by libraries that interface with FFI
|
|
55
55
|
def undefinedToOption[A](value: A): Option[A] =
|
|
56
56
|
if (value.isUndefined) { None() } else { Some(value) }
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
// Show Instances
|
|
60
|
-
// --------------
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def println(o: Option[Int]): Unit = println(show(o))
|
|
64
|
-
def println(o: Option[Double]): Unit = println(show(o))
|
|
65
|
-
def println(o: Option[Bool]): Unit = println(show(o))
|
|
@@ -21,7 +21,7 @@ interface Queue[T] {
|
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
def emptyQueue[T](): Queue[T] at global =
|
|
24
|
-
emptyQueue
|
|
24
|
+
emptyQueue(64)
|
|
25
25
|
|
|
26
26
|
def emptyQueue[T](initialCapacity: Int): Queue[T] at global = {
|
|
27
27
|
|
|
@@ -49,7 +49,7 @@ def emptyQueue[T](initialCapacity: Int): Queue[T] at global = {
|
|
|
49
49
|
val oldSize = capacity.get
|
|
50
50
|
val newSize = capacity.get * 2
|
|
51
51
|
val oldContents = contents.get
|
|
52
|
-
val newContents = array
|
|
52
|
+
val newContents = array(newSize, None())
|
|
53
53
|
|
|
54
54
|
if (head.get < tail.get) {
|
|
55
55
|
// The queue does not wrap around; direct copy is possible.
|
|
@@ -245,7 +245,7 @@ def feed[R, T](arr: ResizableArray[T]) { driver: () => R / next[T] }: R = {
|
|
|
245
245
|
/// Collects elements emitted by a push stream producer `stream` into a new resizable array,
|
|
246
246
|
/// discarding the result of the producer.
|
|
247
247
|
def collect[T] { stream: () => Unit / emit[T] }: ResizableArray[T] =
|
|
248
|
-
returning::collect
|
|
248
|
+
returning::collect{stream}.second
|
|
249
249
|
|
|
250
250
|
namespace returning {
|
|
251
251
|
/// Collects elements emitted by a push stream producer `stream` into a new resizable array,
|
|
@@ -50,7 +50,7 @@ def firstSuccess[A, R, E](head: A, tail: List[A]) { action: A => R / Exception[E
|
|
|
50
50
|
def go(candidate: A, rest: List[A]): R = rest match {
|
|
51
51
|
case Nil() => action(candidate)
|
|
52
52
|
case Cons(next, more) =>
|
|
53
|
-
result
|
|
53
|
+
result { action(candidate) } match {
|
|
54
54
|
case Success(value) => value
|
|
55
55
|
case Error(_, _) => go(next, more)
|
|
56
56
|
}
|
|
@@ -9,7 +9,7 @@ effect Scan[A] = {next[A], peek[A]}
|
|
|
9
9
|
def skip[A](): Unit / {next[A], stop} = { do next(); () }
|
|
10
10
|
|
|
11
11
|
def expect(message: String) { scanner: () => Unit / fail }: Unit / Exception[WrongFormat] =
|
|
12
|
-
returning::expect
|
|
12
|
+
returning::expect(message){scanner}
|
|
13
13
|
|
|
14
14
|
/// If conversion is successful read the next token, otherwise stop.
|
|
15
15
|
def readWhen[A, B] { convert: A => B / fail }: B / { Scan[A], stop } = {
|
|
@@ -55,12 +55,12 @@ def readSome[A] { predicate: A => Bool }: Unit / { Scan[A], emit[A], fail } =
|
|
|
55
55
|
|
|
56
56
|
/// Skips until the predicate does not hold for the next token.
|
|
57
57
|
def skipMany[A] { predicate: A => Bool }: Unit / Scan[A] =
|
|
58
|
-
for
|
|
58
|
+
for { readMany{predicate} } { _ => () }
|
|
59
59
|
|
|
60
60
|
/// Skips until the predicate does not hold for the next token.
|
|
61
61
|
/// Must skip at least one token, else fail.
|
|
62
62
|
def skipSome[A] { predicate: A => Bool }: Unit / { Scan[A], fail } =
|
|
63
|
-
for
|
|
63
|
+
for { readSome{predicate} } { _ => () }
|
|
64
64
|
|
|
65
65
|
/// Read the next character if it is the given one.
|
|
66
66
|
def readIf(e: Char): Unit / { Scan[Char], stop } = {
|
|
@@ -117,7 +117,7 @@ def skipWhitespace(): Unit / Scan[Char] =
|
|
|
117
117
|
/// Read a prefix of the characters of the given string.
|
|
118
118
|
/// Fails when it cannot read all of them.
|
|
119
119
|
def readString(string: String): Unit / { Scan[Char], fail } =
|
|
120
|
-
for
|
|
120
|
+
for { string.each } { char => must { readIf(char) } }
|
|
121
121
|
|
|
122
122
|
/// Check that the next character is a digit in base 10, and if so read and return it.
|
|
123
123
|
def readDigit(): Int / { Scan[Char], stop } =
|
|
@@ -127,7 +127,7 @@ def readDigit(): Int / { Scan[Char], stop } =
|
|
|
127
127
|
/// Fails when there isn't at least one digit.
|
|
128
128
|
def readDecimal(): Int / { Scan[Char], fail } = {
|
|
129
129
|
var result = must { readDigit() }
|
|
130
|
-
for
|
|
130
|
+
for { many { readDigit() } } { digit =>
|
|
131
131
|
result = result * 10 + digit
|
|
132
132
|
}
|
|
133
133
|
result
|
|
@@ -141,7 +141,7 @@ def readHexDigit(): Int / { Scan[Char], stop } =
|
|
|
141
141
|
/// Fails when there isn't at least one digit.
|
|
142
142
|
def readHexadecimal(): Int / { Scan[Char], fail } = {
|
|
143
143
|
var result = must { readHexDigit() }
|
|
144
|
-
for
|
|
144
|
+
for { many { readHexDigit() } } { digit =>
|
|
145
145
|
result = result * 16 + digit
|
|
146
146
|
}
|
|
147
147
|
result
|
|
@@ -163,10 +163,10 @@ def readDouble(): Double / { Scan[Char], fail} = {
|
|
|
163
163
|
def exponent(pre: Double): Double / fail = {
|
|
164
164
|
try {
|
|
165
165
|
if(do peek() == 'e' || do peek() == 'E') {
|
|
166
|
-
skip
|
|
167
|
-
val e = do peek
|
|
168
|
-
case '+' => skip
|
|
169
|
-
case '-' => skip
|
|
166
|
+
skip()
|
|
167
|
+
val e = do peek() match {
|
|
168
|
+
case '+' => skip(); readDecimal()
|
|
169
|
+
case '-' => skip(); neg(readDecimal())
|
|
170
170
|
case _ => readDecimal()
|
|
171
171
|
}
|
|
172
172
|
pre * pow(10.0, e)
|
|
@@ -179,7 +179,7 @@ def readDouble(): Double / { Scan[Char], fail} = {
|
|
|
179
179
|
if (succeeds { must { readIf('.') } }) {
|
|
180
180
|
var b = if (pre >= 0) 0.1 else -0.1
|
|
181
181
|
var r = pre.toDouble
|
|
182
|
-
while (optionally
|
|
182
|
+
while (optionally { must { readDigit() } } is Some(d)) {
|
|
183
183
|
r = r + b * d.toDouble
|
|
184
184
|
b = b * 0.1
|
|
185
185
|
}
|
|
@@ -73,7 +73,7 @@ def fromMapKeys[K, V](map: Map[K, V]): Set[K] =
|
|
|
73
73
|
///
|
|
74
74
|
/// O(N)
|
|
75
75
|
def toMap[K, V](keys: Set[K]) { valueOf: K => V }: Map[K, V] = {
|
|
76
|
-
val newTree
|
|
76
|
+
val newTree = internal::map(keys.tree) { (k, _v) => valueOf(k) }
|
|
77
77
|
Map(newTree, keys.compare)
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -248,14 +248,14 @@ def each[A](set: Set[A]): Unit / emit[A] =
|
|
|
248
248
|
/// Collects elements emitted by a push stream producer `stream` into a new set
|
|
249
249
|
/// using provided `compare` function, discarding the result of the producer.
|
|
250
250
|
def collect[A](?compare: (A, A) => Ordering at {}) { stream: () => Unit / emit[A] }: Set[A] =
|
|
251
|
-
returning::collect
|
|
251
|
+
returning::collect(compare){stream}.second
|
|
252
252
|
|
|
253
253
|
namespace returning {
|
|
254
254
|
/// Collects elements emitted by a push stream producer `stream` into a new set
|
|
255
255
|
/// using provided `compare` function, returning the result of the producer as well as the collected set.
|
|
256
256
|
def collect[A, R](?compare: (A, A) => Ordering at {}) { stream: () => R / emit[A] }: (R, Set[A]) = {
|
|
257
|
-
val (r, tree) = internal::collect
|
|
258
|
-
stream::returning::for
|
|
257
|
+
val (r, tree) = internal::collect(compare) {
|
|
258
|
+
stream::returning::for {stream} { (v) => do emit((v, ())) }
|
|
259
259
|
}
|
|
260
260
|
(r, Set(tree, compare))
|
|
261
261
|
}
|
|
@@ -22,19 +22,19 @@ effect next[A](): A / stop
|
|
|
22
22
|
///
|
|
23
23
|
/// e.g. for[Int] { prog() } { el => println(el) }
|
|
24
24
|
def for[A] { stream: () => Unit / emit[A] } { action: A => Unit }: Unit =
|
|
25
|
-
returning::for
|
|
25
|
+
returning::for{stream}{action}
|
|
26
26
|
|
|
27
27
|
/// Applies the given function on each emitted element and reemits the result.
|
|
28
28
|
///
|
|
29
29
|
/// e.g. map[Int,String]{ x => x.show } { range(0, 5) }
|
|
30
30
|
def map[A, B] { function: A => B } { stream: () => Unit / emit[A] }: Unit / emit[B] =
|
|
31
|
-
returning::map
|
|
31
|
+
returning::map{function}{stream}
|
|
32
32
|
|
|
33
33
|
/// Runs the given action whenever the given driver reads an element.
|
|
34
34
|
///
|
|
35
35
|
/// e.g. var i = -1; with tap { i = i + 1; i }; driver()
|
|
36
36
|
def tap[A] { action: () => A / stop } { driver: () => Unit / next[A] }: Unit =
|
|
37
|
-
returning::tap
|
|
37
|
+
returning::tap{action}{driver}
|
|
38
38
|
|
|
39
39
|
/// Runs the stream as a message queue,
|
|
40
40
|
/// handling each of its elements that can produce new elements ("messages"),
|
|
@@ -75,12 +75,12 @@ def rangeFrom(lower: Int): Unit / emit[Int] = {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
def index[A] { stream: () => Unit / emit[A] }: Unit / emit[Indexed[A]] =
|
|
78
|
-
returning::index
|
|
78
|
+
returning::index{stream}
|
|
79
79
|
|
|
80
80
|
|
|
81
81
|
/// If `number` is zero or negative it does nothing
|
|
82
82
|
def limit[A](number: Int) { stream: () => Unit / emit[A] }: Unit / emit[A] =
|
|
83
|
-
boundary { returning::limit
|
|
83
|
+
boundary { returning::limit(number){stream} }
|
|
84
84
|
|
|
85
85
|
|
|
86
86
|
/// If `number` is zero or negative it does nothing
|
|
@@ -122,10 +122,10 @@ def takeWhile[A] { predicate: A => Bool } { stream: () => Unit / emit[A] }: Unit
|
|
|
122
122
|
|
|
123
123
|
|
|
124
124
|
def sum { stream: () => Unit / emit[Int] }: Int =
|
|
125
|
-
returning::sum
|
|
125
|
+
returning::sum{stream}.second
|
|
126
126
|
|
|
127
127
|
def product { stream: () => Unit / emit[Int] }: Int =
|
|
128
|
-
returning::product
|
|
128
|
+
returning::product{stream}.second
|
|
129
129
|
|
|
130
130
|
/// Returns true if all elements in the stream satisfy the given predicate,
|
|
131
131
|
/// false otherwise. Short-circuits as soon as a non-matching element is found.
|
|
@@ -290,7 +290,7 @@ def manyTee[A]{ body: { { { => Unit / emit[A] } => Unit }{ => Unit / emit[A] } =
|
|
|
290
290
|
try {
|
|
291
291
|
body{ {hnd}{prod} =>
|
|
292
292
|
running = running + 1
|
|
293
|
-
teeing
|
|
293
|
+
teeing{ {b} => hnd{b}; running = running - 1 }{prod}
|
|
294
294
|
}
|
|
295
295
|
} with emit[A] { _ =>
|
|
296
296
|
if(running > 0) resume(())
|
|
@@ -307,7 +307,7 @@ def manyTee[A]{ body: { { { => Unit / emit[A] } => Unit }{ => Unit / emit[A] } =
|
|
|
307
307
|
/// assertEquals(product{ range(0, 10) }, productRes)
|
|
308
308
|
///
|
|
309
309
|
def tee[A]{ cons1: { => Unit / emit[A] } => Unit }{ cons2: { => Unit / emit[A] } => Unit }{ prod: => Unit / emit[A] }: Unit = {
|
|
310
|
-
manyTee
|
|
310
|
+
manyTee{ {tee} =>
|
|
311
311
|
with tee{cons1}
|
|
312
312
|
with tee{cons2}
|
|
313
313
|
prod()
|
|
@@ -358,7 +358,7 @@ effect peek[A](): A / stop
|
|
|
358
358
|
|
|
359
359
|
/// Provide the ability to peek when consuming a stream.
|
|
360
360
|
def peeking[A] { peeker: () => Unit / {next[A], peek[A]} }: Unit / next[A] =
|
|
361
|
-
returning::peeking
|
|
361
|
+
returning::peeking{peeker}
|
|
362
362
|
|
|
363
363
|
// Counting and padding
|
|
364
364
|
// --------------------
|
|
@@ -371,21 +371,21 @@ effect getPos(): Int
|
|
|
371
371
|
def tracking[A](init: Int){ body: => Unit / { emit[A], getPos } }: Unit / emit[A] = {
|
|
372
372
|
var n = init
|
|
373
373
|
try body()
|
|
374
|
-
with emit[A] { b => n = n + 1; resume(do emit
|
|
374
|
+
with emit[A] { b => n = n + 1; resume(do emit(b)) }
|
|
375
375
|
with getPos{ resume(n) }
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
/// Pad to a multiple of fac emitted values, by emitting gen repeatedly
|
|
379
379
|
def pad[A](fac: Int){ gen: => A }: Unit / {getPos, emit[A]} = {
|
|
380
380
|
while(mod(do getPos(), fac) != 0) {
|
|
381
|
-
do emit
|
|
381
|
+
do emit(gen())
|
|
382
382
|
}
|
|
383
383
|
}
|
|
384
384
|
|
|
385
385
|
/// Track how many bytes were emitted (in the body)
|
|
386
386
|
/// Handles getPos
|
|
387
387
|
def tracking[A]{ body: => Unit / { emit[A], getPos } }: Unit / emit[A] =
|
|
388
|
-
tracking
|
|
388
|
+
tracking(0){body}
|
|
389
389
|
|
|
390
390
|
namespace returning {
|
|
391
391
|
|