~prokop/lua-fp

functional style iterators for lua
fix ivals nil returning
'first' collector
c39317c0 — Prokop Randacek 2 years ago
iterator chaining

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~prokop/lua-fp
read/write
git@git.sr.ht:~prokop/lua-fp

You can also use your local clone with git send-email.

#lua functional style iterators

#Example usage

for i, c in str('abcd'):enumerate():map('a*a, b..b') do
	print(i, c)
end
1	aa
4	bb
9	cc
16	dd

#Infinite prime number stream

function is_prime(n)
	return f.range(2, math.ceil(math.sqrt(n)))
		:all(function(i) return (n % i) ~= 0 end)
end

for prime in f.count():filter(is_prime) do
	print(prime)
end

#Iterator chaining

for i in f.range(2) .. f.range(2) .. f.range(2) do
	print(i)
end
0
1
0
1
0
1

#List of implemented functions

  • all
  • any
  • collect
  • count
  • cycle
  • drop
  • enumerate
  • filter
  • first
  • fold
  • ivals (iterator over sequential keys in table)
  • join
  • kvs (table key-value pairs iterator)
  • map
  • ones
  • rand
  • range
  • single
  • str (string iterator)
  • take
  • .. operator (iterator chaining)