Live data from Hacker News

Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

bitbucket.org

241–250 of 304 posts

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#241

Earlier quoted context omitted.

From a guy that was doing cool things with a computer at age 12 but instead of starting to workout at age 12, started at age 18. Any age is good to start working out but sooner is better.

What bizarre advice for a programming thread. And what are those five days for?

spring break

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#242

Excuse my portion of scepticism, it's not the fact that a "12 year old" built this, sure anyone can learn how to code and we can't know how much help this person got along the way. But it's rather the person's language when writing plain text, here in the comments for example. It doesn't feel adolescent but more grownup to me ... But maybe it's just me realising my own shortcommings as a 12 year old, as I only was pl…

I think that to post here, is a good sign. Show HN wherever you are able to do, alone or after a 100M financing is one of the reason forums like this one exists. The other is to just share your code and meet digital people.

> It doesn't feel adolescent but more grownup to me

Showing skepticism is also fine, our industry is not just nice code review and beautiful moments where people want to encourage you.

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#243

Earlier quoted context omitted.

I'm a long time coder and still liked watching your video just for watching someone else work in emacs. How do you get emacs to highlight all occurrences of map? Was it a keybinding for "highlight-phrase"?

This is vim.

I think I just died.

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#244
post #167

Liam, this is incredible ! I thought it might be useful for you to incrementally see how someone in industry would review or change this code, so here's a little code review via video: https://youtu.be/UkVOrcS--04 We very incrementally build up to the final code, which can be found here: https://gist.github.com/stevekrenzel/b490564bf1c7f98e232a6c8... Hope you find it helpful!

Instead of this: (rule & (1 You could just do: (rule >> i) & 1 Otherwise it was a good review! I'd probably also extract parts of this long line so it's not as cumbersome to read: return state.map((item, i) => rules[((state[i - 1] || 0) * 4) + (item * 2) + (state[i + 1] || 0)] ); to return state.map((item, i) => { const prevItem = state[i - 1] || 0; const nextItem = state[i + 1] || 0; return rules[(prevItem It also u…

I like to break expressions across multiple lines, and line up any patterns that are repeated, so your eye can easily scan up and down and obviously see what's the same, and what's different.

For example, instead of:

    var length = Math.sqrt((x * x) + (y * y));
You can go:

    var length = Math.sqrt((x * x) +
                           (y * y));
That way you can see the similarity of squaring x, and squaring y. But breaking up the multiplications because they repeat the x's and y's would be taking it too far -- it always requires balance and thinking about how can I communicate my intent and the SHAPE of the problem I'm solving.

You can also use indentation and formatting to bring out and make obvious other two-dimensional patterns and regularities, especially with two-dimensional cellular automata and graphics code.

There are some examples of that technique in my 2D cellular automata machine code. (There's a lot of code, so don't be overwhelmed, since I've been working on that since around 1985 or so. So never give up and just stick to it! You don't need to understand the code, which is pretty esoteric, just notice the patterns.)

https://github.com/SimHacker/CAM6/blob/master/javascript/CAM...

                        // Load the right two columns of the 3x3 window.
                        n  = cells[cellIndex - nextCol - nextRow];  ne = cells[cellIndex - nextRow];
                        c  = cells[cellIndex - nextCol          ];  e  = cells[cellIndex          ];
                        s  = cells[cellIndex - nextCol + nextRow];  se = cells[cellIndex + nextRow];
[...]

                            // Scroll the 3x3 window to the right, scrolling the middle and right
                            // columns to the left, then scooping up three new cells from the right
                            // leading edge.
                            nw = n;  n = ne;  ne = cells[cellIndex + nextCol - nextRow];
                            w  = c;  c =  e;  e  = cells[cellIndex + nextCol          ];
                            sw = s;  s = se;  se = cells[cellIndex + nextCol + nextRow];
[...]

                            var sum8 =
                                    (nw & 1) + (n & 1) + (ne & 1) +
                                    (w  & 1) +           (e  & 1) +
                                    (sw & 1) + (s & 1) + (se & 1);
[...]

                                var tableIndex =
                                    (((c         >> plane) & 0x03) > plane) & 0x03) > plane) & 0x03) > plane) & 0x03) > plane) & 0x03) > 1    ) & 0x03) 
[...]

When the sub-expressions won't fit on one line, you can still use indentation to help the reader understand the spatial patterns:

                            error +=
                                (nw * kernelBytes[4 - kernelDown - kernelRight]) +
                                    (n  * kernelBytes[4 - kernelDown]) +
                                        (ne * kernelBytes[4 - kernelDown + kernelRight]) +
                                (w  * kernelBytes[4 - kernelRight]) +
                                    (c  * kernelBytes[4]) +
                                        (e  * kernelBytes[4 + kernelRight]) +
                                (sw * kernelBytes[4 + kernelDown - kernelRight]) +
                                    (s  * kernelBytes[4 + kernelDown]) +
                                        (se * kernelBytes[4 + kernelDown + kernelRight]) +
                                frob;
But that would probably be more readable if I used intermediate variables for the kernelBytes[...] expressions, gave them descriptive names matching their corresponding directions, and lined up all their calculations so you could see the similarities and differences, because right now all the kernleBytes[4 +/- kernelDown +/- kernelRight] expressions are jumbled around horizontally, when they could be lined up nicely by themselves if they weren't interleaved with the n/s/e/w/ne/nw/sw/se multiplications. (I'll leave that improvement as an exercise for the reader. ;)

The point is you want to vertically line up as many of the repeated letters and symbols in nice neat columns as possible, so that your eye can easily see which are the same, and the ones that aren't stand out obviously so you can ignore all the same things and focus on the differences. (Which are typically variations like +/-, sin/cos, x/y/z, -1/0/1, nw/n/ne/w/c/e/sw/s/se, and longer series of names and numbers. Whatever changes should stick out visually, and be lined up and grouped together so your eyes can scan over them!)

That not only helps other people understand your code, but it also helps you be sure that you wrote the right thing, what you actually meant, instead of making an easy to miss typo.

The difficulty of reading you own code, which is HARD even for professional programmers, is that you usually see what you MEANT to write, not what you actually wrote. That's why it's so important to get other people to read your code, and to read other people's code, like pair programming and code reviews, or even explaining it to a duck.

https://en.wikipedia.org/wiki/Rubber_duck_debugging

It takes a lot more effort and concentration to slow down and look at what's actually there, instead of what you want to be there. (That's true for many aspects of life...)

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#246
post #58

Hey looks good :)! Just a few things you might not know about: Google String.prototype.padEnd. It’s built in to JS and should be able to replace your zero fill function. And instead of mutating the ruleSet array you should be able to use Array.prototype.map like this: const ruleSet = zeroFill(8, rules.toString(2)) .split('') .reverse() .map(item => parseInt(item, 10)); And you can clone arrays like this, instead of u…

Good tips if you want your code to fail randomly on older devices.

Not even businesses doesn't (and shouldn't) care for older devices when it doesn't make economic sense -- even less so a fun learning project.

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#247
post #110

I guess I'm the only one here doubting that the combination of both this project and the language used in the comment replies is coming from a 12 year old. Take it as a compliment if it's legitimate.

That makes two of us. My first thought was "Why would someone lie about being 12?", but then i remembered this is the internet. I would ask for proof, but it looks like everyone has already made up their mind or doesn't care.

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#249

Excuse my portion of scepticism, it's not the fact that a "12 year old" built this, sure anyone can learn how to code and we can't know how much help this person got along the way. But it's rather the person's language when writing plain text, here in the comments for example. It doesn't feel adolescent but more grownup to me ... But maybe it's just me realising my own shortcommings as a 12 year old, as I only was pl…

[deleted]

Re: Show HN: I'm 12, learning JS, and wrote Wolfram's cellular automaton in Node

#250
post #167

Liam, this is incredible ! I thought it might be useful for you to incrementally see how someone in industry would review or change this code, so here's a little code review via video: https://youtu.be/UkVOrcS--04 We very incrementally build up to the final code, which can be found here: https://gist.github.com/stevekrenzel/b490564bf1c7f98e232a6c8... Hope you find it helpful!

If you would start a channel where you review code of random public repos I would definitely subscribe. Well done.

Just registered to +1 this
Post reply on HN