Skip to content

Instantly share code, notes, and snippets.

@laser
Last active January 3, 2016 20:29
Show Gist options
  • Save laser/8515071 to your computer and use it in GitHub Desktop.
Save laser/8515071 to your computer and use it in GitHub Desktop.
Higher-order function
# implemented in the controller action, result from the
# calculator operation is passed to the Repository
# app/controllers/calculation.rb
def create
h = ActiveSupport::JSON.decode(request.body)
fx = lambda do |op, mod, repo|
lambda do |x, y|
result = mod.method(op.to_sym).to_proc[x, y]
repo.save_calculation op, x, y, result
result
end
end
@result = fx[h["operation"], StatelessCalculator, Repository][h["x"], h["y"]]
render :calculation_view
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment