Skip to content

Commit

Permalink
gen: propagate luafun iterators [TO SQUASH]
Browse files Browse the repository at this point in the history
  • Loading branch information
ligurio committed Jul 3, 2024
1 parent 9000134 commit f997ef3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 118 deletions.
22 changes: 22 additions & 0 deletions molly/compat/tbl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Returns a new table, recursively copied from the one given.
--
-- @param table table to be copied
-- @return table
local function tbl_copy(orig)
local orig_type = type(orig)
local copy
if orig_type == "table" then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[tbl_copy(orig_key)] = tbl_copy(orig_value)
end
-- number, string, boolean, etc.
else
copy = orig
end
return copy
end

return {
copy = tbl_copy,
}
133 changes: 15 additions & 118 deletions molly/gen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
local fun = require('fun')
local clock = require('molly.clock')
local log = require('molly.log')
local tbl = require('molly.compat.tbl')

local fun_mt = debug.getmetatable(fun.range(10))
local methods = fun_mt.__index
local exports = table.copy(fun)
local exports = tbl.copy(fun)

local unwrap = function(self)
return self.gen, self.param, self.state
Expand Down Expand Up @@ -85,8 +86,6 @@ end
-- ...
--
--- @function iter
local iter = fun.iter
exports.iter = iter

--- Execute the function `fun` for each iteration value.
-- See [fun.each](https://luafun.github.io/basic.html#fun.each).
Expand All @@ -103,19 +102,14 @@ exports.iter = iter
-- ...
--
-- @function each
local each = fun.each
methods.each = each
exports.each = each

--- An alias for each().
-- See `gen.each`.
-- @function for_each
methods.for_each = each
exports.for_each = each

--- An alias for each().
-- See `gen.each`.
-- @function foreach
methods.foreach = each
exports.foreach = each

--- Generators: Finite Generators
-- @section
Expand Down Expand Up @@ -155,8 +149,6 @@ exports.foreach = each
-- ...
--
-- @function range
local range = fun.range
exports.range = range

--- Generators: Infinity Generators
-- @section
Expand All @@ -174,70 +166,45 @@ exports.range = range
-- ...
--
-- @function duplicate
local duplicate = fun.duplicate
exports.duplicate = duplicate

--- An alias for duplicate().
-- @function xrepeat
-- See `gen.duplicate`.
exports.xrepeat = duplicate

--- An alias for duplicate().
-- @function replicate
-- See `gen.duplicate`.
exports.replicate = duplicate

--- Return `fun(0)`, `fun(1)`, `fun(2)`, ... values indefinitely.
-- @function tabulate
-- See [fun.tabulate](https://luafun.github.io/generators.html#fun.tabulate).
local tabulate = fun.tabulate
exports.tabulate = tabulate

--- Generators: Random sampling
-- @section

--- @function rands
-- See [fun.rands](https://luafun.github.io/generators.html#fun.rands).
local rands = fun.rands
methods.rands = rands
exports.rands = rands

--- Slicing: Subsequences
-- @section

--- @function take_n
-- See [fun.take_n](https://luafun.github.io/slicing.html#fun.take_n).
local take_n = fun.take_n
methods.take_n = take_n
exports.take_n = take_n

--- @function take_while
-- See [fun.take_while](https://luafun.github.io/slicing.html#fun.take_while).
local take_while = fun.take_while
methods.take_while = take_while
exports.take_while = take_while

--- @function take
-- See [fun.take](https://luafun.github.io/slicing.html#fun.take).
local take = fun.take
methods.take = take
exports.take = take

--- @function drop_n
-- See [fun.drop_n](https://luafun.github.io/slicing.html#fun.drop_n).
local drop_n = fun.drop_n
methods.drop_n = drop_n
exports.drop_n = drop_n

--- @function drop_while
-- See [fun.drop_while](https://luafun.github.io/slicing.html#fun.drop_while).
local drop_while = fun.drop_while
methods.drop_while = drop_while
exports.drop_while = drop_while

--- @function drop
-- See [fun.drop](https://luafun.github.io/slicing.html#fun.drop).
local drop = fun.drop
methods.drop = drop
exports.drop = drop

--- @function span
-- See [fun.span](https://luafun.github.io/slicing.html#fun.span).
Expand All @@ -247,85 +214,61 @@ exports.span = span
--- An alias for span().
-- See `fun.span`.
-- @function split
methods.split = span
exports.split = span

--- An alias for span().
-- See `fun.span`.
-- @function split_at
methods.split_at = span
exports.split_at = span

--- Indexing
-- @section

--- @function index
-- See [fun.index](https://luafun.github.io/indexing.html#fun.index).
local index = fun.index
methods.index = index
exports.index = index

--- An alias for index().
-- See `fun.index`.
-- @function index_of
methods.index_of = index
exports.index_of = index

--- An alias for index().
-- See `fun.index`.
-- @function elem_index
methods.elem_index = index
exports.elem_index = index

--- @function indexes
-- See [fun.indexes](https://luafun.github.io/indexing.html#fun.indexes).
local indexes = fun.indexes
methods.indexes = indexes
exports.indexes = indexes

--- An alias for indexes().
-- See `fun.indexes`.
-- @function indices
methods.indices = indexes
exports.indices = indexes

--- An alias for indexes().
-- See `fun.indexes`.
-- @function elem_indexes
methods.elem_indexes = indexes
exports.elem_indexes = indexes

--- An alias for indexes().
-- See `fun.indexes`.
-- @function elem_indices
methods.elem_indices = indexes
exports.elem_indices = indexes

--- Filtering
-- @section

--- Return a new iterator of those elements that satisfy the `predicate`.
-- See [fun.filter](https://luafun.github.io/filtering.html#fun.filter).
-- @function filter
local filter = fun.filter
methods.filter = filter
exports.filter = filter

--- An alias for filter().
-- See `gen.filter`.
-- @function remove_if
methods.remove_if = filter
exports.remove_if = filter

--- If `regexp_or_predicate` is string then the parameter is used as a regular
-- expression to build filtering predicate. Otherwise the function is just an
-- alias for gen.filter().
-- @function grep
-- See [fun.grep](https://luafun.github.io/filtering.html#fun.grep).
local grep = fun.grep
methods.grep = grep
exports.grep = grep

--- The function returns two iterators where elements do and do not satisfy the
-- predicate.
-- @function partition
-- See [fun.partition](https://luafun.github.io/filtering.html#fun.partition).
local partition = fun.partition
methods.partition = partition
exports.partition = partition

--- Reducing: Folds
-- @section
Expand All @@ -334,94 +277,57 @@ exports.partition = partition
-- operator `accfun` and the initial value `initval`.
-- @function foldl
-- See [fun.foldl](https://luafun.github.io/reducing.html#fun.foldl).
local foldl = fun.foldl
methods.foldl = foldl
exports.foldl = foldl

--- An alias to foldl().
-- See `gen.foldl`.
-- @function reduce
methods.reduce = foldl
exports.reduce = foldl

--- Return a number of elements in `gen, param, state` iterator.
-- @function length
-- See [fun.length](https://luafun.github.io/reducing.html#fun.length).
local length = fun.length
methods.length = length
exports.length = length

--- Return a new table (array) from iterated values.
-- @function totable
-- See [fun.totable](https://luafun.github.io/reducing.html#fun.totable).
local totable = fun.totable
methods.totable = totable
exports.totable = totable

--- Return a new table (map) from iterated values.
-- @function tomap
-- See [fun.tomap](https://luafun.github.io/reducing.html#fun.tomap).
local tomap = fun.tomap
methods.tomap = tomap
exports.tomap = tomap

--- Reducing: Predicates
-- @section

--- @function is_prefix_of
-- See [fun.is_prefix_of](https://luafun.github.io/reducing.html#fun.is_prefix_of).
local is_prefix_of = fun.is_prefix_of
methods.is_prefix_of = is_prefix_of
exports.is_prefix_of = is_prefix_of

--- @function is_null
-- See [fun.is_null](https://luafun.github.io/reducing.html#fun.is_null).
local is_null = fun.is_null
methods.is_null = is_null
exports.is_null = is_null

--- @function all
-- See [fun.all](https://luafun.github.io/reducing.html#fun.all).
local all = fun.all
methods.all = all
exports.all = all

--- An alias for all().
-- See `fun.all`.
-- @function every
methods.every = all
exports.every = all

--- @function any
-- See [fun.any](https://luafun.github.io/reducing.html#fun.any).
local any = fun.any
methods.any = any
exports.any = any

--- An alias for any().
-- See `fun.any`.
-- @function some
methods.some = any
exports.some = any

--- Transformations
-- @section

--- @function map
-- See [fun.map](https://luafun.github.io/transformations.html#fun.map).
local map = fun.map
methods.map = map
exports.map = map

--- @function enumerate
-- See [fun.enumerate](https://luafun.github.io/transformations.html#fun.enumerate).
local enumerate = fun.enumerate
methods.enumerate = enumerate
exports.enumerate = enumerate

--- @function intersperse
-- See [fun.intersperse](https://luafun.github.io/transformations.html#fun.intersperse).
local intersperse = fun.intersperse
methods.intersperse = intersperse
exports.intersperse = intersperse

--- Compositions
-- @section
Expand All @@ -434,9 +340,6 @@ exports.intersperse = intersperse
-- @param ... - an iterators
-- @return an iterator
-- @function zip
local zip = fun.zip
methods.zip = zip
exports.zip = zip

--- A cycled version of an iterator.
-- Make a new iterator that returns elements from `{gen, param, state}` iterator
Expand All @@ -450,9 +353,6 @@ exports.zip = zip
-- @return an iterator
-- See [fun.cycle](https://luafun.github.io/compositions.html#fun.cycle).
-- @function cycle
local cycle = fun.cycle
methods.cycle = cycle
exports.cycle = cycle

--- Make an iterator that returns elements from the first iterator until it is
-- exhausted, then proceeds to the next iterator, until all of the iterators are
Expand All @@ -477,9 +377,6 @@ exports.cycle = cycle
-- ...
--
-- @function chain
local chain = fun.chain
methods.chain = chain
exports.chain = chain

--- (TODO) Cycles between several generators on a rotating schedule.
-- Takes a flat series of [time, generator] pairs.
Expand Down

0 comments on commit f997ef3

Please sign in to comment.