pharo-project / pharo

Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk.
http://pharo.org
Other
1.2k stars 356 forks source link

[Feature Request] Provide syntactic sugar to make programming in a functional style easier #11587

Open astares opened 2 years ago

astares commented 2 years ago

At ESUG 2022 there was discussed about syntactic sugar to make programming in a functional style easier. Eliminating the ().

See https://twitter.com/StOnSoftware/status/1562411468901031937

image

image

Attila Magyar suggested on Twitter that this can be implemented as a proxy with DNU handler, without adding new syntax. See https://twitter.com/zeroflag/status/1562414140639137792

image

We could provide such a feature in the standard image if people need and upvote this.

zeroflag commented 2 years ago

@astares thanks for sharing. Here is a POC implementation of this: https://gist.github.com/zeroflag/1634116227c3b811f0692db4f2889634

Gist
chain.st
GitHub Gist: instantly share code, notes, and snippets.
LucFabresse commented 2 years ago

using DNU for this is not that good because it requires 2 lookups for each chained message.

guillep commented 2 years ago

I think from all the extensions shown, destructuring assignments are the easiest to integrate:

It would be nice that somebody writes a Phep on this (or in the more complex one). The slides can be a starting point!

dvmason commented 1 year ago

@astares thanks for sharing. Here is a POC implementation of this: https://gist.github.com/zeroflag/1634116227c3b811f0692db4f2889634

Gistchain.stGitHub Gist: instantly share code, notes, and snippets.

Yes, if you look at the https://github.com/dvmason/Pharo-Functional repo, you will see that we already have that as well, but as @LucFabresse mentions, this requires a DNU and perform: for each message, neither of which are super-fast operations!

Gist
chain.st
GitHub Gist: instantly share code, notes, and snippets.
GitHub
GitHub - dvmason/Pharo-Functional: Functional support for Pharo
Functional support for Pharo. Contribute to dvmason/Pharo-Functional development by creating an account on GitHub.
dvmason commented 1 year ago

I think from all the extensions shown, destructuring assignments are the easiest to integrate:

  • less parser friction, it should be implemented without breaking the parser
  • backwards compatible, it relies on syntax that is currently invalid so it will not break existing programs
  • no extra runtime cost (just syntax sugar for collection accesses)

It would be nice that somebody writes a Phep on this (or in the more complex one). The slides can be a starting point!

All of the syntax proposals are backwards compatible... they all are invalid syntax in a standard parser. And all the code and implementation is in the repo: https://github.com/dvmason/Pharo-Functional

I would argue that of the proposals, the parrot operator and collection literals are the easiest, zero-cost options. Destructuring assignments also have the same cost as writing them out.

Initializing locals at point of declaration is also zero-cost and I'd love to see them.

Expressions as messages are more of a mental shift. They can make for some very elegant code, but they aren't quite zero-cost, and the more elegant code may be a matter of taste.