SO. I do lots of crafty shit. I also do lots of coding shit. I knit/crochet on the TTC (where I meet ALL the cool people of Toronto), and at lunch. Invariably, one of my colleagues will look at my pattern and go "DEAR ME, THAT'S CODE"

(everyone sounds like this in my head).
And if you've done any kind of programming, it is! A knitting/crochet pattern is a list of instructions that you run through in some order to execute a thing.

An example:
So as I was talking* about a few weeks back, @Helena_LB got me into crochet and I made this splendid shawl!

*complaining
The shawl, in addition to being a thing your stylish babushka would 100% wear is also a while loop. In coding, if you have a pattern that needs to repeat until a (set of) condition(s) is met, but you don't necessarily know when that will be, you use a while loop.
For this shawl, I started off with a single pattern of stitches and continued that pattern until the shawl was the size I wanted it. I am also still not convinced crochet isn't witchcraft.
In code, it might look like this:

while shawl != the right size:
add(1 new stitch)
crochet(1 row of pattern)
add(1 new stitch)
add(tassels)

(note: != means 'does not equal')
With knitting, I make a lot of hats and mittens. These have repeating patterns over a finite, usually pre-determined number of iterations.

Do you know what that means?

Aw yiss. For loop time.
With a for loop, you're running through a (set of) pattern(s) x number of times. The x is the index, and you need to figure out what that is ahead of time.

These are called Newfoundland mittens...according to Nanny it's "because the Newfoundland women used to knit them, love"
For these mittens, we need 11 alternating rows of pattern, breaking after the 4th repetition (or iteration). We could set the index variable like this:

index = [1 2 3 4 5 6 7 8 9 10 11]

For row 4, we'll need to change the loop to put in the thumb
Our loop might then look like this:

for i =1:length(index)
if index(i) == odd
knit(pattern)
else if index(i) == 4
knit(thumb pattern)
else if index(i) == even
knit(pattern2)
Note: index(i) is calling the "ith" variable in "index". We've set i to the length of the index (11), so if i = 1, we go to the 1st element of "index", which = 1!
Also, I've snuck some if/else statements in there. Where we want the pattern to change between even and odd iterations, we've told the loop to check the number of the index, and decide which pattern to use based on that.
Important: if you put the special instructions for iteration 4 AFTER the even-number statement, we're going to have a pair of thumb-less mittens. Which is sub-optimal.
Alternate code here:

index = [1 2 THUMB 3 4 5]

for i = 1:length(index)
if index(i) == THUMB
knit(thumb pattern)
else
knit(pattern + pattern2)
end
knit(pattern)
And, because craft and code are two branches on the same nerd-tree, we combine. Like this hat!

We start off at the bottom with a for-loop of ribbing, then a for-loop of pattern stitches that continues for 3 iterations, then finish with a while loop until it's head-sized.
A common for/while loop combo is the dreaded scarf. A lot of people recommend starting with scarves. This is a terrible idea. Scarves take for fucking ever. And then you have to keep going.
Here's one I made for my mother-in-law, and gets us to debugging. There's a bug in this code (dear Liza, dear Liza), and I didn't notice until WAY TOO LATE:
The raised stitches are supposed to go over/under/over, but they don't. Here, what past-Sarah could have done was undone all of the work up to this point, then started from before the bug, fixing the iterations in question.

(Narrator: she didn't)
...and if I've said it before, I will SURELY say it again: https://twitter.com/sciencebanshee/status/1074676967562194945
Another type of knitting is colourwork. These fancy bastards are a pair of mittens I made last winter:
Colourwork, to me, is a lot like data visualization/plotting. You have data (stitches) and need to take them from one space (the pattern) and put them into another space (reality).
And this brings me to one of my favourite sources of dudgeon. Most of the needleworkers I know are ladies, and many, though far from all, of the coders I know are dudes.
And this makes me think about my grandmother, who crocheted, knitted, sewed, and embroidered, often designing and altering patterns to suit her; but who didn't consider herself particularly well-educated.

There's a fuckload of math and engineering here.
And there are people talking about this all over the internet! https://twitter.com/web_goddess/status/1144003866020605952
And here:
https://twitter.com/jmshoffstall/status/1144006212125634561
So. If you're a crafter who's curious about coding, or a coder who's curious about crafting, DO IT. It'll be a party.
Thanks for reading and I'll see you around #ScienceTwitter, #KnittingTwitter, and #ScientistsWhoKnit.

Much love.
You can follow @sciencebanshee.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.