lab42_core
Ruby Core Module Extensions (in the spirit of lab419/core)
Dir
Dir.files "**/*" do | partial_path, full_path |
end
Enumerable
enum.grep2 expr # ===>
enum.partition{ |ele| expr === ele }
Hash
{a: 42, b: 43}.only :a, :c # ===> {a: 42}
Fn
Must be required specifically!
Access to methods
require 'lab42/core/fn'
# Get method of an object
printer = Kernel.fn.puts
printer.( 42 )
2.times(&printer)
# It also extends some enumerable methods for easier execution
%w{hello world}.each printer
Thusly fn
gives us access to methods of objects, but what about instance methods?
Access to instance methods
Is realized via fm
.
require 'lab42/core/fn'
(1..100).reduce Fixnum.fm.+ # ---> 5050
Objects as constant procs
While Object#self
will return the obvious, so will by extension object.fn.self
.
This can be very useful, e.g. in this nice way to create an empty array ;)
(1..100).filter(false.fn.self)
If you hesitate to use this all, have a look into Kernel#const_lambda
Partial Application
f = Array.fm.push :next
[[],[1].map( f ) # ---> [[:next], [1, :next]]
a=[]
f = a.fn.push :first
f.(1) # a ---> [:first, 1]