GithubHelp home page GithubHelp logo

ibayramli / zsh-functional Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tarrasch/zsh-functional

0.0 0.0 0.0 54 KB

Higher order functions for zsh

Shell 30.96% Perl 40.62% Makefile 1.65% Perl 6 26.76%

zsh-functional's Introduction

Build Status

ZSH higher order functions

You can look at yogsototh's blogpost for the idea of higher order functions in zsh.

Install

If you are lazy just paste the following lines in your terminal:

cd /tmp && \
curl -O https://raw.github.com/Tarrasch/zsh-functional/master/install.sh && \
chmod u+x install.sh && . ./install.sh && \rm -f ./install.sh

If you want to do it manually, clone this repo to ~/.zsh/functional and add

. ~/.zsh/functional/functional.plugin.zsh

to your .zshrc

Antigen

If you use antigen, you can simply add

antigen-bundle Tarrasch/zsh-functional

Among your other antigen-bundle commands.

Examples and usage

Here are some examples:

$ mapa '$1*2' {1..3}
2
4
6

$ folda '$1+$2' {1..5}
15

$ folda '$acc+$x' {1..5}
15

$ filter 'echo $1|grep a >/dev/null' ab cd ef ada
ab
ada

$ map 'X $1:t Y' ~/.zsh/functional/src/*
X each Y
X filter Y
X fold Y
X map Y

$ map 'result $1' $(mapa '$1+5' $(mapa '$1*2' {1..3}))
result 7
result 9
result 11

$ echo "1\n2\n3" | mapa '$1*2' | mapa '$1+5' | map 'result $1'
result 7
result 9
result 11

Here are some examples with named functions:

$ insideXY(){print -- "X $1 Y"}
$ eachf insideXY a b c d
X a Y
X b Y
X c Y
X d Y

$ add(){print -- $(($1+$2))}
$ foldf add {1..5}
15

Please refer to the tests for complete specifications. The advantages of the cram tests are that they are validated and readable.

Furthermore, the commands will print out their --help if they are provided no arguments.

Naming conventions

I found the lambda expression versions most useful hence they have the shortest (plain) name. Functions ending with an f are those taking in a named function and functions ending with an a will take an arithmetic lambda expression. The examples above should clarify the syntax.

map or each?

map is simply each with an implicit echo, it should feel somewhat intuitive for ruby developers. Note how mapa exists but not eacha, and the contrary for mapf and eachf.

Reading from standard input

All functions will read from stdin if no arguments are given to them. Please see tests/filter.t For a test that lazily generates the first 10 primes.

Contributing

Good idea! Just add a test and implement the new functionality and send away your pull request! :)

Running the tests

We test like antigen does testing.

Creds

Yann Esposito for the HoF idea and big thanks to Sterling's blogpost for discovering and starting implementing the anonymous function features.

zsh-functional's People

Contributors

tarrasch avatar yogsototh avatar dchenbecker avatar swistak35 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.