diff --git a/crates/nu-std/std/iter.nu b/crates/nu-std/std/iter.nu index 21f520b0d3..b20d72c0c0 100644 --- a/crates/nu-std/std/iter.nu +++ b/crates/nu-std/std/iter.nu @@ -197,3 +197,26 @@ export def zip-with [ # -> list reduce {|it, acc| do $fn $acc $it } } } + +# Zips two lists and returns a record with the first list as headers +# +# # Example +# ```nu +# use std ["assert equal" "iter iter zip-into-record"] +# +# let res = ( +# [1 2 3] | iter zip-into-record [2 3 4] +# ) +# +# assert equal $res [ +# [1 2 3]; +# [2 3 4] +# ] +# ``` +export def zip-into-record [ # -> table + other: list # the values to zip with +] { + into record + | append ($other | into record) + | headers +} \ No newline at end of file diff --git a/crates/nu-std/tests/test_iter.nu b/crates/nu-std/tests/test_iter.nu index 52f7bff14e..dfdef1f12d 100644 --- a/crates/nu-std/tests/test_iter.nu +++ b/crates/nu-std/tests/test_iter.nu @@ -110,3 +110,17 @@ export def test_iter_flat_map [] { let res = ([1 2 3] | iter flat-map {|it| $it + ($it * 10)}) assert equal $res [11 22 33] } + +export def test_iter_zip_into_record [] { + let headers = [name repo position] + let values = [rust github 1] + + let res = ( + $headers | iter zip-into-record $values + ) + + assert equal $res [ + [name repo position]; + [rust github 1] + ] +}