6 ms·
Wait, Ruby doesn't have first class functions? I'm pretty sure it does and that it is full-fledged, compared to Python's intentionally restricted `lambda` synt
by sedeki 6y ago
Wait, Ruby doesn't have first class functions?
I'm pretty sure it does and that it is full-fledged, compared to Python's intentionally restricted `lambda` syntax.
- capableweb 6y agoNope! You can't pass functions, return them from other methods and so on, so it's not first class. What you do have in Ruby is Proc, that can be used kind of like first class functions, so you don't really miss out on much.
- syspec 6y agoAh, so it's the same then
- feanaro 6y agoNot really the same since lambda in Python is just a syntactic variant that is restricted. A function defined using `def` can be passed around without restrictions, which you simply cannot do in Ruby.
- zwp 6y agoMaybe I misunderstand your use of the word "function" here, but in addition to Proc/lambda you can certainly reference a method, pass that around, return it, call it? See: * https://ruby-doc.org/core-2.7.1/Method.html * https://ruby-doc.org/core-2.7.1/Object.html#method-i-method $ irb 2.3.3 :001 > p = Kernel.method(:puts) => #<Method: Kernel.puts> 2.3.3 :002 > p.call "Hello" Hello => nil 2.3.3 :003 > def run_a_method(meth, *args) 2.3.3 :004?> meth.call(*args) 2.3.3 :005?> end => :run_a_method 2.3.3 :006 > run_a_method(p, "Hi!") Hi! => nil 2.3.3 :007 > ps. this also works with the "to_proc" operator (&) so you can do this: [1, 2, 3].each(&Kernel.method(:puts)) (but I wouldn't necessarily recommend this since it's not very readable, at least to my eyes).
- jablan 6y agoYeah, and that's not what first-class usually means. I simply can't say "my_method = Kernel.puts" the same way I can say "my_class = Kernel". Ruby authors seemed to want to make parentheses optional by any price, and that killed the opportunity to make methods first class.
- zwp 6y agoSo the functionality is equivalent, but ruby's syntax is slightly longer. Is there something other than ruby's slightly longer syntax that you find lacking? Ruby's Method class matches the design "everything is an object" (strings, numbers, modules & classes themselves...). Are regular expressions first class in ruby? There is top-level syntactic sugar for REs (/.../) but REs are just instances of `Regexp`. It's just syntax. I don't think you can argue Python REs are first class: I have to drag in `re` and then `re.compile()` a string (and my editor won't know to syntax highlight metachars or interpolation). I use REs more often than method references in either language... But I'm not trying to "whataboutwhataboutwhatabout" here. I'm trying to say there's a difference of philosophy. Ruby Methods are just more objects (Method<Object). They aren't some sort of blessed type, just like everything else. Everything descends from Object. Kernel is just a module. Method is just a class. And so on. In that context, what does it mean to be "first class"? Objects are first class and methods are objects. QED. No? (FWIW I agree on ruby's optional parens, too much inconsistency for a few less lit pixels on the screen).
- capableweb 6y ago> In that context, what does it mean to be "first class" It's easy. First, what does it mean to be "first class"? > a first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, modified, and assigned to a variable Secondly, how does that apply to functions? > [First class functions] means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures In what way doesn't Ruby have first class functions? This article explains it better: https://codequizzes.wordpress.com/2014/05/19/ruby-methods-arent-first-class-but-procs-are-first-class/ https://codequizzes.wordpress.com/2014/05/19/ruby-methods-ar... In practice, because of Proc, you can use Proc wherever you'd use functions and get the same effect in practice, so this is not a big issue. But just because Ruby has Procs, doesn't mean it has first class functions, which is a specific concept.
- burlesona 6y agoThis is incorrect. If you want an anonymous function (with no object context) you can write a lambda, store it to a value, make other lambdas that call it and return yet another lambda etc. You can do the exact same thing with object methods if you want, eg. my_puts = Kernel.method(:puts). You can even do this in one line and stop the auto parenthesis behavior if you want. foo = method def foo ... end Now foo will reference the method and you have to use foo() to call it. The reason that people don’t do a bunch of that in Ruby is that the support for anonymous blocks is so convenient That there aren’t that many cases where you really need anonymous functions detached from any object.