3 ms·
> I kind of see Ruby like a hybrid functional language. Functions aren't even first class in Ruby (though it does have closures). I certainly wouldn't describe
by DanWaterworth 13y ago
> I kind of see Ruby like a hybrid functional language.
Functions aren't even first class in Ruby (though it does have closures). I certainly wouldn't describe it as a hybrid functional language.
- riffraff 13y agoI'd refrain from saying "functions aren't first class in ruby", unless you qualify the assertion. E.g. you could mean "you can't call functions by just adding () at the end", or "the block syntax only allows passing one function", but these are both just syntax differences. On the other hand, you can create standalone functions, pass them around, introspect them, invoke them, call methods on them and compose them, which is more or less what you can do with integers in python (we can agree integers are first class objects in python, I think) So there may be reason for saying they are "not first class", but you should expand on it.
- DanWaterworth 13y agoYou're right, I should have backed up that assertion. The reason I say that functions aren't first class is that if you create a function: def factorial(n) if n > 0 n * factorial(n - 1) else 1 end end You can't pass it into another function without creating an object for it. ie, using Kernel.method(:factorial). It's the same with blocks, they aren't first class, in order to construct values, you have to construct objects that holds them.
- stouset 13y agoSo? I can easily write >> Y = ->(&x) { ->(f) { f[f] }[ ->(g) { x[ ->(*n) { g[g][*n] } ] } ] } >> fac = Y.call {|this| -> (n) { n > 0 ? n * this[n - 1] : 1 } } >> fac[5] => 120 You can argue that Ruby doesn't lend itself towards higher-order functional programming, but I asserting that Ruby isn't a function language isn't a tenable position.
- DanWaterworth 13y agoDo you deny that factorial, as I implemented it, is a function? Do you deny that I can't pass factorial into another function without creating an object to hold it? It doesn't matter what else is true. These facts alone mean that ruby's function's aren't first class.