Skip to content

add a few new commands (union, intersect, difference, permutations, combinations) - #18467

Merged
fdncred merged 2 commits into
nushell:mainfrom
fdncred:union_intersect_diff_permut_combis
Jun 27, 2026
Merged

fdncred merged 2 commits into
nushell:mainfrom
fdncred:union_intersect_diff_permut_combis

Conversation

@fdncred

@fdncred fdncred commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR adds a few new commands union, intersect, difference, combinations, and permutations as built-in filter commands. These provide immutable, functional list operations.

These commands and presented as a starting point. I'm sure there are edge cases where they do not work but we can continue to work on them as time passes.

User-facing changes (Release notes)

Added some commands for set operations and combinations

Added union, intersect, difference, combinations, and permutations as built-in filter commands. These provide immutable, functional list operations.

Commands

union

Returns a deduplicated list of unique elements present in either the input list or the given list.

[1 2 3 4] | union [3 4 5 6]
# ╭─┬───╮
# │0│ 1 │
# │1│ 2 │
# │2│ 3 │
# │3│ 4 │
# │4│ 5 │
# │5│ 6 │
# ╰─┴───╯

[{a:1} {a:2}] | union [{a:2} {a:3}]
# ╭─┬───────╮
# │0│ a: 1  │
# │1│ a: 2  │
# │2│ a: 3  │
# ╰─┴───────╯
intersect

Returns a deduplicated list of unique elements present in both the input list and the given list.

[1 2 3 4] | intersect [3 4 5 6]
# ╭─┬───╮
# │0│ 3 │
# │1│ 4 │
# ╰─┴───╯

[1 2 3] | intersect [4 5 6]
# ╭──────────╮
# │ empty    │
# ╰──────────╯
difference

Returns a deduplicated list of unique elements present in the input list but not in the given list.

[1 2 3 4] | difference [3 4 5 6]
# ╭─┬───╮
# │0│ 1 │
# │1│ 2 │
# ╰─┴───╯

[{a:1} {a:2} {a:3}] | difference [{a:2} {a:4}]
# ╭─┬───────╮
# │0│ a: 1  │
# │1│ a: 3  │
# ╰─┴───────╯
combinations

Generates all combinations of a given size k from the input list, streamed lazily via ListStream. If k > n, returns an empty list.

[1 2 3] | combinations 2
# ╭─┬──────────╮
# │0│ ╭─┬───╮  │
# │ │ │0│ 1 │  │
# │ │ │1│ 2 │  │
# │ │ ╰─┴───╯  │
# │1│ ╭─┬───╮  │
# │ │ │0│ 1 │  │
# │ │ │1│ 3 │  │
# │ │ ╰─┴───╯  │
# │2│ ╭─┬───╮  │
# │ │ │0│ 2 │  │
# │ │ │1│ 3 │  │
# │ │ ╰─┴───╯  │
# ╰─┴──────────╯

[1 2] | combinations 3
# ╭──────────╮
# │ empty    │
# ╰──────────╯
permutations

Generates all permutations of the input list using Heap's algorithm, streamed lazily via ListStream.

[1 2 3] | permutations
# ╭─┬──────────╮
# │0│ ╭─┬───╮  │
# │ │ │0│ 1 │  │
# │ │ │1│ 2 │  │
# │ │ │2│ 3 │  │
# │ │ ╰─┴───╯  │
# │1│ ╭─┬───╮  │
# │ │ │0│ 2 │  │
# │ │ │1│ 1 │  │
# │ │ │2│ 3 │  │
# │ │ ╰─┴───╯  │
# │2│ ╭─┬───╮  │
# │ │ │0│ 3 │  │
# │ │ │1│ 1 │  │
# │ │ │2│ 2 │  │
# │ │ ╰─┴───╯  │
# │3│ ╭─┬───╮  │
# │ │ │0│ 1 │  │
# │ │ │1│ 3 │  │
# │ │ │2│ 2 │  │
# │ │ ╰─┴───╯  │
# │4│ ╭─┬───╮  │
# │ │ │0│ 2 │  │
# │ │ │1│ 3 │  │
# │ │ │2│ 1 │  │
# │ │ ╰─┴───╯  │
# │5│ ╭─┬───╮  │
# │ │ │0│ 3 │  │
# │ │ │1│ 2 │  │
# │ │ │2│ 1 │  │
# │ │ ╰─┴───╯  │
# ╰─┴──────────╯

[1 2] | permutations
# ╭─┬──────────╮
# │0│ ╭─┬───╮  │
# │ │ │0│ 1 │  │
# │ │ │1│ 2 │  │
# │ │ ╰─┴───╯  │
# │1│ ╭─┬───╮  │
# │ │ │0│ 2 │  │
# │ │ │1│ 1 │  │
# │ │ ╰─┴───╯  │
# ╰─┴──────────╯

Additional notes

@fdncred fdncred added new-command notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Jun 24, 2026
@stormasm

Copy link
Copy Markdown
Contributor

@fdncred I like these commands and will be definitely used by folks who work in certain areas of science, engineering and mathematics as well as computer science.

Way to go ! These are good 😄

@fdncred
fdncred merged commit c642af6 into nushell:main Jun 27, 2026
17 checks passed
@fdncred
fdncred deleted the union_intersect_diff_permut_combis branch June 27, 2026 12:51
@github-actions github-actions Bot added this to the v0.114.0 milestone Jun 27, 2026
@isti115

isti115 commented Jul 18, 2026

Copy link
Copy Markdown

With such an extensive "stdlib" I might even try Advent of Code in nushell this year. 😀 (Or at least some ProjectEuler. 😉)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-command notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants