Skip to content

Questions arise while implementing list-ops exercise as library implementation #375

Closed
@glennj

Description

@glennj

I’m implementing list-ops as a library, and I've got some questions about the best way to proceed. Consider

source list_ops.sh
double() { echo $(( $1 * 2 ));}
list=( 1 2 3 4 )
list::map double list   # or
list::map double "${list[@]}"

Should the array be passed by name, or should the elements be passed? For a map function it doesn't really matter, but if the list function is something like intersection it would make more sense to pass 2 array names.

Also, struggling with the best way to return a result array:

  • the safest way is to force the user to pass an array name:
result=()
list::map double list result    # in some order
printf "%s\n" "${result[@]}"
  • the simpler method, command substitution, is subject to word splitting and globbing:
result=( $( list::map double list ) )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions