- [Email](mailto:jweissman1986 at gmail.com)
Provides for symbol and proc-based functional composition with |
.
- Give procs superpowers, like composition of procs (and symbols, see below) with
.compose
(aliased to|
) - Other things like symbols get some powers too, like curried procs from symbols with
:join.(' ')
Here are some of the super-powers Proc
and friends will have after including this.
- Compose procs with
|
(this is chainable, chaining is associative) - Compose a proc with itself 'n' times with ^:
any_proc ^ n # any_proc | any_proc | any_proc ...
- Arithmetic sum, product and exponents of a proc with
+
,*
and**
- Memoize a proc with
~
- Apply a proc to every element of an array with
%
- Negate a proc with unary
-
- Automatic
#to_proc
conversion in a compositional pipeline::upcase | :reverse
- Curry with
#call
like:split.(' ')
(courtesy of this approach from StackOverflow) #each
to map theproc
-ified symbol::split.(' ') | :capitalize.each | :reverse
#elements
for a proc to call a message once for each argument
require 'functionalism'
inp = -> { gets }
out = ->(x) { print x }
cap = ->(s) { s.capitalize }
# capture a line from stdin and print
(inp | out).call
# interpose a filter
(inp | cap | out).call
# a more complex pipeline constructed with symbols
pipes = :split.(' ') | :capitalize.each | :reverse | :join.(' ')
pipes.("hello world") #=> "World Hello"
Add to the Gemfile.
gem 'functionalism', github: 'jweissman/functionalism'
Copyright (c) 2016 Joseph Weissman
See {file:LICENSE.txt} for details.