https://github.com/iagoleal/lazy-streams
Lazy (possibly) infinite linked lists in Lua
https://github.com/iagoleal/lazy-streams
data-structure lua streams
Last synced: 8 months ago
JSON representation
Lazy (possibly) infinite linked lists in Lua
- Host: GitHub
- URL: https://github.com/iagoleal/lazy-streams
- Owner: iagoleal
- License: mit
- Created: 2021-04-04T04:54:20.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-24T19:45:07.000Z (almost 5 years ago)
- Last Synced: 2025-03-13T08:13:40.903Z (about 1 year ago)
- Topics: data-structure, lua, streams
- Language: Lua
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lazy Streams in Lua
| **Build Status** |
|:----------------:|
| [![Build Status][build-img]][build-url] |
A Lua library to manipulate lazy linked lists.
Highly based on Haskell's list type.
## Usage Examples
To calculate all Fibonacci number, you can do:
```lua
s = require("lazy-streams")
local add = function(x,y)
return x+y
end
-- Stream of all Fibonacci numbers
local fibs = s.cons(0, s.new(1, function()
return s.map(add, fibs, fibs:tail())
end))
```
To calculate all prime numbers,
you can do an infinite sieve of Eratosthenes:
```lua
s = require("lazy-streams")
-- Make a non-divisibility test
function notDivisible(p)
return function(x)
return math.fmod(x,p) ~= 0
end
end
-- Sieve of Eratosthenes
function sieve(stream)
local p = stream:head()
return s.new(p, function()
return era(stream:tail():filter(notDivisible(p)))
end)
end
-- Stream of all prime numbers
local primes = sieve(s.range(2))
```
[build-img]: https://github.com/iagoleal/lazy-streams/actions/workflows/ci.yml/badge.svg?branch=master
[build-url]: https://github.com/iagoleal/lazy-streams/actions/workflows/ci.yml