by Arne Brasseur / plexus
JRubyConf EU 2013 Lightning talk
FREEZE ALL THE THINGS!
class Foo
def initialize(a,b)
@a, @b = a.freeze, b.freeze
end
end
IceNine!
IceNice.deep_freeze(foo)
def MyCalculation
def self.call ; self.new.call ; end
def initialize(param_x = 7)
@param_x = param_x.freeze
end
def call(in_value) ; out_value ; end
def intermediate
@intermediate ||= @param_x * @param_x
end
end
Callable like a lambda
@calc = MyCalculation
new_value = @calc.(new_value)
class ValueClass
# ...snip...
def |(callable)
callable.call(self)
end
def process(callables)
callables.empty? ? self :
callables.first.(self).process(callables.drop(1))
end
end
new_value = value | MyCalculation \
| MyCalculation.new(5) | ->(v) { v.foo }
new_value = value.process(
MyCalculation,
MyCalculation.new(5),
->(v) { v.foo }
)
What about operators that look like they modify the object?
# This does not work
h << foo
h << bla
# This does... but ugly backslash
h = h << foo \
<< bla
(Re)discovered <<=
h = foo
h <<= H[:title, "Web Linguistics"]
h <<= H[:link, rel: 'stylesheet', href: 'stylesheets/stylesheet.css']
h <<= H[:script, src: file_uri_relative('javascripts/highlight.pack.js')]
def do_x
->(_) { ... }
end
def only_y
->(_) { ... }
end
foo.map(&do_x).filter(&only_y)
Twitter / Github : @plexus