Module: Tins::ProcCompose
Overview
A module that provides function composition functionality for Proc objects.
This module enables the composition of two functions (procs) such that the result is a new proc that applies the second function to the input, then applies the first function to the result. This follows the mathematical concept of function composition: (f ∘ g)(x) = f(g(x))
Instance Method Summary collapse
-
#compose(other) ⇒ Proc
(also: #*)
Composes this proc with another callable, creating a new proc that applies the other callable first, then applies this proc to its result.
Instance Method Details
#compose(other) ⇒ Proc Also known as: *
Composes this proc with another callable, creating a new proc that applies the other callable first, then applies this proc to its result.
The composition follows the mathematical convention:
(self ∘ other)(args) = self(other(args))
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tins/proc_compose.rb', line 51 def compose(other) block = -> *args { if other.respond_to?(:call) call(*other.call(*args)) else call(*other.to_proc.call(*args)) end } if self.class.respond_to?(:new) self.class.new(&block) else Proc.new(&block) end end |