Last active
May 26, 2025 14:43
-
-
Save balupton/80d27cf1a9e193f8247ee4baa2ad8566 to your computer and use it in GitHub Desktop.
Dorothy: intersect and complement prototypes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove the items from <remove-array-var-name> from <array-var-name>: | |
# __complement <array-var-name> <remove-array-var-name> | |
# remove the <remove-needle>s from <array-var-name>: | |
# __complement <array-var-name> -- ..<remove-needle> | |
# function __complement { | |
# local array_var_name_left="$1" array_var_name_right n_left n_right i_left v_left i_right found result=() | |
# shift | |
# # trunk-ignore(shellcheck/SC1087) | |
# eval "n_left=\${#$array_var_name_left[@]}" | |
# if [[ $1 == '--' ]]; then | |
# shift | |
# local args=("$@") | |
# array_var_name_right='args' | |
# else | |
# array_var_name_right="$2" | |
# fi | |
# # trunk-ignore(shellcheck/SC1087) | |
# eval "n_right=\${#$array_var_name_right[@]}" | |
# for ((i_left = 0; i_left < n_left; ++i_left)); do | |
# for ((i_right = 0; i_right < n_right; ++i_right)); do | |
# eval "v_left=\${$array_var_name_left[i_left]}" | |
# if eval "[[ \$v_left != \"\${$array_var_name_right[i_right]}\" ]]"; then | |
# result+=("$v_left") | |
# fi | |
# done | |
# done | |
# eval "$array_var_name_left=(\${result[@]})" | |
# } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# __intersect <array-var-name> <array-var-name> | |
function __intersect { | |
local array_var_name_left="$1" array_var_name_right="$2" n_left n_right i_left i_right | |
# trunk-ignore(shellcheck/SC1087) | |
eval "n_left=\${#$array_var_name_left[@]}" | |
# trunk-ignore(shellcheck/SC1087) | |
eval "n_right=\${#$array_var_name_right[@]}" | |
for ((i_left = 0; i_left < n_left; ++i_left)); do | |
for ((i_right = 0; i_right < n_right; ++i_right)); do | |
if eval "[[ \"\${$array_var_name_left[i]}\" == \"\${$array_var_name_right[i]}\" ]]"; then | |
eval "__print_lines \"\${$array_var_name_left[i]}\"" | |
break | |
fi | |
done | |
done | |
} | |
# there is a __remove_needles in balupton's dotfiles | |
# __complement <array-var-name> <array-var-name> | |
function __complement { | |
local array_var_name_left="$1" array_var_name_right="$2" n_left n_right i_left i_right found | |
# trunk-ignore(shellcheck/SC1087) | |
eval "n_left=\${#$array_var_name_left[@]}" | |
# trunk-ignore(shellcheck/SC1087) | |
eval "n_right=\${#$array_var_name_right[@]}" | |
for ((i_left = 0; i_left < n_left; ++i_left)); do | |
found='no' | |
for ((i_right = 0; i_right < n_right; ++i_right)); do | |
if eval "[[ \"\${$array_var_name_left[i]}\" == \"\${$array_var_name_right[i]}\" ]]"; then | |
found='yes' | |
break | |
fi | |
done | |
if [[ $found == 'no' ]]; then | |
eval "__print_lines \"\${$array_var_name_left[i]}\"" | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment