I remember seeing Maxwell's equations written in Lisp (I believe it was in the Structure and Interpretation of Programs). This made me feel something very profound, although I am still unable to put this feeling into words.
I really appreciated and missed Scala's `Some`/`None`/`Option` [1] and `lazy` constructs, so much so that I brought them with me to TypeScript.
Option provides an elegant way to handle parameters or results that may or may not be defined. Here's a simple Option implementation: https://gist.github.com/mceachen/75598510275865b8cf88bb2ef80... With this you can write something like Opt(possiblyNullResult).flatMap(ea => functionThatRequiresANonNullResultAndReturnsUndefinedOrDefined(ea)).getOrElse(() => someDefaultValue) And here's a (very) simple implementation of lazy: https://github.com/photostructure/exiftool-vendored.js/blob/... The idea of lazy is to allow deferment of expensive operations until they are actually needed. The above implementation allows for something like: const service = lazy(makeService) Which will ensure makeService is only called once, and the first caller will have to wait for the result of makeService(). All subsequent callers re-use the first result. It's a simple construct, but extremely handy. [1] https://github.com/scala/scala/blob/v2.13.1/src/library/scal...
Lexical Scanning In Go is a charming piece of programming:
https://m.youtube.com/watch?v=HxaD_trXwRE A good debuggable piece of code explains what it is trying to do by being clearly written and conforming to a consistent and logical model. I don’t think I would ever have invented this type of lexer pattern in Go by myself, but would be very grateful to come across it in a code base I had to fix. Like Duff’s Device mentioned in this thread, it has just the right amount of cleverness without becoming inscrutable. Also, if you’ll excuse some avuncular pride, my niece and I wrote some code yesterday. She asked me how many times grandma’s clock chimes every day and we ended up with the following Ruby:
What’s quite elegant about Ruby (in and of itself, not in comparison to your examples) is how consistently it is implemented using its own simple set of core features, with very little magic or syntactic sugar. reduce, Enumerable, what the & operator is doing etc.
What’s so challenging about Ruby is how the language can be abused by library vendors to make all kinds of surprising magic and homegrown syntactic sugar.
> What’s quite elegant about Ruby is how consistently it is implemented using its own simple set of core features, with very little magic or syntactic sugar.
Honestly, I don't think this has much to do with Ruby. This a consequence of using reduce or any other higher-order functions, which come from functional programming, and are now available in almost all modern multi-paradigm programming languages (including Python and Haskell). I you like this kind of constructs, you should definitely learn a functional programming language, you'll love it.
Comments (380) 116 threads
Earlier commentsOption provides an elegant way to handle parameters or results that may or may not be defined.
Here's a simple Option implementation:
https://gist.github.com/mceachen/75598510275865b8cf88bb2ef80...
With this you can write something like
Opt(possiblyNullResult).flatMap(ea => functionThatRequiresANonNullResultAndReturnsUndefinedOrDefined(ea)).getOrElse(() => someDefaultValue)
And here's a (very) simple implementation of lazy:
https://github.com/photostructure/exiftool-vendored.js/blob/...
The idea of lazy is to allow deferment of expensive operations until they are actually needed.
The above implementation allows for something like:
const service = lazy(makeService)
Which will ensure makeService is only called once, and the first caller will have to wait for the result of makeService(). All subsequent callers re-use the first result.
It's a simple construct, but extremely handy.
[1] https://github.com/scala/scala/blob/v2.13.1/src/library/scal...
https://m.youtube.com/watch?v=HxaD_trXwRE
A good debuggable piece of code explains what it is trying to do by being clearly written and conforming to a consistent and logical model. I don’t think I would ever have invented this type of lexer pattern in Go by myself, but would be very grateful to come across it in a code base I had to fix. Like Duff’s Device mentioned in this thread, it has just the right amount of cleverness without becoming inscrutable.
Also, if you’ll excuse some avuncular pride, my niece and I wrote some code yesterday. She asked me how many times grandma’s clock chimes every day and we ended up with the following Ruby:
Python: 2 * sum([x for x in range(1, 13)])
Haskell: 2 * sum [1..12]
Continue this thread — 1 reply
What’s so challenging about Ruby is how the language can be abused by library vendors to make all kinds of surprising magic and homegrown syntactic sugar.
Honestly, I don't think this has much to do with Ruby. This a consequence of using reduce or any other higher-order functions, which come from functional programming, and are now available in almost all modern multi-paradigm programming languages (including Python and Haskell).
I you like this kind of constructs, you should definitely learn a functional programming language, you'll love it.
All comments shown.
Couldn't load more comments. Check your connection and try again.