{ "info": { "author": "David Peter", "author_email": "mail@david-peter.de", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Programming Language :: Python", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "# shell-functools\n\n[![Build Status](https://travis-ci.org/sharkdp/shell-functools.svg?branch=master)](https://travis-ci.org/sharkdp/shell-functools)\n\n*A collection of functional programming tools for the shell.*\n\nThis project provides higher order functions like `map`, `filter`, `foldl`, `sort_by` and `take_while` as simple command-line tools.\nFollowing the UNIX philosophy, these commands are designed to be composed via pipes. A\n[large collection](#available-function-arguments) of functions such as `basename`, `replace`, `contains` or `is_dir` are provided as\narguments to these commands.\n\n## Contents\n\n* [Demo](#demo)\n* [Quick start](#quick-start)\n* [Documentation and examples](#documentation-and-examples)\n * [Usage of `map`](#usage-of-map)\n * [Usage of `filter`](#usage-of-filter)\n * [Usage of `foldl`](#usage-of-foldl)\n * [Usage of `foldl1`](#usage-of-foldl1)\n * [Usage of `sort_by`](#usage-of-sort_by)\n * [Chaining commands](#chaining-commands)\n * [Lazy evaluation](#lazy-evaluation)\n * [Working with columns](#working-with-columns)\n * [Available function arguments](#available-function-arguments)\n\n## Demo\n\n\n\n## Quick start\n\nIf you want to try it out on your own, run:\n``` bash\npip install shell-functools\n```\n\nIf you only want to try it out temporarily, you can also use:\n``` bash\ngit clone https://github.com/sharkdp/shell-functools /tmp/shell-functools\nexport PATH=\"$PATH:/tmp/shell-functools/ft\"\n```\n\n## Documentation and examples\n\n### Usage of `map`\n\nThe `map` command takes a [function argument](#available-function-arguments) and applies it to every line of input:\n``` bash\n> ls\ndocument.txt\nfolder\nimage.jpg\n\n> ls | map abspath\n/tmp/demo/document.txt\n/tmp/demo/folder\n/tmp/demo/image.jpg\n```\n\n### Usage of `filter`\n\nThe `filter` command takes a [function argument](#available-function-arguments) with a `Bool`ean return type. It applies that function to each input line and shows only those that returned `true`:\n``` bash\n> find\n.\n./folder\n./folder/me.jpg\n./folder/subdirectory\n./folder/subdirectory/song.mp3\n./document.txt\n./image.jpg\n\n> find | filter is_file\n./folder/me.jpg\n./folder/subdirectory/song.mp3\n./document.txt\n./image.jpg\n```\n\n### Usage of `foldl`\n\nThe `foldl` command takes a [function argument](#available-function-arguments) 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:\n\nAdd up the numbers from 0 to 100:\n``` bash\n> seq 100 | foldl add 0\n5050\n```\n\nMultiply the numbers from 1 to 10:\n``` bash\n> seq 10 | foldl mul 1\n3628800\n```\n\nAppend the numbers from 1 to 10 in a string:\n``` bash\n> seq 10 | map append \" \" | foldl append \"\"\n1 2 3 4 5 6 7 8 9 10\n```\n\n### Usage of `foldl1`\n\nThe `foldl1` command is a variant of `foldl` that uses the first input as the initial value.\nThis can be used to shorten the example above to:\n``` bash\n> seq 100 | foldl1 add\n> seq 10 | foldl1 mul\n> seq 10 | map append \" \" | foldl1 append\n```\n\n### Usage of `sort_by`\n\nThe `sort_by` command also takes a [function argument](#available-function-arguments). In the\nbackground, it calls the function on each input line and uses the results to sort the *original input*.\nConsider the following scenario:\n``` bash\n> ls\na.mp4 b.tar.gz c.txt\n> ls | map filesize\n7674860\n126138\n2214\n```\n\nWe can use the `filesize` function to sort the entries by size:\n```\n> ls | sort_by filesize\nc.txt\nb.tar.gz\na.mp4\n```\n\n### Chaining commands\n\nAll of these commands can be composed by using standard UNIX pipes:\n``` bash\n> find\n.\n./folder\n./folder/me.jpg\n./folder/subdirectory\n./folder/subdirectory/song.mp3\n./document.txt\n./image.jpg\n\n> find | filter is_file | map basename | map append \".bak\"\nme.jpg.bak\nsong.mp3.bak\ndocument.txt.bak\nimage.jpg.bak\n```\n\n### Lazy evaluation\n\nAll commands support lazy evaluation (i.e. they consume input in a streaming way) and never perform\nunnecessary work (they exit early if the *output* pipe is closed).\n\nAs an example, suppose we want to compute the sum of all odd squares lower than 10000. Assuming we\nhave a command that prints the numbers from 1 to infinity (use `alias infinity=\"seq 999999999\"` for\nan approximation), we can write:\n``` bash\n> infinity | filter odd | map pow 2 | take_while less_than 10000 | foldl1 add\n166650\n```\n\n### Working with columns\n\nThe `--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`:\n\n``` bash\n> ls | filter is_file | map split_ext\ndocument\ttxt\nimage\tjpg\n\n> ls | filter is_file | map split_ext | map -c1 to_upper\nDOCUMENT\ttxt\nIMAGE\tjpg\n\n> ls | filter is_file | map split_ext | map -c1 to_upper | map join .\nDOCUMENT.txt\nIMAGE.jpg\n```\n\nHere is a more complicated example:\n``` bash\n> find -name '*.jpg'\n./folder/me.jpg\n./image.jpg\n\n> find -name '*.jpg' | map duplicate\n./folder/me.jpg ./folder/me.jpg\n./image.jpg ./image.jpg\n\n> find -name '*.jpg' | map duplicate | map -c2 basename\n./folder/me.jpg me.jpg\n./image.jpg image.jpg\n\n> find -name '*.jpg' | map duplicate | map -c2 basename | map -c2 prepend \"thumb_\"\n./folder/me.jpg\t thumb_me.jpg\n./image.jpg thumb_image.jpg\n\n> find -name '*.jpg' | map duplicate | map -c2 basename | map -c2 prepend \"thumb_\" | map run convert\nRunning 'convert' with arguments ['./folder/me.jpg', 'thumb_me.jpg']\nRunning 'convert' with arguments ['./image.jpg', 'thumb_image.jpg']\n```\n\nGet the login shell of user `shark`:\n``` bash\n> cat /etc/passwd | map split : | filter -c1 equal shark | map index 6\n/usr/bin/zsh\n```\n\n\n### Available function arguments\n\nYou can call `ft-functions`, to get an overview of all available arguments to `map`, `filter`, etc.:\n\n#### File and Directory operations ####\n```\nabspath :: Path \u2192 Path\ndirname :: Path \u2192 Path\nbasename :: Path \u2192 Path\nis_dir :: Path \u2192 Bool\nis_file :: Path \u2192 Bool\nis_link :: Path \u2192 Bool\nexists :: Path \u2192 Bool\nhas_ext ext :: Path \u2192 Bool\nstrip_ext :: Path \u2192 String\nreplace_ext new_ext :: Path \u2192 Path\nsplit_ext :: Path \u2192 Array\n```\n#### Logical operations ####\n```\nnon_empty :: * \u2192 Bool\nnonempty :: * \u2192 Bool\n```\n#### Arithmetic operations ####\n```\nadd num :: Int \u2192 Int\nsub num :: Int \u2192 Int\nmul num :: Int \u2192 Int\neven :: Int \u2192 Bool\nodd :: Int \u2192 Bool\npow num :: Int \u2192 Int\n```\n#### Comparison operations ####\n```\neq other :: * \u2192 Bool\nequal other :: * \u2192 Bool\nequals other :: * \u2192 Bool\nne other :: * \u2192 Bool\nnot_equal other :: * \u2192 Bool\nnot_equals other :: * \u2192 Bool\nge i :: Int \u2192 Bool\ngreater_equal i :: Int \u2192 Bool\ngreater_equals i :: Int \u2192 Bool\ngt i :: Int \u2192 Bool\ngreater i :: Int \u2192 Bool\ngreater_than i :: Int \u2192 Bool\nle i :: Int \u2192 Bool\nless_equal i :: Int \u2192 Bool\nless_equals i :: Int \u2192 Bool\nlt i :: Int \u2192 Bool\nless i :: Int \u2192 Bool\nless_than i :: Int \u2192 Bool\n```\n#### String operations ####\n```\nappend suffix :: String \u2192 String\nstrip :: String \u2192 String\nsubstr start end :: String \u2192 String\ntake count :: String \u2192 String\nto_lower :: String \u2192 String\nto_upper :: String \u2192 String\nreplace old new :: String \u2192 String\nprepend prefix :: String \u2192 String\ncapitalize :: String \u2192 String\ndrop count :: String \u2192 String\nduplicate :: String \u2192 Array\ncontains substring :: String \u2192 Bool\nstarts_with pattern :: String \u2192 Bool\nstartswith pattern :: String \u2192 Bool\nends_with pattern :: String \u2192 Bool\nendswith pattern :: String \u2192 Bool\nlen :: String \u2192 Int\nlength :: String \u2192 Int\nformat format_str :: * \u2192 String\n```\n#### Array operations ####\n```\nat idx :: Array \u2192 String\nindex idx :: Array \u2192 String\njoin separator :: Array \u2192 String\nsplit separator :: String \u2192 Array\n```\n#### Other operations ####\n```\nconst value :: * \u2192 *\nrun command :: Array \u2192 !\nid :: * \u2192 *\nidentity :: * \u2192 *\n```", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/sharkdp/shell-functools", "keywords": "shell functional-programming filesystem string-manipulation command-line", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "shell-functools", "package_url": "https://pypi.org/project/shell-functools/", "platform": "", "project_url": "https://pypi.org/project/shell-functools/", "project_urls": { "Homepage": "https://github.com/sharkdp/shell-functools" }, "release_url": "https://pypi.org/project/shell-functools/0.3.0/", "requires_dist": null, "requires_python": ">=3.5", "summary": "A collection of functional programming tools for the shell.", "version": "0.3.0" }, "last_serial": 3839354, "releases": { "0.2.0": [ { "comment_text": "", "digests": { "md5": "41df7d6e373c7ffb6718c5256ca8cb1a", "sha256": "606409dc3b13f7da061d44bcee84bb153a54951b4ceae971e78f4413283f46da" }, "downloads": -1, "filename": "shell-functools-0.2.0.tar.gz", "has_sig": false, "md5_digest": "41df7d6e373c7ffb6718c5256ca8cb1a", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.5", "size": 13062, "upload_time": "2018-03-22T06:45:38", "url": "https://files.pythonhosted.org/packages/50/cc/763fe777de0ea969d8f8e547f3949a2aa6512c8597e95f69eb7c4ec06134/shell-functools-0.2.0.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "0b208fdae566f6c099e1bea3807ccacc", "sha256": "b3f99a9748656a56eb6d4335b9057f61e5798ee6578170027aab08c5f51d3091" }, "downloads": -1, "filename": "shell-functools-0.3.0.tar.gz", "has_sig": false, "md5_digest": "0b208fdae566f6c099e1bea3807ccacc", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.5", "size": 15547, "upload_time": "2018-05-06T21:28:01", "url": "https://files.pythonhosted.org/packages/71/89/8badd56d0264f7a38e3832dac59ad1026ef72f642638b3249ae58b87325a/shell-functools-0.3.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "0b208fdae566f6c099e1bea3807ccacc", "sha256": "b3f99a9748656a56eb6d4335b9057f61e5798ee6578170027aab08c5f51d3091" }, "downloads": -1, "filename": "shell-functools-0.3.0.tar.gz", "has_sig": false, "md5_digest": "0b208fdae566f6c099e1bea3807ccacc", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.5", "size": 15547, "upload_time": "2018-05-06T21:28:01", "url": "https://files.pythonhosted.org/packages/71/89/8badd56d0264f7a38e3832dac59ad1026ef72f642638b3249ae58b87325a/shell-functools-0.3.0.tar.gz" } ] }