diff --git a/README.md b/README.md index 27679aa..0a9585d 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ Bash library which provides utility functions and helpers for functional program - [Check](#check) - [check::command_exists()](#checkcommand_exists) - [check::is_sudo()](#checkis_sudo) +- [Collection](#collection) + - [test_func()](#test_func) - [Date](#date) - [date::now()](#datenow) - [date::epoc()](#dateepoc) @@ -476,6 +478,57 @@ check::is_sudo - **0**: If the script is executed with root privilege. - **1**: If the script is not executed with root privilege +## Collection + +(Experimental) Functions to iterates over a list of elements, yielding each in turn to an iteratee function. + +### test_func() + +Iterates over elements of collection and invokes iteratee for each element. +Input to the function can here a pipe output, here-string or file + +#### Example + +```bash +test_func(){ +### collection::each() + + 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 +``` + +#### Example + +```bash +# 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[@]}" +``` + +#### Arguments + +- **$1** (string): Iteratee function. + +#### Exit codes + +- **0**: If successful. +- **2**: Function missing arguments. +- other exitcode returned by iteratee + +#### Output on stdout + +- Output of iteratee function. + ## Date Functions for manipulating dates. diff --git a/bash_utility.sh b/bash_utility.sh index 087ab6e..4ee0eaf 100644 --- a/bash_utility.sh +++ b/bash_utility.sh @@ -9,3 +9,4 @@ source src/date.sh source src/interaction.sh source src/check.sh source src/format.sh +source src/collection.sh diff --git a/src/collection.sh b/src/collection.sh new file mode 100644 index 0000000..0ac6a4f --- /dev/null +++ b/src/collection.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +# @file Collection +# @brief (Experimental) Functions to iterates over a list of elements, yielding each in turn to an iteratee function. + +# @description Iterates over elements of collection and invokes iteratee for each element. +# Input to the function can here a pipe output, here-string or file +# @example +# 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 +# +# @example +# # 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[@]}" +# +# @arg $1 string Iteratee function. +# +# @exitcode 0 If successful. +# @exitcode 2 Function missing arguments. +# @exitcode other exitcode returned by iteratee +# +# @stdout Output of iteratee function. +collection::each() { + [[ $# = 0 ]] && printf "%s: Missing arguments\n" "${FUNCNAME[0]}" && return 2 + + declare IFS=$'\n' + while read -r it; do + if [[ "$@" == *"$"* ]]; then + eval "$@" + else + eval "$@" "'${it}'" + fi + declare -i ret="$?" + + if [[ $ret -ne 0 ]]; then + return $ret + fi + + done +}