Module: ProcUtils
- Defined in:
- lib/proc_utils.rb,
lib/proc_utils/version.rb,
lib/proc_utils/core_ext/proc.rb,
lib/proc_utils/core_ext/symbol.rb
Defined Under Namespace
Modules: CoreExt
Constant Summary collapse
- ALL =
i(partial partial_right bind flip wrap compose memoize once)
- SYM =
ALL - i(flip wrap compose)
- VERSION =
"0.1.0"
Class Method Summary collapse
- .bind(subject, receiver, *bound_args) ⇒ Object
- .compose(subject, other) ⇒ Object
- .flip(subject) ⇒ Object
- .memoize(subject) ⇒ Object
- .once(subject) ⇒ Object
- .partial(subject, *bound_args) ⇒ Object
- .partial_right(subject, *bound_args) ⇒ Object
- .wrap(subject, other) ⇒ Object
Class Method Details
.bind(subject, receiver, *bound_args) ⇒ Object
16 17 18 |
# File 'lib/proc_utils.rb', line 16 def bind(subject, receiver, *bound_args) proc { |*args| subject.call(receiver, *bound_args, *args) } end |
.compose(subject, other) ⇒ Object
28 29 30 |
# File 'lib/proc_utils.rb', line 28 def compose(subject, other) proc { |*args| subject.call(*other.call(*args)) } end |
.flip(subject) ⇒ Object
20 21 22 |
# File 'lib/proc_utils.rb', line 20 def flip(subject) proc { |*args| subject.call(*args.reverse) } end |
.memoize(subject) ⇒ Object
32 33 34 35 |
# File 'lib/proc_utils.rb', line 32 def memoize(subject) cache = {} proc { |*args| cache[args] ||= subject.call(*args) } end |
.once(subject) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/proc_utils.rb', line 37 def once(subject) cache, called = nil, false proc do |*args| cache, called = subject.call(*args), true unless called cache end end |
.partial(subject, *bound_args) ⇒ Object
8 9 10 |
# File 'lib/proc_utils.rb', line 8 def partial(subject, *bound_args) proc { |receiver, *args| subject.call(receiver, *bound_args, *args) } end |
.partial_right(subject, *bound_args) ⇒ Object
12 13 14 |
# File 'lib/proc_utils.rb', line 12 def partial_right(subject, *bound_args) proc { |receiver, *args| subject.call(receiver, *args, *bound_args) } end |
.wrap(subject, other) ⇒ Object
24 25 26 |
# File 'lib/proc_utils.rb', line 24 def wrap(subject, other) proc { |*args| subject.call(other, *args) } end |