πŸ§‘πŸΎβ€πŸ’» 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 πŸ‘‡πŸ½

The ? operator in JavaScript.
The “logical and” operator (and in Python, && in JavaScript and C).
The “logical not” operator (not in Python, ! in JavaScript and C).
The “plus” operator (+).
The “logical or” operator (or in Python, || in JavaScript and C).
The “bitwise and” operator (&).
The “bitwise not” operator (~).
πŸ€” What is the arity of the - operator?

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

Explain what a variable is and how it’s used. Make sure to use the term “memory location” in your explanation.

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:

  1. What properties may a “recipe” class have, for representing recipes you could cook? How may a person interact with a recipe?
  2. What properties may a “Big Mac recipe” have, for representing how to make a Big Mac?
  3. What is the difference between a recipe for a Big Mac, and an actual Big Mac you may buy yourself?
  4. 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

πŸ€” What does the following decimal expression evaluate to?: 9 | 14 (Hint: Convert to binary)
πŸ€” What does the following decimal expression evaluate to?: 9 & 14 (Hint: Convert to binary)
πŸ€” What does the following decimal expression evaluate to?: 9 ^ 14 (Hint: Convert to binary)
πŸ€” What does the following decimal expression evaluate to?: ~9 (Hint: Convert to binary)

βš–οΈ Comparing programming languages

Learning Objectives

Variables are names for memory locations 🧢 🧢 Memory location Memory is where we store values. You can think of memory as a long list of locations. Each location can store one byte of data. We can store the byte 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. . What’s inside the memory location can be changed.

πŸ€” Which languages allow assigning a new value with a different type to a variable.

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
flowchart LR Initial[Hello I am EXTRA happy] Initial --grep '[A-Z]'--> Filtered[Hello I EXTRA] --tr '[:upper:]' '[:lower:]'--> Mapped[hello i extra] --sort--> Sorted[hello extra i]
πŸ€” If /doesnotexist doesn’t exist, what will be output to stdout and stderr by the command ls /doesnotexist
πŸ€” If the working directory contains the files: ‘primates’, ‘fish’, and ‘monotremes’, what will ls | sort | grep i | wc -l output?

Next we will learn about some programs commonly used in 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 force ls 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.

Julia Evans’ comic about sort and uniq

(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
πŸ€” What command would output the lines of the file sorted alphabetically?
πŸ€” What command would output the lines of the file sorted by the number after the first space, starting with hamsters 300?
πŸ€” What would the command awk '{print $1}' input.txt | sort | uniq -c | sort -rn output?

πŸ’» 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.

Julia Evans’ comic about head and tail

(Source, including text-only transcript: https://wizardzines.com/comics/head-tail/)

flowchart LR Initial[1 This is the first line 2 And more 3 4 Yet more 5 It keeps going 6 This file has 9 lines 7 This is line 7 8 And they all lived 9 happily ever after] --head -n5--> Head[1 This is the first line 2 And more 3 4 Yet more 5 It keeps going] --tail -n2--> Tail[4 Yet more 5 It keeps going] style Initial text-align:left style Head text-align:left style Tail text-align:left

Learn about head and tail from their man pages (and the backlog exercises).

Imagine we have an input file which has 100 lines.

πŸ€” What would the command head -n 8 input output?
πŸ€” What command/pipeline could we write to skip the first three lines of the file, and then output the next 2 lines?

πŸ’» tr

Learning Objectives

tr translates (replaces) characters.

Learn about tr from its man page (and the backlog exercises).

πŸ€” What would the command echo 'hello' | tr 'eo' 'yz' output?
πŸ€” What command could we write to delete all of the vowels from the input?

❓ 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.