Feature #17043
closedInvokable module for custom Proc-like objects
Description
Ruby beautifully integrates Functional and Object-Oriented Programming, and there's more moving in the direction of supporting Functional Programming. A generalization of the Proc interface would enable users to integrate functional approach into their classic OOP design patterns. One of the obvious examples would be "Command" objects.
Also, generic structures, which Ruby has great support for. Hash now has to_proc
. But, it'd be great to be able to treat sets as predicate functions.
I've put together a prototype that I've found useful in my own work here: https://github.com/delonnewman/invokable.
It works like Enumerable; it can be included in any class that implements a call
method. Then you get to_proc
, curry
, <<
and >>
for right and left composition, and memoize
. More could be added. If you include Invokable::Command
, you can treat your "Command" object as an automatically curried function.
Updated by shyouhei (Shyouhei Urabe) almost 5 years ago
- Status changed from Open to Feedback
While your library seems great, what is proposed in this ticket is not that obvious. How can we help? What can we do?
Updated by delonnewman (Delon Newman) almost 5 years ago
Yeah, I guess I wasn't that clear. I'm curious if an officially blessed "Invokable" module might be a useful thing for the standard library. I obviously don't have the perspective you do, but it seems that could be a powerful thing to make idiomatic for the language as a whole. Whereas, as a gem it could only be idiomatic, at most, for a team.
Also, thanks for the edits (sawa) I didn't realize I wouldn't be able to edit the description once I submitted it, and thank you all for your hard work.
Updated by marcandre (Marc-Andre Lafortune) almost 5 years ago
- Status changed from Feedback to Rejected
But, it'd be great to be able to treat sets as predicate functions
Note that since Ruby 2.5, Set#===
is an alias for include?
, so you can do ary.grep(Set[...])
for example.
I'm curious if an officially blessed "Invokable" module might be a useful thing for the standard library.
Most of the standard library is being made available as separate gems (or removed altogether), so we're basically trying to reduce the standard library, not extend it. Thanks for enquiring though.
Updated by delonnewman (Delon Newman) almost 5 years ago
Got it, that make sense. But, just to be clear my proposal is not to include my module in the standard library, but to include a module that would generalize the notion of a Proc, much as Enumerable
generalizes the notion of a collection. Which could potentially simplify a great many things--perhaps sometime in the future.
Thank you for your consideration and the tip on Set#===
. I'd never used it with grep. And +1 on your efforts around improving Set support (marcandre).