Module: Tins::ProcPrelude

Included in:
Proc
Defined in:
lib/tins/proc_prelude.rb

Overview

Tins::ProcPrelude provides a set of utility methods for creating and composing Proc objects with common functional programming patterns.

This module contains various helper methods that return lambda functions, making it easier to build complex processing pipelines and functional transformations. These are particularly useful in functional programming contexts or when working with higher-order functions.

The methods are typically accessed through the singleton interface:

Proc.array.(1, 2, 3)  # => [1, 2, 3]

Examples:

Basic usage with map_apply in reduce context

# Create a proc that applies a method to each element and accumulates results
proc = Proc.map_apply(:upcase) { |s, upcased| s << upcased }

# Use it with Array#reduce
['hello', 'world'].reduce([], &proc)
# => ['HELLO', 'WORLD']

Using array to convert arguments to list

proc = Proc.array
proc.(1, 2, 3)
# => [1, 2, 3]

Instance Method Summary collapse

Instance Method Details

#apply {|list| ... } ⇒ Proc

Create a proc that applies the given block to a list of arguments.

Yields:

  • (list)

    The block to apply to the arguments

Returns:

  • (Proc)

    A proc that takes arguments and calls the block with them unpacked



32
33
34
35
# File 'lib/tins/proc_prelude.rb', line 32

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

#arrayProc

Create a proc that converts all arguments to a list array.

Returns:

  • (Proc)

    A proc that takes any number of arguments and returns them as a list



62
63
64
# File 'lib/tins/proc_prelude.rb', line 62

def array
  lambda { |*list| list }
end

#call(obj) {|obj| ... } ⇒ Proc

Create a proc that evaluates a block in the context of an object.

Parameters:

  • obj (Object)

    The object to evaluate the block against

Yields:

  • (obj)

    The block to evaluate in the object’s context

Returns:

  • (Proc)

    A proc that takes an object and evaluates the block



54
55
56
57
# File 'lib/tins/proc_prelude.rb', line 54

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

#const(konst = nil) {|konst| ... } ⇒ Proc

Create a proc that returns a constant value.

Parameters:

  • konst (Object) (defaults to: nil)

    The constant value to return (optional)

Yields:

  • (konst)

    Block to compute the constant value if not provided

Returns:

  • (Proc)

    A proc that always returns the same value



125
126
127
128
# File 'lib/tins/proc_prelude.rb', line 125

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

#firstProc Also known as: head

Create a proc that returns the first element (the head) of a list.

Returns:

  • (Proc)

    A proc that takes a list and returns its first element



70
71
72
# File 'lib/tins/proc_prelude.rb', line 70

def first
  lambda { |*list| list.first }
end

#from { ... } ⇒ Proc

Create a proc that calls a method on self with given arguments.

This method uses binding introspection to dynamically determine which method to call, making it useful for creating flexible function references.

Examples:

Dynamic method invocation

def square(x)
  x ** 2
end

proc = Proc.from { :square }
[1, 2, 3].map(&proc)
# => [1, 4, 9]

Yields:

  • The block that should return a method name (symbol)

Returns:

  • (Proc)

    A proc that takes arguments and calls the specified method



154
155
156
157
158
# File 'lib/tins/proc_prelude.rb', line 154

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

#id1Proc

Create a proc that returns its argument unchanged.

Returns:

  • (Proc)

    A proc that takes an element and returns it unchanged



115
116
117
# File 'lib/tins/proc_prelude.rb', line 115

def id1
  lambda { |obj| obj }
end

#lastProc

Create a proc that returns the last element of a list.

Returns:

  • (Proc)

    A proc that takes a list and returns its last element



97
98
99
# File 'lib/tins/proc_prelude.rb', line 97

def last
  lambda { |*list| list.last }
end

#map_apply(my_method, *args) {|x, y| ... } ⇒ Proc

Create a proc that applies a method to an object and then applies the block.

Parameters:

  • my_method (Symbol)

    The method name to call on each element

  • args (Array)

    Additional arguments to pass to the method

Yields:

  • (x, y)

    The block to apply after calling the method

Returns:

  • (Proc)

    A proc that takes two arguments and applies the method + block

Raises:

  • (ArgumentError)

    if no block is provided



44
45
46
47
# File 'lib/tins/proc_prelude.rb', line 44

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

#nth(n) ⇒ Proc

Create a proc that returns the nth element of a list.

Parameters:

  • n (Integer)

    The index of the element to return

Returns:

  • (Proc)

    A proc that takes a list and returns element at index n



134
135
136
# File 'lib/tins/proc_prelude.rb', line 134

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

#rotate(n = 1) ⇒ Proc Also known as: swap

Create a proc that rotates a list by n positions.

Parameters:

  • n (Integer) (defaults to: 1)

    Number of positions to rotate (default: 1)

Returns:

  • (Proc)

    A proc that takes a list and returns it rotated



106
107
108
# File 'lib/tins/proc_prelude.rb', line 106

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

#secondProc

Create a proc that returns the second element of a list.

Returns:

  • (Proc)

    A proc that takes a list and returns its second element



80
81
82
# File 'lib/tins/proc_prelude.rb', line 80

def second
  lambda { |*list| list[1] }
end

#tailProc

Create a proc that returns all elements except the first (the tail) from a list.

Returns:

  • (Proc)

    A proc that takes a list and returns all but the first element



89
90
91
# File 'lib/tins/proc_prelude.rb', line 89

def tail
  lambda { |*list| list[1..-1] }
end