diff options
-rw-r--r-- | applause.lua | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/applause.lua b/applause.lua index dec82bd..f060789 100644 --- a/applause.lua +++ b/applause.lua @@ -1886,6 +1886,11 @@ function Stream:map(fnc) end Stream["\u{00A8}"] = Stream.map -- APL Double-Dot +--- APL synonym of @{Stream:map}. +-- @function Stream:each +-- @within Class Stream +Stream.each = Stream.map + ScanStream = DeriveClass(MuxableStream) -- We have trailing non-stream arguments @@ -1975,6 +1980,54 @@ function Stream:fold(fnc) return FoldStream:new(self, fnc) end +--- APL synonym for @{Stream:fold}. +-- @function Stream:reduce +-- @within Class Stream +Stream.reduce = Stream.fold + +PartitionStream = DeriveClass(MuxableStream) + +-- We have trailing non-stream arguments +PartitionStream.sig_last_stream = 1 + +function PartitionStream:muxableCtor(stream, size) + self.stream = stream + self.size = size +end + +function PartitionStream:gtick() + local tick = self.stream:gtick() + local size = self.size + local buffer = table.new(size, 0) + + return function() + for i = 1, size do + buffer[i] = tick() + if not buffer[i] then return end + end + + return buffer + end +end + +function PartitionStream:len() + return math.ceil(self.stream:len() / self.size) +end + +--- Partition stream into arrays of the given size. +-- In other words, this buffers the given number of samples +-- before passing them down in an array. +-- If the stream ends before filling the buffer, the incomplete +-- buffer will be discarded. +-- @within Class Stream +-- @int size Size of the chunks, generated by the Stream. +-- @treturn Stream Stream of chunks +-- @usage =tostream{1,2,3,4,5}:partition(2):map(tostream) +-- @fixme The APL partition operation is much more powerful. +function Stream:partition(size) + return PartitionStream:new(self, size) +end + --- -- ZipStream combines any number of streams into a single -- stream using a function. |