Skip to content
Swizec Teller - a geek with a hatswizec.com

Livecoding #39: Towards an AI that writes JavaScript

This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding. Usually shorter than 500 words. Often with pictures. Livecoding happens almost every Sunday at 2pm PDT on multiple channels. You should follow My Youtube channel to catch me live.

Sound on YouTube was borked yesterday. I sounded like Mr. Robot. It all worked well on LiveEdu, but they didn't save the video it looks like. ¯\_(ツ)_

Anyway, we made some progress from last time. Our evolutionary approach still gets stuck in a local maximum after just a few iterations, but it no longer gets stuck on a single solution.

So even though the fitness score is, say, 0.4545 and never changes, the result itself keeps changing. We achieved that by improving the pairing function.

Our algorithm can now choose between two different pairing functions. Either the top 50% of the population breeds pairwise, or we take the top 30 specimens and breed each specimen with all others in the top 30.

const PAIRING_STRATEGIES = {
top_half_pairs: (population) =>
_.chunk(_.take(population, population.length / 2), 2),
top_30_superset: (population) => {
const top30 = _.take(population, 30);
return _.flatten(top30.map((A) => top30.map((B) => [A, B])));
},
};

This creates a lot of thrashing at the top, and it also makes our code faster to run. Thrashing is what we wanted to achieve, but unfortunately, it didn't have the effect on fitness scores that we hoped for.

Observe:

16
{ fitness: 0.45454545454545453,
code: '*15+',
fitnessLast: 0.45454545454545453,
codeLast: '4584',
size: 200 }
17
{ fitness: 0.45454545454545453,
code: '06*0',
fitnessLast: 0.45454545454545453,
codeLast: '4584',
size: 200 }
18
{ fitness: 0.45454545454545453,
code: '686.+9',
fitnessLast: 0.45454545454545453,
codeLast: '4584',
size: 200 }
19
{ fitness: 0.45454545454545453,
code: '86.+',
fitnessLast: 0.45454545454545453,
codeLast: '4584',
size: 200 }
20
{ fitness: 0.45454545454545453,
code: '28+',
fitnessLast: 0.45454545454545453,
codeLast: '0+*-/',
size: 200 }
21
{ fitness: 0.45454545454545453,
code: '800406',
fitnessLast: 0.45454545454545453,
codeLast: '092-092-',
size: 200 }
22
{ fitness: 0.45454545454545453,
code: '00406*',
fitnessLast: 0.45454545454545453,
codeLast: '092-092-',
size: 200 }

We achieved total randomness with no convergence. This is kind of pointless, but at least the simplified character set produces more syntactically correct solutions.

A glimmer of hope

Towards the end of my livecoding session, we found a glimmer of hope. Using Prettier.

Prettier can take a JavaScript AST and convert it into plain JavaScript code. This means we can go 1-up on abstraction levels and still produce readable code in the end.

Prettier also pointed us towards Babylon, which is a JavaScript parser that takes code and turns it into an AST. This means we can take any syntactically valid code and turn it into an AST.

ASTs, by the way, are abstract syntax trees. They're how computers read code.

What's left now is to figure out:

  1. How to generate an initial population of ASTs
  2. How to breed and mutate ASTs

🤔

You can see the full current code on Github. Not too useful yet unless you're reading months from now. Github keeps links looking the same :)

Did you enjoy this article?

Published on June 5th, 2017 in Livecoding, Technical

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their JS careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️