Live data from Hacker News

Ask HN: What is the most beautiful piece of code you've ever read?

news.ycombinator.com

361–370 of 394 posts

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#361

A fully graphical calculator app in Rebol is just a few lines of code. The formatting looks pretty legible at the below link if you scroll down a bit. REBOL [title: "Calculator"] view layout [ origin 0 space 0x0 across style btn btn 50x50 [append f/text face/text show f] f: field 200x40 font-size 20 return btn "1" btn "2" btn "3" btn " + " return btn "4" btn "5" btn "6" btn " - " return btn "7" btn "8" btn "9" btn "…

Nice to see Rebol and Red mentioned!

Here's a Red version of the calculator: https://github.com/red/code/blob/master/Showcase/calculator.... You can see the syntax is very close. We strive for compatibility with Rebol, but are also changing things that we feel could be improved.

A small twist on the UI can use a `panel`, which supports a divider option to make grid layout easier. e.g., for 4 items across, you can do this:

  view [
     style b: button 50x50 bold
     panel 4 [
          b "1"  b "2"  b "3"  b "+"
          b "4"  b "5"  b "6"  b "-"
          b "7"  b "8"  b "9"  b "*"
          b "0"  b "."  b "/"  b "=" 
     ] 
  ]
Red is fully open source, and can be compiled. Most code anyway. You can write code that is too dynamic to compile until we go JIT. Red is bootstrapped in Rebol, but will be self hosted next year. It is also its own, entire, toolchain. Red compiles to Red/System, a low (C) level dialect/EDSL, which compiles directly to machine code. You can mix and match the two in apps as well.

We're still alpha, and have a lot of work ahead: https://www.red-lang.org/

If you liked Rebol, check us out, and help create the future.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#362

A fully graphical calculator app in Rebol is just a few lines of code. The formatting looks pretty legible at the below link if you scroll down a bit. REBOL [title: "Calculator"] view layout [ origin 0 space 0x0 across style btn btn 50x50 [append f/text face/text show f] f: field 200x40 font-size 20 return btn "1" btn "2" btn "3" btn " + " return btn "4" btn "5" btn "6" btn " - " return btn "7" btn "8" btn "9" btn "…

Nice to see Rebol and Red mentioned! Here's a Red version of the calculator: https://github.com/red/code/blob/master/Showcase/calculator.... You can see the syntax is very close. We strive for compatibility with Rebol, but are also changing things that we feel could be improved. A small twist on the UI can use a `panel`, which supports a divider option to make grid layout easier. e.g., for 4 items across, you can do…

Our main chat is at https://gitter.im/red/red

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#363
post #24

The one that blew my mind when I was in college was a simplified version of quicksort in Haskell. It's just so elegant and clean. quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) where lesser = filter ( = p) xs Now surely someone may come along and point out how this isn't a true quicksort[0] because it doesn't partition the elements in place, but…

Though it's not true quicksort, it's possible to explain the idea of this algorithm with code like this in just a few minutes while the truest implementation in C with Hoare partition looks really confusing

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#364
This grammar of the aⁿbⁿ language in Prolog's Definite Clause Grammar syntactic sugar:

  s --> a,b.
  s --> a,s,b.
  a --> [a].
  b --> [b].
s is a non-terminal, a and b are preterminals, [a] and [b] are terminals and "-->" can be read as "expands to". The syntax is the same as BNF and the grammar is a Prolog program that is directly executable as both a recogniser or a generator, depending on instantiation pattern at call time.

How does the grammar work? It must accept, or generate, a string of equal numbers of a's and b's, but the grammar is not keeping track of the length. There is nothing to count how many a's have been consumed or produced so far. How does it know?

s is the start symbol of the grammar. The first production of s, which is also the terminating condition for the recursion, accepts or produces one a followed by one b. The second production of s accepts an a, followed by an s-string, followed by a b.

Suppose we executed the grammar as a generator. In the first step, the output would be the string S₁ = ab. In the second step, the output would be the string S₂ = aS₁b. In the n'th step the output would be aSₙb.

So the grammar would always add exactly one a at the start, and one be at the end of its output, recursively.

And it would always generate the same number of a's as b's.

Similar for when it runs as an acceptor.

You can visualise the first couple of steps as follows:

           S
   ,-------|-------.
   |       S       |
   A      / \      B
   |     A   B     |
   a     |   |     b
         a   b

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#365
post #66

I began coding in IBM/LCSI PC Logo. The first line of code I ever wrote was: FD 100 That's the "hello, world" of turtle graphics in Logo. While probably not as beautiful as the several splendid examples posted in this thread, that simple line of code changed my world. I could make stuff happen in an otherwise mostly blank monochrome CRT display. Until then I had seen CRTs in televisions where I had very little contro…

Cool, we got Logo, BASIC and now Prolog. All one needs to begin programming, eh.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#366

Earlier quoted context omitted.

> In almost every other language this code would look messy or use some terrible recursion. Nah, lots of other languages can do this. Python: def factorial(i): return reduce(operator.mul, range(2, i+1), 1) Ruby: def factorial(i) (2..i).reduce(1, :*) end Haskell: factorial n = foldl (*) 1 [2..n]

For Python, we don't need to supply the optional initializer 1. And reduce resides in the functools namespace (much like mul is in operator). I assume we're not talking about that peculiar dialect of Python that is no longer supported a few months from now.

> For Python, we don't need to supply the optional initializer 1.

Without the initializer, factorial(0) doesn't work.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#367
for(int i=1;iSounds trivial compared to the rest listed here, but for me it was just the first time I got a for loop to work in java.

Like, conceptually I knew programming was about getting machines to doStuff, but this was probably the first time I actually had a machine do something I asked of it directly. Well, that and Logo writer.

https://twitter.com/dosnostalgic/status/726236764759842817

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#368
post #249

https://www.cs.princeton.edu/courses/archive/spr09/cos333/be... Something completely different: the Factor language, in the beginning.

The link goes to an article about Rob Pike's regular expression matcher (which is great BTW).

Looking for the "beginning" of the Factor language led me to this wonderful site about concatenative languages:

https://concatenative.org/wiki/view/Concatenative%20language

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#369

Pick a random line from a file / stream without knowing how many lines there are to choose from in one pass without storing the lines that have been seen. perl -e 'while( ){$x=$_ if rand() For each line, pick that line as your random line if a random number (0 It hits my elegant bone. Only one line... rand < 1/1, pick it. Two lines, same as one, but the second line has a 1/2 change of replacing line one. Third line s…

Same number of keystrokes, but IMHO more idiomatic & readable: perl -ne '$x=$_ if rand()

Ha, that's how I originally wrote it, but I thought I should get rid of -n and END{} in an attempt to ward of the eww Perl comments. Sigh.

But at least now I know that it's called Reservoir Sampling. I had wondered how to generalize it to wanting N lines.

Re: Ask HN: What is the most beautiful piece of code you've ever read?

#370
post #315

Earlier quoted context omitted.

Thank you for the share! This was excellent. Could you leave a sentence or two about how it made you "change the way [you] think about code"?

Because of Norvig's spelling corrector, my measure for good code is whether or not I can understand what it's supposed to be doing, and how it's supposed to work, from reading it in one pass. This code passes that test. Before reading it I would have never thought that could be a goal— I couldn't imagine it was possible to know what a program does without comments and documentation. When I heard the word "readable" f…

Thank you very much for your response!
Post reply on HN