Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Created February 11, 2021 15:21
Show Gist options
  • Save DarkWiiPlayer/f01e6ff1d5b60f153aca00b8e72e4a5b to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/f01e6ff1d5b60f153aca00b8e72e4a5b to your computer and use it in GitHub Desktop.
local fun = require 'fun' ()
local function avg(...)
local sum, count = 0, 0
for i=1,select('#', ...) do
count = count + 1
sum = sum + select(i, ...)
end
return sum / count
end
function dice(sides, number)
if number > 0 then
return math.random(sides), dice(sides, number-1)
end
end
duplicate(20, 20) -- An iterator that yields two values
:take(10)
:map(dice) -- calls dice(20, 20), returning 20 integers
:map(avg) -- averages all 20 integers
:each(print)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment