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]
Instance Method Summary collapse
-
#apply {|list| ... } ⇒ Proc
Create a proc that applies the given block to a list of arguments.
-
#array ⇒ Proc
Create a proc that converts all arguments to a list array.
-
#call(obj) {|obj| ... } ⇒ Proc
Create a proc that evaluates a block in the context of an object.
-
#const(konst = nil) {|konst| ... } ⇒ Proc
Create a proc that returns a constant value.
-
#first ⇒ Proc
(also: #head)
Create a proc that returns the first element (the head) of a list.
-
#from { ... } ⇒ Proc
Create a proc that calls a method on self with given arguments.
-
#id1 ⇒ Proc
Create a proc that returns its argument unchanged.
-
#last ⇒ Proc
Create a proc that returns the last element of a list.
-
#map_apply(my_method, *args) {|x, y| ... } ⇒ Proc
Create a proc that applies a method to an object and then applies the block.
-
#nth(n) ⇒ Proc
Create a proc that returns the nth element of a list.
-
#rotate(n = 1) ⇒ Proc
(also: #swap)
Create a proc that rotates a list by n positions.
-
#second ⇒ Proc
Create a proc that returns the second element of a list.
-
#tail ⇒ Proc
Create a proc that returns all elements except the first (the tail) from a list.
Instance Method Details
#apply {|list| ... } ⇒ Proc
Create a proc that applies the given block to a list of arguments.
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 |
#array ⇒ Proc
Create a proc that converts all arguments to a list array.
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.
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.
125 126 127 128 |
# File 'lib/tins/proc_prelude.rb', line 125 def const(konst = nil, &my_proc) konst ||= my_proc.() lambda { |*_| konst } end |
#first ⇒ Proc Also known as: head
Create a proc that returns the first element (the head) of a list.
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.
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 |
#id1 ⇒ Proc
Create a proc that returns its argument unchanged.
115 116 117 |
# File 'lib/tins/proc_prelude.rb', line 115 def id1 lambda { |obj| obj } end |
#last ⇒ Proc
Create a proc that returns the last element of a list.
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.
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.
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.
106 107 108 |
# File 'lib/tins/proc_prelude.rb', line 106 def rotate(n = 1) lambda { |*list| list.rotate(n) } end |
#second ⇒ Proc
Create a proc that returns the second element of a list.
80 81 82 |
# File 'lib/tins/proc_prelude.rb', line 80 def second lambda { |*list| list[1] } end |
#tail ⇒ Proc
Create a proc that returns all elements except the first (the tail) from a list.
89 90 91 |
# File 'lib/tins/proc_prelude.rb', line 89 def tail lambda { |*list| list[1..-1] } end |