<commit_before>require 'thread'

require 'functional/obligation'

module Functional

  class Future
    include Obligation
    behavior(:future)

    def cancel
      return false if @t.nil? || fulfilled?
      t = Thread.kill(@t)
      unless t.alive?
        @value = nil
        @state = :fulfilled
      end
      return ! t.alive?
    end

    def initialize(*args)

      unless block_given?
        @state = :fulfilled
      else
        @state = :pending
        @t = Thread.new do
          semaphore.synchronize do
            Thread.pass
            begin
              @value = yield(*args)
              @state = :fulfilled
            rescue Exception => ex
              @state = :rejected
              @reason = ex
            end
          end
        end
        @t.abort_on_exception = true
      end
    end
  end
end

module Kernel

  def future(*args, &block)
    return Functional::Future.new(*args, &block)
  end
  module_function :future
end
<commit_msg>Minor refactor to Future--trying to lock down an intermittently failing test.<commit_after>require 'thread'

require 'functional/obligation'

module Functional

  class Future
    include Obligation
    behavior(:future)

    def cancel
      return false if @t.nil? || fulfilled?
      t = Thread.kill(@t)
      unless t.alive?
        @value = nil
        @state = :fulfilled
      end
      return ! t.alive?
    end

    def initialize(*args)

      unless block_given?
        @state = :fulfilled
      else
        @value = nil
        @state = :pending
        @t = Thread.new do
          semaphore.synchronize do
            Thread.pass
            begin
              @value = yield(*args)
              @state = :fulfilled
            rescue Exception => ex
              @state = :rejected
              @reason = ex
            end
          end
        end
        @t.abort_on_exception = true
      end
    end
  end
end

module Kernel

  def future(*args, &block)
    return Functional::Future.new(*args, &block)
  end
  module_function :future
end

{'fraction_non_alphanumeric': 0.07970660146699267, 'fraction_numerical': 0.0, 'mean_word_length': 1.6332046332046333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1426868', 'n_tokens_mistral': 596, 'n_tokens_neox': 580, 'n_words': 168}