swizec.com

#### Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

# 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 :)

Published on June 5th, 2017 in Livecoding, Technical

#### Continue reading about Livecoding #39: Towards an AI that writes JavaScript

Semantically similar articles hand-picked by GPT-4

### Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

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 ❤️

Created by Swizec with ❤️