Skip to content

Commit 8fdc5bd

Browse files
Move nth, first to stream
1 parent 10f8efd commit 8fdc5bd

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

libraries/common/binstream.effekt

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -280,35 +280,6 @@ def groupBytesLE{ body: => Unit / emit[Bit] }: Unit / emit[Byte] = {
280280
def groupBytes{ body: => Unit / emit[Bit] }: Unit / emit[Byte] =
281281
groupBytesBE{body}
282282

283-
/// Return just nth element of the given stream.
284-
/// Raises MissingValue if not enough elements are emitted
285-
// TODO could be in stream
286-
def nth[A](n: Int){ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
287-
var m = n
288-
try {
289-
body()
290-
val r: A = do raise[MissingValue](MissingValue(), "code in first did not emit any values")
291-
r
292-
} with emit[A] { a =>
293-
if (m == 0) {
294-
a
295-
} else {
296-
m = m - 1
297-
resume(())
298-
}
299-
}
300-
}
301-
302-
/// Return just first element of the given stream.
303-
/// Raises MissingValue if no elements are emitted
304-
// TODO could be in stream
305-
def first[A]{ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
306-
try {
307-
body()
308-
val r: A = do raise[MissingValue](MissingValue(), "code in first did not emit any values")
309-
r
310-
} with emit[A] { a => a }
311-
}
312283
// Literals/splices
313284
// ----------------
314285
/// Splices allowed in bit stream literals

libraries/common/stream.effekt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,34 @@ def pad[A](fac: Int){ gen: => A }: Unit / {getPos, emit[A]} = {
531531
def tracking[A]{ body: => Unit / { emit[A], getPos } }: Unit / emit[A] =
532532
tracking[A](0){body}
533533

534+
/// Return just the nth element of the given stream.
535+
/// Raises MissingValue if not enough elements are emitted
536+
def nth[A](n: Int){ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
537+
var m = n
538+
try {
539+
body()
540+
val r: A = do raise[MissingValue](MissingValue(), "code in first did not emit any values")
541+
r
542+
} with emit[A] { a =>
543+
if (m == 0) {
544+
a
545+
} else {
546+
m = m - 1
547+
resume(())
548+
}
549+
}
550+
}
551+
552+
/// Return just the first element of the given stream.
553+
/// Raises MissingValue if no elements are emitted
554+
def first[A]{ body: => Unit / emit[A] }: A / Exception[MissingValue] = {
555+
try {
556+
body()
557+
val r: A = do raise[MissingValue](MissingValue(), "code in first did not emit any values")
558+
r
559+
} with emit[A] { a => a }
560+
}
561+
534562
namespace returning {
535563

536564
/// Canonical handler of push streams that performs `action` for every

0 commit comments

Comments
 (0)