Module: Functions::PreludeMeta

Extended by:
ClassMethods
Defined in:
lib/functions/prelude_meta.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

create_proc_method, define, define_v1, define_v2, method, reduce_left, reduce_right

Class Method Details

.included(klass) ⇒ Object



5
6
7
# File 'lib/functions/prelude_meta.rb', line 5

def self.included(klass)
  klass.extend ClassMethods
end

Instance Method Details

#after(f, g, x) ⇒ Object



212
213
214
215
216
217
218
219
# File 'lib/functions/prelude_meta.rb', line 212

def after(f, g, x)
  f = f(f)
  g = f(g)
  return g.(par(*f, x)) if Array === f && Proc === g
  return par(*g, f.(x)) if Proc === f && Array === g
  return par(*g, par(*f, x)) if Array === f && Array === g
  return g.(f.(x)) # else
end

#compose(f, g, x) ⇒ Object



206
207
208
209
210
# File 'lib/functions/prelude_meta.rb', line 206

def compose(f, g, x)
  f = f(f)
  g = f(g)
  f.(g.(x))
end

#const(c, x) ⇒ Object



168
169
170
# File 'lib/functions/prelude_meta.rb', line 168

def const(c, x)
  c
end

#divide(arr) ⇒ Object



275
276
277
# File 'lib/functions/prelude_meta.rb', line 275

def divide(arr)
  arr.inject { |a,b| a/b }
end

#f(f) ⇒ Object



160
161
162
# File 'lib/functions/prelude_meta.rb', line 160

def f(f)
  Symbol === f ? f = m(f) : f
end

#filter(a, f) ⇒ Object



255
256
257
# File 'lib/functions/prelude_meta.rb', line 255

def filter(a, f)
  a.select { |x| f(f).(x) }
end

#foldl(f, i, a) ⇒ Object



241
242
243
# File 'lib/functions/prelude_meta.rb', line 241

def foldl(f, i, a)
  a.inject(i) { |r, x| f(f).(r, x) }
end

#foldr(f, i, a) ⇒ Object



245
246
247
# File 'lib/functions/prelude_meta.rb', line 245

def foldr(f, i, a)
  foldl(f(f), i, a.reverse)
end

#from_one_to(to) ⇒ Object



293
294
295
# File 'lib/functions/prelude_meta.rb', line 293

def from_one_to(to)
  Range.new(1, to)
end

#from_to(from, to) ⇒ Object

creates a range

param from [Fixnum] the start of the range param to [Fixnum] the end of the range



289
290
291
# File 'lib/functions/prelude_meta.rb', line 289

def from_to(from, to)
  Range.new(from, to)
end

#id(x) ⇒ Object



164
165
166
# File 'lib/functions/prelude_meta.rb', line 164

def id(x)
  x
end

#lengthObject

Returns calls the length method on the object.

Returns:

  • calls the length method on the object



226
# File 'lib/functions/prelude_meta.rb', line 226

def_method :length

#m(method) ⇒ Object



156
157
158
# File 'lib/functions/prelude_meta.rb', line 156

def m(method)
  lambda(&method(method))
end

#map(a, f) ⇒ Object



251
252
253
# File 'lib/functions/prelude_meta.rb', line 251

def map(a, f)
  a.map { |x| f(f).(x) }
end

#maxInteger

Returns the maximum.

Parameters:

  • the (#max)

    object to call the max method on

Returns:

  • (Integer)

    the maximum



238
# File 'lib/functions/prelude_meta.rb', line 238

def_method :max

#merge(xs, ys, &by) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/functions/prelude_meta.rb', line 180

def merge(xs, ys, &by)

  return xs if ys.empty?
  return ys if xs.empty?

  x, *xt = xs
  y, *yt = ys

  return merge(xt, ys, &by) >> x if block_given? ? by.(x) <= by.(y) : x <= y
  return merge(xs, yt, &by) >> y

end

#merge_sort(xs, &by) ⇒ Object



193
194
195
196
197
# File 'lib/functions/prelude_meta.rb', line 193

def merge_sort(xs, &by)
  return xs if xs.length <= 1 # stopcondition
  left, right = split_in_half(xs)
  merge(merge_sort(left, &by), merge_sort(right, &by), &by)
end

#minObject

Returns calls the min method on the Enumerable.

Parameters:

  • object (#min)

    the object to call the min method on

Returns:

  • calls the min method on the Enumerable



234
# File 'lib/functions/prelude_meta.rb', line 234

def_method :min

#par(*fs, x) ⇒ Object



267
268
269
# File 'lib/functions/prelude_meta.rb', line 267

def par(*fs, x)
  fs.map { |f| f(f).(x) }
end

#parallel(f, g, x) ⇒ Object



263
264
265
# File 'lib/functions/prelude_meta.rb', line 263

def parallel(f, g, x)
  [f(f).(x), f(g).(x)]
end

#quick_sort(list) ⇒ Object



199
200
201
202
203
204
# File 'lib/functions/prelude_meta.rb', line 199

def quick_sort(list)
  return [] if list.size == 0
  pivot, *xs = *list
  less, more = xs.partition { |y| y < pivot }
  quick_sort(less) + [pivot] + quick_sort(more)
end

#reverseObject

Returns calls the reverse method on the object.

Parameters:

  • object

    the target object

Returns:

  • calls the reverse method on the object



230
# File 'lib/functions/prelude_meta.rb', line 230

def_method :reverse

#split_in(xs, n) ⇒ Object



172
173
174
# File 'lib/functions/prelude_meta.rb', line 172

def split_in(xs, n)
  xs.each_slice((xs.length+1)/n).to_a
end

#split_in_half(xs) ⇒ Object



176
177
178
# File 'lib/functions/prelude_meta.rb', line 176

def split_in_half(xs)
  split_in(xs, 2)
end

#sum(a) ⇒ Object



259
260
261
# File 'lib/functions/prelude_meta.rb', line 259

def sum(a)
  a.inject(0, :+)
end

#zip(a, b) ⇒ Object



249
# File 'lib/functions/prelude_meta.rb', line 249

def zip(a, b) a.zip(b) end