-
Notifications
You must be signed in to change notification settings - Fork 1
/
lengthy.go
37 lines (33 loc) · 876 Bytes
/
lengthy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package stream
import "github.com/primetalk/goio/io"
// Repeat appends the same stream infinitely.
func Repeat[A any](stm Stream[A]) Stream[A] {
return AndThenLazy(stm, func() Stream[A] { return Repeat(stm) })
}
// Take cuts the stream after n elements.
func Take[A any](stm Stream[A], n int) Stream[A] {
if n <= 0 {
return Empty[A]()
} else {
return io.Map[StepResult[A]](stm, func(sra StepResult[A]) StepResult[A] {
nextCount := n
if sra.HasValue {
nextCount = n - 1
}
sra.Continuation = Take(sra.Continuation, nextCount)
return sra
})
}
}
// Drop skips n elements in the stream.
func Drop[A any](stm Stream[A], n int) Stream[A] {
if n <= 0 {
return stm
} else {
return io.Map[StepResult[A]](stm, func(sra StepResult[A]) StepResult[A] {
sra.Continuation = Drop(sra.Continuation, n-1)
sra.HasValue = false
return sra
})
}
}