Sha256: 5633d90444e7b0ac804d9631a86f325ec68dc54d7ae673d233a25e25755f44ed

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

require 'concurrent/promise'

require 'dry/monads/task'

module Dry
  module Monads
    # Lazy is a twin of Task which is always executed on the current thread.
    # The underlying mechanism provided by concurrent-ruby ensures the given
    # computation is evaluated not more than once (compare with the built-in
    # lazy assignement ||= which does not guarantee this).
    class Lazy < Task
      class << self
        # @private
        def new(promise = nil, &block)
          if promise
            super(promise)
          else
            super(Concurrent::Promise.new(executor: :immediate, &block))
          end
        end

        private :[]
      end

      # Forces the compution and returns its value.
      #
      # @return [Object]
      def value!
        @promise.execute.value!
      end
      alias_method :force!, :value!

      # Forces the computation. Note that if the computation
      # thrown an error it won't be re-raised as opposed to value!/force!.
      #
      # @return [Lazy]
      def force
        @promise.execute
        self
      end

      # @return [String]
      def to_s
        state = case promise.state
                when :fulfilled
                  value!.inspect
                when :rejected
                  "!#{ promise.reason.inspect }"
                else
                  '?'
                end

        "Lazy(#{ state })"
      end
      alias_method :inspect, :to_s

      # Lazy constructors
      #
      module Mixin
        # @see Dry::Monads::Lazy
        Lazy = Lazy

        # @see Dry::Monads::Unit
        Unit = Unit

        # Lazy constructors
        module Constructors
          # Lazy computation contructor
          #
          # @param block [Proc]
          # @return [Lazy]
          def Lazy(&block)
            Lazy.new(&block)
          end
        end

        include Constructors
      end
    end

    require 'dry/monads/registry'
    register_mixin(:lazy, Lazy::Mixin)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dry-monads-1.3.4 lib/dry/monads/lazy.rb
dry-monads-1.3.3 lib/dry/monads/lazy.rb
dry-monads-1.3.2 lib/dry/monads/lazy.rb
dry-monads-1.3.1 lib/dry/monads/lazy.rb
dry-monads-1.3.0 lib/dry/monads/lazy.rb
dry-monads-1.2.0 lib/dry/monads/lazy.rb