Class: ConvenientService::Utils::Proc::Conjunct
- Inherits:
-
Support::Command
- Object
- Support::Command
- ConvenientService::Utils::Proc::Conjunct
- Defined in:
- lib/convenient_service/utils/proc/conjunct.rb
Overview
TODO: Compose.
TODO: Disjunct.
Instance Attribute Summary collapse
-
#procs ⇒ Object
readonly
Returns the value of attribute procs.
Instance Method Summary collapse
-
#call ⇒ Object
Creates a conjunction of procs.
-
#initialize(procs) ⇒ Conjunct
constructor
A new instance of Conjunct.
Methods inherited from Support::Command
Constructor Details
#initialize(procs) ⇒ Conjunct
Returns a new instance of Conjunct.
14 15 16 |
# File 'lib/convenient_service/utils/proc/conjunct.rb', line 14 def initialize(procs) @procs = procs end |
Instance Attribute Details
#procs ⇒ Object (readonly)
Returns the value of attribute procs.
12 13 14 |
# File 'lib/convenient_service/utils/proc/conjunct.rb', line 12 def procs @procs end |
Instance Method Details
#call ⇒ Object
Creates a conjunction of procs. All procs should accept only one argument.
For example, let's assume we have two arbitrary functions: f(x) and g(x)
Application of the conjunct
procedure gives a new function h(x) that can be written as follows:
h(x) = f(x) && g(x)
IMPORTANT: Please, learn what is a conjunction before diving into implementation details.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/convenient_service/utils/proc/conjunct.rb', line 31 def call return ->(item) {} if procs.none? return procs.first if procs.one? ## # NOTE: reduce tries to use first element as its initial value if not specified explicitly. # https://ruby-doc.org/core-2.6/Enumerable.html#method-i-reduce # # NOTE: proc can be called by `[]`. # https://ruby-doc.org/core-2.7.1/Proc.html#method-i-5B-5D # procs.reduce(->(item) { true }) { |conjunction, proc| ->(item) { conjunction[item] && proc[item] } } end |