{"id":13761238,"url":"https://github.com/starwing/luaiter","last_synced_at":"2025-06-11T15:36:10.018Z","repository":{"id":137297517,"uuid":"79372899","full_name":"starwing/luaiter","owner":"starwing","description":"A iteration library for Lua","archived":false,"fork":false,"pushed_at":"2021-01-26T11:50:24.000Z","size":53,"stargazers_count":56,"open_issues_count":2,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-05T01:32:12.901Z","etag":null,"topics":["functional","iteration","iterator-library","lua","luafun","selector-interface"],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starwing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-01-18T18:56:16.000Z","updated_at":"2025-03-24T12:43:39.000Z","dependencies_parsed_at":"2024-01-15T03:59:00.508Z","dependency_job_id":"50717b1e-2cad-4d11-b47b-d114746084ea","html_url":"https://github.com/starwing/luaiter","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Fluaiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Fluaiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Fluaiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Fluaiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starwing","download_url":"https://codeload.github.com/starwing/luaiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251171450,"owners_count":21547112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functional","iteration","iterator-library","lua","luafun","selector-interface"],"created_at":"2024-08-03T13:01:45.072Z","updated_at":"2025-04-27T16:32:08.365Z","avatar_url":"https://github.com/starwing.png","language":"Lua","readme":"Lua Iteration Library\n========================\n[![Build Status](https://travis-ci.org/starwing/luaiter.svg?branch=master)](https://travis-ci.org/starwing/luaiter)\n[![Coverage Status](https://coveralls.io/repos/github/starwing/luaiter/badge.svg?branch=master)](https://coveralls.io/github/starwing/luaiter?branch=master)\n\nluaiter is a rewritten version of [luafun][1]: a high-performance\nfunctional programming library for Lua designed with LuaJIT's trace\ncompiler in mind. luaiter focus plain Lua performance improve and\nfollows the standard Lua iteration protocol.\n\nluaiter has the same [License][2] as Lua itself.\n\nSome improves:\n  - avoid any memory allocation when iteration.\n  - use standard iteration protocol.\n  - support Lua 5.3 bit operators.\n  - add more useful functions like `scan` and `flatmap`.\n  - add a powerful `selector` interface for quick-and-dirty lambda\n    function support.\n\n[1]: https://github.com/rtsisyk/luafun\n[2]: https://www.lua.org/license.html\n\n\nThe standard iteration protocol\n-------------------------------\n\nluafun library use a custom protocol for iteration, makes using other\nLua-spec iterator e.g. `io.lines()`, `string.gmatch` difficult, it\nrequires a iteration state variable. luaiter follows the standard\nprotocol without the per-iteration state variable:\n\n```Lua\nfor var1, ... in iter, state, init do\n   ...\nend\n```\n\nThe first return value of `iter` function `var1` used as the state\nvariable, but it's meaningful: If `iter` function is stateful, i.e.\neach iteration will change the `state` content, then var1 may occurs\nthe duplicate value in iterations. In this case, `init` will be `nil`\nto indicate the beginning of the iteration (note that `nil` will never\noccurs in iteration: it means the end of stream). Otherwise, the\n`iter` will be stateless, means var1 will never repetition during\niteration.\n\n- The stateful iterator example: `map` (remember the original iterator\n  `var1`).\n- The stateless iterator example: `range` (it only use previous `var1`\n  to detect the next `var1`).\n\n\nThe selector interface\n----------------------\n\nluaiter has a very special selector interface, the underscore\n`iter._`. This is a special object that has several functinal:\n\n  - `_[1]` ... `_[9]` called `selector`, they can be used as function\n    that select it's 1st...9th argument, e.g. `_[5]` same as\n    `function(a, b, c, d, e) return e end`. They could shorten as\n    `iter._1` to `iter._9`\n\n  - `_1` to `_9` could used in expression, in this case the expression\n    will return a function that do the calculation, and _1 ... _9\n    means the order of arguments, e.g. `_3 + _1 * _2` same as\n    `function(a, b, c)  return c + a * b end`. This will support all\n    Lua operator that could override by metatable, including `_1[_2]`.\n\n  - if use `_` as a function, it could return a function that call the\n    `_`'s single argument, e.g. `_(print)(_2, _1)` same as\n    `function(a, b) return print(b, a) end`, all underscore expression\n    could be used in all place in call, e.g. `_(_2.each)(_3, _1*_4)`\n    same as `function(a, b, c ,d) return b.each(c, a*d) end`\n\n  - `_.self` returns a table-object, use `_.self(obj).each(_1, _2)`\n    same as `function(a, b) return obj:each(a, b) end`.\n\n  - `_.dots` same as `...`, if use `_.dots` in a expression/call, the\n    generated function will accept vararg arguments.\n\n  - `_.land`, `_.lor`, `_.lnot`, `_.andor` same as `and`, `or`, `not`\n    operator and `a and b or c` expression.\n\n  - used of `_` and `_1` to `_9` may cause `load`/`loadstring` when\n    first call the generated function, every time the underscore\n    expression calculated, a new function will `load`/`loadstring`\n    from expression, so don't write expression in loop. Generate the\n    function, and store it in the iterator will cache the generated\n    function.\n\nA example:\n\n```Lua\n\u003e -- Functional style\n\u003e  print(reduce(_1+_2, 0, map(_1^2, range(100))))\n338350.0\n\n\u003e -- Object-oriented style\n\u003e print(range(100):map(_1^2):reduce(_1+_2))\n338350.0\n```\n\n\nThe interface convention\n------------------------\n\nAll functions that accept a iterator may used as the method of\n`iterator` object. Iterator usually place at the end of interface,\nwhen used as methods, the last iterator will be `self`, e.g. `map`\nfunction has signature: `map(func, iter)`, So use `map` as a method\ncan call like this: `iter:map(func)`\n\nIf a function accept multiple iterators, the first will be the self\niterator, e.g. `zip(iter, iters...)` maybe called as `iter:zip(iters...)`\n\nIf a function doesn't accept a iterator, it can not used as the\nmethod of `iterator` object.\n\n\nThe iterators\n-------------\n\nGenerators:\n  - `range([[first,] last[, step]])`\n  - `rand([first, last])`\n  - `str(string)`\n  - `array(table)`\n  - `resolve(...)`\n  - `dup(...)`\n  - `zeros() == dup(0)`\n  - `ones()  == dup(1)`\n\nSlicing:\n  - `take(n, iter)`\n  - `drop(n, iter)`\n  - `slice(first, last, iter)`\n\nTransforms:\n  - `enumerate(iter)`\n  - `map(func, iter)`\n  - `flatmap(func, iter)`\n  - `scan(func, init, iter)`\n  - `group(n, iter)`\n  - `groupby(func, iter)`\n\nCompositions:\n  - `zip(iters...)`\n  - `interleave(iters...)`\n  - `chain(iters...)`\n  - `cycle(iter)`\n\nFiltering:\n  - `takewhile(func, iter)`\n  - `dropwhile(func, iter)`\n  - `filter(func, iter)`\n  - `fitlerout(func, iter)`\n\nReducing:\n  - `each(func, iter)`\n  - `reduce(func, iter)`\n  - `index(func, iter)`\n  - `collect(t, iter)`\n  - `concat(delim, iter)`\n  - `count(iter)`\n  - `isempty(iter)`\n  - `all(func, iter)`\n  - `any(func, iter)`\n\n\n","funding_links":[],"categories":["Lua"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarwing%2Fluaiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarwing%2Fluaiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarwing%2Fluaiter/lists"}