GithubHelp home page GithubHelp logo

jcrben / bashfunc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from timonson/bashfunc

0.0 1.0 0.0 4 KB

A small Bash library for functional programming - map, filter, reduce and more

License: MIT License

Shell 100.00%

bashfunc's Introduction

bashFunc

A small Bash library for functional programming. Right now it contains map, filter, reduce, forEach, some and split.

API

Callback

The callback is always the first argument and you can either use normal functions or a string with the function body inside of () as first argument.
These two code blocks are equal:

reduce '( echo $(( $1 + $2 )) )' 0 1 2 3 4 5
# 15
add() { echo $(($1 + $2)); }
reduce add 0 1 2 3 4 5
# 15

Arguments

As data input you can either use positional arguments or the function reads lines from the standard input - like the native mapfile command does - and takes those as arguments. The following example demostrates the two alternatives:

map '( echo $(( $1 + 10 )) )' 1 2 3 4 5
map '( echo $(( $1 + 10 )) )' < <(printf "%s\n" 1 2 3 4 5)
# 11
# 12
# 13
# 14
# 15

Compatibility

You can of course chain different commands together:

reduce '( echo $(( $1 + $2 )) )' < <(filter '( (( $1 % 2 )) )' < <(map '( echo $(( $1 + 10 )) )' 1 2 3 4 5))
# 39

Examples

Map

map '( echo $(( $1 + 10 )) )' < <(printf "%s\n" 1 2 3 4 5)
# 11
# 12
# 13
# 14
# 15

Filter

filter '( (( $1 % 2 )) )' < <(printf "%s\n" 1 2 3 4 5)
# 1
# 3
# 5

Reduce

The second argument, in this case the 10, is the starting value of the accumulator.

reduce '( echo $(( $1 + $2 )) )' 10 < <(printf "%s\n" 1 2 3 4 5)
# 25

ForEach

forEach '( notify-send $1 )' < <(printf "%s\n" 1 2 3 4 5)

Some

The some function tests whether at least one element passes the test implemented by the provided function.

some '((( $1 % 2 )))' < <(printf "%s\n" 1 2 3 4 5)
# echo $?
# 0

Split

The API for this function is quite different. The first argument is a string and the second argument takes a seperator.

split 'Hello,World,Good,Evening' ','
# Hello
# World
# Good
# Evening

bashfunc's People

Contributors

timonson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.