(Experimental) Functions to iterates over a list of elements, yielding each in turn to an iteratee function.
Iterates over elements of collection and invokes iteratee for each element. Input to the function can be a pipe output, here-string or file.
test_func(){
printf "print value: %s\n" "$1"
return 0
}
arr1=("a b" "c d" "a" "d")
printf "%s\n" "${arr1[@]}" | collection::each "test_func"
collection::each "test_func" < <(printf "%s\n" "${arr1[@]}") #alternative approach
#output
print value: a b
print value: c d
print value: a
print value: d
# If other function from this library is already used to process the array.
# Then following method could be used to pass the array to the function.
out=("$(array::dedupe "${arr1[@]}")")
collection::each "test_func" <<< "${out[@]}"
Checks if iteratee function returns truthy for all elements of collection. Iteration is stopped once predicate returns false. Input to the function can be a pipe output, here-string or file.
arri=("1" "2" "3" "4")
printf "%s\n" "${arri[@]}" | collection::every "variable::is_numeric"
Iterates over elements of array, returning all elements where iteratee returns true. Input to the function can be a pipe output, here-string or file.
arri=("1" "2" "3" "a")
printf "%s\n" "${arri[@]}" | collection::filter "variable::is_numeric"
#output
1
2
3
Iterates over elements of collection, returning the first element where iteratee returns true. Input to the function can be a pipe output, here-string or file.
arr=("1" "2" "3" "a")
check_a(){
[[ "$1" = "a" ]]
}
printf "%s\n" "${arr[@]}" | collection::find "check_a"
#Output
a
Invokes the iteratee with each element passed as argument to the iteratee. Input to the function can be a pipe output, here-string or file.
opt=("-a" "-l")
printf "%s\n" "${opt[@]}" | collection::invoke "ls"
Creates an array of values by running each element in array through iteratee. Input to the function can be a pipe output, here-string or file.
arri=("1" "2" "3")
add_one(){
i=${1}
i=$(( i + 1 ))
printf "%s\n" "$i"
}
printf "%s\n" "${arri[@]}" | collection::map "add_one"
The opposite of filter function; this method returns the elements of collection that iteratee does not return true. Input to the function can be a pipe output, here-string or file.
arri=("1" "2" "3" "a")
printf "%s\n" "${arri[@]}" | collection::reject "variable::is_numeric"
#Ouput
a
Checks if iteratee returns true for any element of the array. Input to the function can be a pipe output, here-string or file.
arr=("a" "b" "3" "a")
printf "%s\n" "${arr[@]}" | collection::reject "variable::is_numeric"