Hacker News new | past | comments | ask | show | jobs | submit login

> Features like first-class functions and lambdas are appearing everywhere.

Smalltalk the proverbial Object Oriented language already had first-class functions a.k.a lambdas (called BlockClosures in Smalltalk). So it's not like you should choose between OOP-classes OR lambdas. Having them both is better than having just one of them.




I can’t amen this enough.

Ironically, Smalltalk syntax for lambdas/closures remains my favorite. No “trailing closure” hack that doesn’t scale and looks ambiguous (looking at you swift). No one line limitation (looking at you Python). No ambiguity between function/method bodies and closures (looking at all of you members of the curly brace Algol descendents). No “you have to reference all of the arguments or you can’t use it” (looking at you Elixir).

There were two capital ironies with Smalltalk’s free functions (closures). Smalltalk USED them. It may have been “objects all the way down”, bit they drank closures the whole way. Both the extensive class library and your own code. I coded and invoked more closures in Smalltalk for a given unit of functionality than I have in any other language to date.

What’s even more ironic to me is that closures/functions in most compiled languages are a semantic facade. You see them in your mental model, but the language/library doesn’t model them for you to interact with. In Smalltalk, you could send messages to closures, AND you could add your own.

I have always found it ironic that in Smalltalk, the “only objects ultimate”, I was actually exposed to closures/functional patterns more than I have been in many other systems that supposedly were more about just that.


For example

    g := [:y :z |
      |f|
      f := [:x | y * x].
      f
    ].

    (g value:3 value:2) value:4.
    (g value:2 value:3) value:4.
https://wiki.c2.com/?SmalltalkBlocksAndClosures


Right, in a language where "everything is an Object" it only makes sense that closures should be Objects as well, and that you can interact with them by sending messages to them. Then add a little bit of syntactic sugar, to make the code more succinct.

Another innovation of Smalltalk which may not be familiar to everybody is that all control structures in Smalltalk are implemented by passing closure-objects as arguments to methods like ifTrue:ifFalse .


> … all control structures in Smalltalk are implemented by…

"… compiler optimises ifTrue:ifFalse: and friends using special opcodes, so as to avoid having to create closures…"

https://stackoverflow.com/questions/32662354/optimising-iftr...


Good point. But that's a technical implementation optimization, which the programmer does not need to be aware of.

It's like saying that functional languages implement tail-recursion as iteration in order to "get rid of" recursion.

But the programmer can still think in terms of recursive calls in their program, and can reason about the correctness of the program by assuming it works by recursion.


> … which the programmer does not need to be aware of…

Except when they do!

When they expect to be able to change those methods, because they have been led to believe they are just methods.


I think Python does allow you to play with closure itself by accessing __closure__. Although it is immutable by default, it is still easy to construct another function from code object and closure directly.


Just like most Lisps had some form of OOP, with CLOS being the one that has won.

Somehow, like always, progress is full of left turns, and we could have been much better if the IT world wasn't busy reinventing them.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: