Line 15: |
Line 15: |
| function Utils.map(tbl, fn) | | function Utils.map(tbl, fn) |
| local result = {} | | local result = {} |
− | for _, v in pairs(tbl) do | + | for k, v in pairs(tbl) do |
− | table.insert(result, fn(v)) | + | table.insert(result, fn(v, k)) |
| end | | end |
| return result | | return result |
Line 23: |
Line 23: |
| function Utils.filter(tbl, pred) | | function Utils.filter(tbl, pred) |
| local result = {} | | local result = {} |
− | for _, v in pairs(tbl) do | + | for k, v in pairs(tbl) do |
− | if pred(v) then | + | if pred(v, k) then |
| table.insert(result, v) | | table.insert(result, v) |
| end | | end |
| end | | end |
| return result | | return result |
| + | end |
| + | |
| + | function Utils.first(tbl) |
| + | for k, v in pairs(tbl) do |
| + | return k, v |
| + | end |
| end | | end |
| | | |
Line 42: |
Line 48: |
| function Utils.imap(arr, fn) | | function Utils.imap(arr, fn) |
| local result = {} | | local result = {} |
− | for _, v in ipairs(arr) do | + | for i, v in ipairs(arr) do |
− | table.insert(result, fn(v)) | + | table.insert(result, fn(v, i)) |
| end | | end |
| return result | | return result |
Line 50: |
Line 56: |
| function Utils.ifilter(arr, pred) | | function Utils.ifilter(arr, pred) |
| local result = {} | | local result = {} |
− | for _, v in ipairs(arr) do | + | for i, v in ipairs(arr) do |
− | if pred(v) then | + | if pred(v, i) then |
| table.insert(result, v) | | table.insert(result, v) |
| end | | end |
Line 58: |
Line 64: |
| end | | end |
| | | |
− | function Utils.ifirst(tbl) | + | function Utils.ifirst(arr) |
− | for k, v in ipairs(tbl) do | + | for k, v in ipairs(arr) do |
− | return k, v
| |
− | end
| |
− | end
| |
− | | |
− | function Utils.first(tbl)
| |
− | for k, v in pairs(tbl) do
| |
| return k, v | | return k, v |
| end | | end |