π§πΎβπ»
prep
Overview description of the prep work for the sprint
π Programming language concepts
Learning Objectives
Read the learning objectives listed on this page: Bear in mind what youβre trying to achieve while reading this text. If a topic isnβt making much sense, and isnβt in the objectives, you can probably skip over it. If a topic is listed in the objectives, you should keep studying it until you are confident youβve met the objective.
Reading
Read chapter 9 of How Computers Work.
Do every exercise listed in the chapter.
You can skip the projects (though you’re welcome to try any of them if you have time!).
Check you have achieved each learning objective listed on this page. If you’re not sure about any of them, ask in Slack.
Exercise
Label each operator with its correct arity:
Drag the correct arity from ππΎ onto each operator ππ½
?
operator in JavaScript.and
in Python, &&
in JavaScript and C).not
in Python, !
in JavaScript and C).+
).or
in Python, ||
in JavaScript and C).&
).~
).π¬ Sometimes: when used in value = -1
it’s unary. But can it be used other ways?
π¬ Sometimes: when used like value = 2 - 1
it’s binary. But can it be used other ways?
π¬ No, in the languages we’re learning -
isn’t a ternary operator.
π¬ Right - sometimes it applies to one value (value = -1
) and other times it needs two (value = 2 - 1
).
In class, we will have a discussion about each of the below prompts. Think about them, and write down notes in your notebook to help you participate in the discussion.
Exercise
Exercise
Write down what happens when you call a function.
Make sure you include how the “next line of code to run” moves around, parameters/arguments, return values, and scope.
Exercise
Write down an explanation of what a class is, which could be understood by someone who’s never written any code before.
To help you think about this, consider:
- What properties may a “recipe” class have, for representing recipes you could cook? How may a person interact with a recipe?
- What properties may a “Big Mac recipe” have, for representing how to make a Big Mac?
- What is the difference between a recipe for a Big Mac, and an actual Big Mac you may buy yourself?
- What is the difference between the idea of any recipe, and an actual recipe for a Big Mac?
Include at least one real-world example of when a class is useful.
Make sure you describe the relationship between a class and an instance of a class.
Exercise
Instances of classes are often called objects.
This kind of “object” is similar to, but slightly different from what we call an “object” in JavaScript.
Write down the differences between these two meanings of the word object.
π Bitwise operators
Learning Objectives
π¬ Not quite - if a bit is set in both numbers, we don’t add them.
π¬ Right! 1001 | 1110 = 1111 - we take all of the bits that are set in either number.
π¬ Not quite - check again what | does.
π¬ Not quite - if a bit is set in both numbers, we don’t add them.
π¬ Not quite - check again what & does.
π¬ Right! 1001 & 1110 = 1000 - we take all of the bits that are set in both numbers.
π¬ Not quite - check again what ^ does.
π¬ Right! 1001 ^ 1110 = 0111 - we take all of the bits that are set in exactly one of the numbers.
π¬ Not quite - check again what ^ does.
π¬ Right! ~1001 flips every bit, which produces 0110 which is 6.
π¬ Interesting - how did you come to this answer? If you can’t explain why 6 and -10 are both valid answers, learn more about two’s complement, truncation, and sign-extension.
π¬ Not quite - check again what ~ does.
βοΈ Comparing programming languages
Learning Objectives
Variables are names for 0x41
in a memory location. One variable always points at one memory location, but depending on its type may include the data from subsequent memory locations too.
π¬ No - some languages require any new value for a variable has the same type as the old value.
π¬ JavaScript does allow this, but it isn’t the only language to allow it.
π¬ No - one of these languages doesn’t allow this.
π¬ Right - JavaScript and Python are both dynamically typed languages. C is a statically typed language.
π¬ No - one of these languages doesn’t allow this.
Exercise
Write the same function twice, once in C and once in Python. The function should take two numbers as parameters, and return the sum of those two numbers.
Write down what’s different about the two function definitions.
Some programming languages are compiled. Others are interpreted.
Exercise
Write down an explanation of what it means to be compiled or interpreted.
List all of the programming languages you know about - is each one compiled or interpreted?
What are the advantages and disadvantages of being compiled or interpreted? Write them down.
π» Shell pipelines
Learning Objectives
Read the learning objectives listed on this page: Bear in mind what youβre trying to achieve while reading this text. If a topic isnβt making much sense, and isnβt in the objectives, you can probably skip over it. If a topic is listed in the objectives, you should keep studying it until you are confident youβve met the objective.
Reading
Key take-aways:
- Most programs take some input, and produce some output.
- Instead of writing one program to do exactly what we want, we can often combine existing programs.
- Programs read from stdin, and write to stdout and stderr.
- They write their main output to stdout.
- They write error messages, progress messages, and other information that isn’t their main output to stderr.
- We can pass information between programs using a pipe:
|
- We can write the output of programs to a file using
>
, or append to a file using>>
.
Comparison with JavaScript
A shell pipeline is like a chain of function calls.
In JavaScript we joined together pipeline stages (functions) with .
s:
["Hello", "I", "am", "EXTRA", "happy"]
.filter((word) => /[A-Z]/.test(word))
.map((word) => word.toLowerCase())
.sort()
In shell we join together pipeline stages (commands) with |
s:
printf "Hello\nI\nam\nEXTRA\nhappy\n" | grep '[A-Z]' | tr '[:upper:]' '[:lower:]' | sort
π¬ Not quite - what are stdout and stderr for?
π¬ Not quite - ls only lists files that exist.
π¬ Right - ls doesn’t have any files to list as output, but does have an error to display.
π¬ Not quite - ls | sort
would output this, but there are more commands in the pipeline.
π¬ Right! We list three files, sort them, search for ones that contain an i (fish and primates), then count the number of output lines (one per file).
π¬ Not quite - ls | sort | grep i
would output this, but there’s one more command in the pipeline.
π¬ Not quite - check what the grep command in the pipeline does.
Next we will learn about some programs commonly used in pipelines.
Youtube: Brian Kernighan on pipelines
π» grep in pipelines
Learning Objectives
We’ve already used grep to search for text in files using regular expressions.
We can also pipe other commands’ output to grep
to search the output the same way.
For example, we can write:
% ls -1
report-draft
report-version-1
report-version-1.1
report-version-2
report-final
report-final-2
% ls -1 | grep -v '[0-9]'
report-draft
report-final
The original ls -1
command showed us all the files in the current directory.
By piping this to grep -v '[0-9]'
we can filter this output down to just the files whose names don’t contain numbers.
grep
operates on lines, and ls -1
outputs one file per line, so grep
tests each file one at a time.
ls
vs ls -1
In our terminal, when we run ls -1
, we get one file output per line. But if we run ls
in our terminal, we get the files on one line, separated by spaces.
We know that grep
operates on individual lines, so it may seem like ls | grep
would have a problem - ls
prints more than one file per line.
But ls
behaves specially. It detects whether it’s outputting to a terminal, or a pipe, and acts differently:
- If it’s outputting to a pipe, it outputs one file per line.
- If it’s outputting to a terminal, it tries to be useful and take up less space. But if you pass
-1
it will forcels
to output one file per line.
So we can write ls | grep -v '[0-9]'
- we don’t need to pass -1
to ls
.
It’s good to know that sometimes programs behave differently when outputting to a terminal or a pipeline.
π» sort and uniq
Learning Objectives
sort
sorts its input. uniq
deduplicates adjacent matching lines.
(Source, including text-only transcript: https://wizardzines.com/comics/sort-uniq/)
Learn about sort
and uniq
from their man pages (and the backlog exercises).
Often we pipe to sort | uniq
not just uniq
so that duplicate lines will be next to each other before they’re passed to uniq
.
For the following quizzes, consider the following input file:
% cat input.txt
pigs 10
chickens 2
pigs 10
goats 3
hamsters 300
π¬ Right - sort sorts the file.
π¬ Not quite - what does -u
do?
π¬ Not quite - what does piping to uniq do?
π¬ Not quite - check what -k1
does.
π¬ Not quite - look at the difference between alphabetical sorting and numerical sorting.
π¬ Right! We need to select the right field, sort numerically, and reverse the order to go biggest to smallest.
π¬ Close, but what order will things be sorted?
π¬ Not quite - look at the order the commands are being run in the pipeline.
π¬ Right! We take just the animal names, then sort them so that uniq will work, then ask uniq to count how many of each it saw, and then sort by how many uniq counted.
π¬ Not quite - look at the order the commands and running in the pipeline.
π» head and tail
Learning Objectives
head
outputs lines (or bytes) from the start of a file. tail
outputs lines (or bytes) from the end of a fail.
(Source, including text-only transcript: https://wizardzines.com/comics/head-tail/)
Learn about head
and tail
from their man pages (and the backlog exercises).
Imagine we have an input file which has 100 lines.
π¬ Not quite - are you confusing -n and -c?
π¬ Not quite - are you confusing head and tail?
π¬ Right! -n takes a number of lines to output, and head goes from the start of the file.
π¬ No - remember each stage in a pipeline applies to the output of the previous stage, not the original file.
π¬ Right - tail skips the first few lines, then head takes just a few from the top of that output.
π¬ Not quite - how many lines does this skip?
π» tr
Learning Objectives
tr
translates (replaces) characters.
Learn about tr
from its man page (and the backlog exercises).
π¬ Not quite - check how multiple characters in a string are interpreted.
π¬ Right! Multiple characters in the first arguments means look for any of them.
π¬ Not quite - check how multiple characters in a string are interpreted.
π¬ Right - we list all of the vowels as things to delete.
π¬ No - the no-flag form of tr doesn’t allow an empty second string.
π¬ No - this will remove all of the vowels, but also remove other characters. tr doesn’t accept regular expressions.
β jq
Learning Objectives
All of the tools we’ve seen so far operate on lines, words, or characters.
Often these are the formats we have. And indeed, often we decide to have our programs output in these formats to make the output easy to process with tools.
But there are more complex formats it can be useful to process too. You have already used JSON in the course. It is a text format that allows us to represent arrays, objects, strings, numbers, and more.
jq
is a tool for processing JSON without having to write a whole program. This can be really useful to quickly analyse some data.
Reading
Practice using jq
using the relevant backlog exercises.