• Stars
    star
    1,133
  • Rank 39,449 (Top 0.9 %)
  • Language
    Python
  • License
    MIT License
  • Created over 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Functional programming tools for the shell

shell-functools

Build Status

A collection of functional programming tools for the shell.

This project provides higher order functions like map, filter, foldl, sort_by and take_while as simple command-line tools. Following the UNIX philosophy, these commands are designed to be composed via pipes. A large collection of functions such as basename, replace, contains or is_dir are provided as arguments to these commands.

Contents

Demo

Quick start

If you want to try it out on your own, run:

pip install shell-functools

If you only want to try it out temporarily, you can also use:

git clone https://github.com/sharkdp/shell-functools /tmp/shell-functools
export PATH="$PATH:/tmp/shell-functools/ft"

Documentation and examples

Usage of map

The map command takes a function argument and applies it to every line of input:

> ls
document.txt
folder
image.jpg

> ls | map abspath
/tmp/demo/document.txt
/tmp/demo/folder
/tmp/demo/image.jpg

Usage of filter

The filter command takes a function argument with a Boolean return type. It applies that function to each input line and shows only those that returned true:

> find
.
./folder
./folder/me.jpg
./folder/subdirectory
./folder/subdirectory/song.mp3
./document.txt
./image.jpg

> find | filter is_file
./folder/me.jpg
./folder/subdirectory/song.mp3
./document.txt
./image.jpg

Usage of foldl

The foldl command takes a function argument and an initial value. The given function must be a binary function with two arguments, like add or append. The foldl command then applies this function iteratively by keeping an internal accumulator:

Add up the numbers from 0 to 100:

> seq 100 | foldl add 0
5050

Multiply the numbers from 1 to 10:

> seq 10 | foldl mul 1
3628800

Append the numbers from 1 to 10 in a string:

> seq 10 | map append " " | foldl append ""
1 2 3 4 5 6 7 8 9 10

Usage of foldl1

The foldl1 command is a variant of foldl that uses the first input as the initial value. This can be used to shorten the example above to:

> seq 100 | foldl1 add
> seq 10 | foldl1 mul
> seq 10 | map append " " | foldl1 append

Usage of sort_by

The sort_by command also takes a function argument. In the background, it calls the function on each input line and uses the results to sort the original input. Consider the following scenario:

> ls
a.mp4  b.tar.gz  c.txt
> ls | map filesize
7674860
126138
2214

We can use the filesize function to sort the entries by size:

> ls | sort_by filesize
c.txt
b.tar.gz
a.mp4

Chaining commands

All of these commands can be composed by using standard UNIX pipes:

> find
.
./folder
./folder/me.jpg
./folder/subdirectory
./folder/subdirectory/song.mp3
./document.txt
./image.jpg

> find | filter is_file | map basename | map append ".bak"
me.jpg.bak
song.mp3.bak
document.txt.bak
image.jpg.bak

Lazy evaluation

All commands support lazy evaluation (i.e. they consume input in a streaming way) and never perform unnecessary work (they exit early if the output pipe is closed).

As an example, suppose we want to compute the sum of all odd squares lower than 10000. Assuming we have a command that prints the numbers from 1 to infinity (use alias infinity="seq 999999999" for an approximation), we can write:

> infinity | filter odd | map pow 2 | take_while less_than 10000 | foldl1 add
166650

Working with columns

The --column / -c option can be used to apply a given function to a certain column in the input line (columns are separated by tabs). Column arrays can be created by using functions such as duplicate, split sep or split_ext:

> ls | filter is_file | map split_ext
document	txt
image	jpg

> ls | filter is_file | map split_ext | map -c1 to_upper
DOCUMENT	txt
IMAGE	jpg

> ls | filter is_file | map split_ext | map -c1 to_upper | map join .
DOCUMENT.txt
IMAGE.jpg

Here is a more complicated example:

> find -name '*.jpg'
./folder/me.jpg
./image.jpg

> find -name '*.jpg' | map duplicate
./folder/me.jpg   ./folder/me.jpg
./image.jpg       ./image.jpg

> find -name '*.jpg' | map duplicate | map -c2 basename
./folder/me.jpg   me.jpg
./image.jpg       image.jpg

> find -name '*.jpg' | map duplicate | map -c2 basename | map -c2 prepend "thumb_"
./folder/me.jpg	  thumb_me.jpg
./image.jpg       thumb_image.jpg

> find -name '*.jpg' | map duplicate | map -c2 basename | map -c2 prepend "thumb_" | map run convert
Running 'convert' with arguments ['./folder/me.jpg', 'thumb_me.jpg']
Running 'convert' with arguments ['./image.jpg', 'thumb_image.jpg']

Get the login shell of user shark:

> cat /etc/passwd | map split : | filter -c1 equal shark | map index 6
/usr/bin/zsh

Available function arguments

You can call ft-functions, to get an overview of all available arguments to map, filter, etc.:

File and Directory operations

abspath             :: Path   → Path
dirname             :: Path   → Path
basename            :: Path   → Path
is_dir              :: Path   → Bool
is_file             :: Path   → Bool
is_link             :: Path   → Bool
is_executable       :: Path   → Bool
exists              :: Path   → Bool
has_ext ext         :: Path   → Bool
strip_ext           :: Path   → String
replace_ext new_ext :: Path   → Path
split_ext           :: Path   → Array

Logical operations

non_empty           :: *      → Bool
nonempty            :: *      → Bool

Arithmetic operations

add num             :: Int    → Int
sub num             :: Int    → Int
mul num             :: Int    → Int
even                :: Int    → Bool
odd                 :: Int    → Bool
pow num             :: Int    → Int

Comparison operations

eq other            :: *      → Bool
equal other         :: *      → Bool
equals other        :: *      → Bool
ne other            :: *      → Bool
not_equal other     :: *      → Bool
not_equals other    :: *      → Bool
ge i                :: Int    → Bool
greater_equal i     :: Int    → Bool
greater_equals i    :: Int    → Bool
gt i                :: Int    → Bool
greater i           :: Int    → Bool
greater_than i      :: Int    → Bool
le i                :: Int    → Bool
less_equal i        :: Int    → Bool
less_equals i       :: Int    → Bool
lt i                :: Int    → Bool
less i              :: Int    → Bool
less_than i         :: Int    → Bool

String operations

reverse             :: String → String
append suffix       :: String → String
strip               :: String → String
substr start end    :: String → String
take count          :: String → String
to_lower            :: String → String
to_upper            :: String → String
replace old new     :: String → String
prepend prefix      :: String → String
capitalize          :: String → String
drop count          :: String → String
duplicate           :: String → Array
contains substring  :: String → Bool
starts_with pattern :: String → Bool
startswith pattern  :: String → Bool
ends_with pattern   :: String → Bool
endswith pattern    :: String → Bool
len                 :: String → Int
length              :: String → Int
format format_str   :: *      → String

Array operations

at idx              :: Array  → String
index idx           :: Array  → String
join separator      :: Array  → String
split separator     :: String → Array
reverse             :: Array  → Array

Other operations

const value         :: *      → *
run command         :: Array  → !
id                  :: *      → *
identity            :: *      → *

More Repositories

1

bat

A cat(1) clone with wings.
Rust
45,968
star
2

fd

A simple, fast and user-friendly alternative to 'find'
Rust
31,156
star
3

hyperfine

A command-line benchmarking tool
Rust
18,256
star
4

hexyl

A command-line hex viewer
Rust
8,565
star
5

pastel

A command-line tool to generate, analyze, convert and manipulate colors
Rust
4,746
star
6

insect

High precision scientific calculator with support for physical units
PureScript
3,147
star
7

dbg-macro

A dbg(…) macro for C++
C++
2,719
star
8

cube-composer

A puzzle game inspired by functional programming
PureScript
1,967
star
9

vivid

A themeable LS_COLORS generator with a rich filetype datebase
Rust
1,479
star
10

binocle

a graphical tool to visualize binary data
Rust
964
star
11

diskus

A minimal, fast alternative to 'du -sh'
Rust
936
star
12

numbat

A statically typed programming language for scientific computations with first class support for physical dimensions and units
Rust
734
star
13

great-puzzles

A curated list of great puzzles
484
star
14

purescript-flare

A special-purpose UI library for Purescript
PureScript
286
star
15

lscolors

A Rust library and tool to colorize paths using LS_COLORS
Rust
235
star
16

trigger

Run a user-defined command on file changes
Shell
181
star
17

bugreport

Rust library to collect system and environment information for bug reports
Rust
131
star
18

quizzity

A fast-paced geography quiz
JavaScript
125
star
19

curiosity

How far does your curiosity take you?
HTML
99
star
20

lucid

A simple mock-application for programs that work with child processes
Rust
86
star
21

parachuting-robots

An interactive version of a classic puzzle
PureScript
78
star
22

purescript-sparkle

Infer user interfaces from type signatures
PureScript
75
star
23

stack-inspector

A gdb command to inspect the size of objects on the stack
Python
62
star
24

painless

Painless parameter handling for easy exploration
C++
60
star
25

yinsh

A web-based version of the board game Yinsh
Haskell
59
star
26

purescript-isometric

Isometric rendering for PureScript
PureScript
58
star
27

pysh

Python-enhanced bash scripts
Python
58
star
28

purescript-quantities

Physical quantities and units
PureScript
53
star
29

minicpp

A REPL-style development environment for small C++ snippets
Shell
48
star
30

purescript-ctprelude

A Prelude with names from category theory
PureScript
48
star
31

qrnet

A neural network that decrypts QR codes
Python
43
star
32

riffle

A simple terminal pager (as a library)
Rust
41
star
33

config-files

A set of configuration files for vim, zsh, i3, ...
Python
37
star
34

content_inspector

Fast inspection of binary buffers to guess/determine the type of content
Rust
32
star
35

factorization-diagrams

Interactive factorization diagrams
HTML
27
star
36

argmax

Rust library to deal with "argument too long" errors
Rust
20
star
37

vacuum-robot-simulator

Simulation environment for an autonomous robot
Rust
19
star
38

sub

A command-line tool for string substitutions (RustLab 2019 workshop)
Rust
16
star
39

purescript-format

Type-safe, printf-style formatting for PureScript
PureScript
15
star
40

PaperLocator

Citation parsing to quickly access publications
JavaScript
14
star
41

purescript-decimals

Arbitrary precision numbers for PureScript
PureScript
13
star
42

flare-example

Quick start project for purescript-flare
PureScript
11
star
43

fd-benchmarks

Benchmarks for 'fd'
Shell
10
star
44

tinytag

tinytag is a "URL" shortener for your filesystem
Python
10
star
45

purescript-flaredoc

Interactive documentation with Flare
PureScript
8
star
46

phd-thesis

LaTeX sources for my PhD thesis
TeX
7
star
47

level11

7
star
48

GPE-Solver

Split-step solver for the 3D (dipolar) Gross-Pitaevskii equation
C++
7
star
49

latex-report

A clean and simple LaTeX template for reports
TeX
7
star
50

Pack-Zack

A tag-based manager for packing lists
CSS
7
star
51

dp

Personal website about some of my open source projects, board games and mountainbiking.
HTML
5
star
52

cs231n

My solutions to the CS231n assignments
Jupyter Notebook
4
star
53

imdb-ratings

Statistical analysis of my IMDB movie ratings
Jupyter Notebook
3
star
54

purescript-pairs

Datatype and functions for pairs of values
PureScript
3
star
55

zehntausend

A solver for the dice game Zehntausend
Haskell
3
star
56

gnuplot-mm

GnuPlot bindings for Mathematica
Mathematica
2
star
57

talk-moderne-programmiersprachen

JavaScript
2
star
58

rock-paper-scissors

Interactive regex puzzle
HTML
2
star
59

purescript-gametree

Algorithms for two-player zero-sum games
PureScript
2
star
60

snake-factory

A work-in-progress game with monadic parsers and snakes
PureScript
1
star
61

gomoku

Five in a row
PureScript
1
star