Sha256: cfb50e130a9c1f15c0f86c1d70c6713bbdaaccdb61370cb3b676111c5a41d4e2

Contents?: true

Size: 1.57 KB

Versions: 51

Compression:

Stored size: 1.57 KB

Contents

require 'tins/memoize'

module Tins
  module ProcPrelude
    def apply(&my_proc)
      my_proc or raise ArgumentError, 'a block argument is required'
      lambda { |list| my_proc.call(*list) }
    end

    def map_apply(my_method, *args, &my_proc)
      my_proc or raise ArgumentError, 'a block argument is required'
      lambda { |x, y| my_proc.call(x, y.__send__(my_method, *args)) }
    end

    def call(obj, &my_proc)
      my_proc or raise ArgumentError, 'a block argument is required'
      obj.instance_eval(&my_proc)
    end

    def array
      lambda { |*list| list }
    end
    memoize_function :array, :freeze =>  true

    def first
      lambda { |*list| list.first }
    end
    memoize_function :first, :freeze =>  true

    alias head first

    def second
      lambda { |*list| list[1] }
    end
    memoize_function :second, :freeze =>  true

    def tail
      lambda { |*list| list[1..-1] }
    end
    memoize_function :tail, :freeze =>  true

    def last
      lambda { |*list| list.last }
    end
    memoize_function :last, :freeze =>  true

    def rotate(n = 1)
      lambda { |*list| list.rotate(n) }
    end

    alias swap rotate

    def id1
      lambda { |obj| obj }
    end
    memoize_function :id1, :freeze =>  true

    def const(konst = nil, &my_proc)
      konst ||= my_proc.call
      lambda { |*_| konst }
    end

    def nth(n)
      lambda { |*list| list[n] }
    end

    def from(&block)
      my_method, binding = block.call, block.binding
      my_self = eval 'self', binding
      lambda { |*list| my_self.__send__(my_method, *list) }
    end
  end
end

Version data entries

51 entries across 49 versions & 7 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/proc_prelude.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/proc_prelude.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/lib/tins/proc_prelude.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/lib/tins/proc_prelude.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/proc_prelude.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/proc_prelude.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/proc_prelude.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/proc_prelude.rb
suzuko-0.1.8 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/lib/tins/proc_prelude.rb
suzuko-0.1.7 vendor/bundle/ruby/2.0.0/gems/tins-1.6.0/lib/tins/proc_prelude.rb
mastermind_adeybee-0.1.4 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/proc_prelude.rb
mastermind_adeybee-0.1.3 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/proc_prelude.rb
mastermind_adeybee-0.1.2 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/proc_prelude.rb
mastermind_adeybee-0.1.1 vendor/bundle/ruby/2.2.0/gems/tins-1.6.0/lib/tins/proc_prelude.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/tins-1.6.0/lib/tins/proc_prelude.rb
tins-1.6.0 lib/tins/proc_prelude.rb
tins-1.5.4 lib/tins/proc_prelude.rb
tins-1.5.3 lib/tins/proc_prelude.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/tins-1.5.1/lib/tins/proc_prelude.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/tins-1.5.2/lib/tins/proc_prelude.rb