Module: Functions::Prelude
- Defined in:
- lib/functions/prelude_lambda/math.rb,
lib/functions/prelude_lambda/basic.rb,
lib/functions/prelude_lambda/sorting.rb
Constant Summary collapse
- Max =
Send.(:max)
- Min =
Send.(:min)
- Sum =
->(arr) { arr.inject(0, :+) }
- Multiply =
->(arr) { arr.inject(1, :*) }
- Divide =
->(arr) { arr.inject(:/) }
- Average =
Parallel.(Sum,Length) > Divide
- Power =
->(p,x) { x**p }.curry
- IsDivisor =
->(a, c) { a % c == 0 }
- Divisors =
->(a) { Filter.(IsDivisor.partial(a), FromOneTo.(a)) }
- Gcd =
euclid algorithm
->(a, b) { b == 0 ? a : Gcd.(b, a % b) }
- GcdA =
ReduceLeft.(Gcd)
- Lcm =
mathematical law
->(a, b) { a*b / Gcd.(a, b) }
- LcmA =
ReduceLeft.(Lcm)
- Id =
the identity function
->(x) { x }
- Const =
the constant function
->(c, x) { c }
- Split_In =
splits a list xs in peices of size n
->(n, xs) { xs.each_slice((xs.length+1)/n).to_a }
- Split_In_Half =
splits a list in half
Split_In.curry.(2)
- Merge_By =
merges two ordered lists by a function f that compares the values if no function is given the values are compared by the “<” operator
->(f, xs, ys) do return xs if ys.empty? return ys if xs.empty? x, *xt = xs y, *yt = ys return Merge_By.(f, xt, ys) >> x if f.nil? ? x <= y : f.(x) <= f.(y) return Merge_By.(f, xs, yt) >> y end
- Merge =
merges two list by the natural comparison operator <
Merge_By.partial(nil)
- Compose =
composes two functions
->(f, g, x) { f.(g.(x)) }.curry
- ComposeCurried =
manually curried version of the Compose function
->(f) { ->(g) { ->(x) { f.(g.(x)) } } }
- After =
composes two functions in reverse sequence
->(f, g, x) { f = Par.(f) if Array === f; g = Par.(g) if Array === g; g.(f.(x)) }.curry
- AfterCurried =
manually curried Version of the After composition function
->(f) { ->(g) { ->(x) { f = Par.(f) if Array === f; g = Par.(g) if Array === g; g.(f.(x)) } } }
- Send =
sends the message m to an object o
->(m, o) { o.send(m) }.curry
- Flatten =
Flattens an array
Send.(:flatten)
- Reverse =
Reverses an array
Send.(:reverse)
- Length =
Returns the length
Send.(:length)
- Foldl =
->(f, i, a) { a.inject(i) { |r, x| f.(r, x) } }.curry
- ReduceLeft =
->(f, a) { a.inject { |r, x| f.(r, x) } }.curry
- Foldr =
->(f, i, a) { Foldl.(f, i, a.reverse) }
- ReduceRight =
->(f, a) { ReduceLeft.(f, a.reverse) }.curry
- Zip =
->(a, b) { a.zip(b) }.curry
- Merge_Hash =
->(as, bs) { as.merge(bs) { |k, a, b| [a, b] } }.curry
- Zip_Hash_Left =
->(as, bs) { as.each_with_object({}) { |(k, a), h| h[k] = [a, bs[k]]; h } }.curry
- Zip_Hash_Inner =
->(as, bs) { as.each_with_object({}) { |(k, a), h| b = bs[k]; h[k] = [a, b] if b; h } }.curry
- Zip_Hash_Right =
->(as, bs) { bs.each_with_object({}) { |(k, b), h| h[k] = [as[k], b]; h } }.curry
- Map =
->(f, a) { a.map { |x| f.(x) } }.curry
- Map_Hash =
->(f, h) { Hash[h.map{|k, v| [k, f.(v)] }] }.curry
- Filter =
->(f, xs) { xs.select { |x| f.(x) } }.curry
- Parallel =
->(f, g, x) { [f.(x), g.(x)] }.curry
- Par =
->(fs, x) { fs.map { |f| f.(x) } }.curry
- Intersect =
->(as) { as.inject(:&) }
- Group =
->(f,a) { a.group_by(&f) }.curry
- Values =
Send.(:values)
- Partition =
TODO investigate semantics
->(f) { Group.(f) > Values }
- FromTo =
->(from) { ->(to) { Range.new(from, to) } }
- FromOneTo =
FromTo.(1)
- Count_By =
->(f,a) { a.inject( Hash.new(0) ) { |h,e| h[f.(e)] += 1; h } }.curry
- Count =
count_by = ->(f) { group_by.( f ) < map_hash.( send.(:length) ) }
->(a) { a.inject( Hash.new(0) ) { |h,e| h[e] += 1; h } }
- Merge_Sort_By =
->(f, xs) do return xs if xs.length <= 1 # stopcondition left, right = Split_In_Half.(xs) Merge_By.(f, Merge_Sort_By.(f, left), Merge_Sort_By.(f, right)) end
- Merge_Sort =
Merge_Sort_By.partial(nil)
- Quick_Sort_By =
Merge_Sort_By.partial(Identity)
->(f, list) do return [] if list.size == 0 return list if list.size == 1 pivot, *xs = *list smaller_than = f.nil? ? ->(y) { y < pivot } : ->(y) { f.(y) < f.(pivot) } less, more = xs.partition &smaller_than Quick_Sort_By.(f, less) + [pivot] + Quick_Sort_By.(f, more) end
- Quick_Sort =
Quick_Sort_By.partial(nil)