Sha256: 9ab3385c841dd8075fb7076933c5be02e992db76780b5c2d19355e87c31236e6

Contents?: true

Size: 1.39 KB

Versions: 17

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Utils
    module Proc
      ##
      # TODO: Compose.
      #
      # TODO: Disjunct.
      #
      class Conjunct < Support::Command
        attr_reader :procs

        def initialize(procs)
          @procs = procs
        end

        ##
        # 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.
        # - https://en.wikipedia.org/wiki/Logical_conjunction
        #
        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
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.19.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.18.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.17.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.16.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.15.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.14.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.13.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.12.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.11.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.10.1 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.10.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.9.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.8.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.7.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.6.0 lib/convenient_service/utils/proc/conjunct.rb
convenient_service-0.5.0 lib/convenient_service/utils/proc/conjunct.rb