diff --git a/functional.lua b/functional.lua index 4c0e34a..dfd0d08 100644 --- a/functional.lua +++ b/functional.lua @@ -86,6 +86,19 @@ end --alias functional.remap = functional.map_inplace +--maps a sequence {a, b, c} -> {a[k], b[k], c[k]} +-- (automatically drops any nils to keep a sequence) +function functional.map_field(t, k) + local result = {} + for i = 1, #t do + local v = t[i][k] + if v ~= nil then + table.insert(result, v) + end + end + return result +end + --maps a sequence into a new index space (see functional.map) -- the function may return an index where the value will be stored in the result -- if no index (or a nil index) is provided, it will insert as normal diff --git a/sequence.lua b/sequence.lua index 4277a4a..8d6f540 100644 --- a/sequence.lua +++ b/sequence.lua @@ -67,6 +67,7 @@ end --(common case where something returns another sequence for chaining) for _, v in ipairs({ "map", + "map_field", "filter", "remove_if", "zip",