Dallycot

A linked open code engine

View project on GitHub

Stream Library

The definitions provided by this library operate on streams and/or produce new streams. This library also contains a number of streams representing special sequences.

Functions

downfrom

(x: Numeric) → [Numeric]

Produces a stream of numbers starting with the given number down through zero.

Examples

downfrom(5) = [ 5, 4, 3, 2, 1, 0]
downfrom(0) = [ 0 ]
downfrom(-1) = [ ]
downfrom(3.2) = [ 4, 3, 2, 1, 0 ]

Implementation

y-combinator(
  (self, n) :> (
    (ceil(n) != n) : self(self, ceil(n))
    (n > 0) : [ n, self(self, n - 1) ]
    (n = 0) : [ 0 ]
    (     ) : [ ]
  )
)

insert-after

(stream: [Any], value: Any) → [Any]

Returns a new stream with the given value inserted after the head of the given stream.

Implementation

(stream, value) :> [ stream', value, stream... ]

last

(s: [Any]) → Any

Returns the last value in a finite stream. This function will walk an infinite stream until it encounters some other limit such as running out of processing time.

Examples

last( mean( 1..10 ) ) = 55
last( max( [ 1, 3, 5, 3, 1 ] ) ) = 5

Implementation

y-combinator(
  (self, stream) :> (
    (?(stream...)) : self(self, stream...)
    (            ) : stream'
  )
)

set-first

(stream: [Any], stream-head: Any) → [Any]

Returns a new stream with the provided stream head replacing the head of the given stream.

Implementation

(stream, stream-head) :> stream-head ::> stream...

set-rest

(stream: [Any], stream-tail: [Any]) → [Any]

Returns a new stream with the head of the first stream followed by the second stream.

Implementation

(stream, stream-tail) :> stream' ::> stream-tail

until

(f: Function, stream: [Any]) → [Any]

Returns a new stream with elements from the provided stream until either the end of the stream of until an element in the stream causes the function f to return a true value.

Implementation

y-combinator(
  (self, f, stream) :> (
    (?stream and ~f(stream')) : [ stream', self(self, f, stream...) ]
    (                       ) : ( )
  )
)