CS404, week 4

CS404 – Session 4
9/30/2010

JavaScript data types

Boolean

  • true
  • var foo = true;
  • (true is a ‘literal’)

Numbers

  • 3.141
  • var bar = 3.141;
  • (there can be number ‘literals’ too, like pi)

Strings

  • "Howdy!"
  • var baz = "Howdy!"
  • can use double or single quotes, just be consistent
  • quotes tell the interpreter that the contents are a literal

Arrays

  • [true, "dat", 42]
  • [0, 1, 2];]
  • var bizzle: [true, "dat", 42];
  • square brackets filled with a list of elements
  • 0-based indexing; first element in the array is referred to by 0

Objects

  • { slot :- 3.14 }
  • a variable like any other
  • like a "dictionary" or "map" or "hash" or "hash(table)"
  • a mapping between a key like "a" and a value like 3.141
  • the object *is* the mapping from key to value
  • value can be any JS data type
  • an array is a special case of object

var foo = 101;
var obj = {
    "A" : 3.141,
    "b" : function() { },
    foo: {},
    3: [1, 2, 3, 4, 5]
    };

If you want to have keys that are numbers, you must use the ["32"] syntax to call it
window is a defined object with a bazillion properties, for example, onLoad. the value of onLoad is a function.

Functions

function (argument) {
};
var variableName = function () {
};
var panda = function () {
    alert ("Let me out!")
};

- Simplest function has no arguments

// what’s an argument? = characteristics. E.g. to draw a flower, you would pass # of petals and petal colour as arguments

  • Statements are just a bunch of calls or arguments. To find the end of a statement, look for the semi-colons.
  • When you use a variable name in the REPL instead of a literal, it will run whatever the var defines
  • The REPL will always print the final statement’s value.
     
  • Put a semi-colon at the end of a statement
  • Ruby, Perl, JS – loosely typed variables; strictly typed data

Factorials

// argh!

5! = 5 x 4 x 3 x 2 x 1 = 120

var

-- is the same thing as a = a-1

Calling a function suggests taking the code that’s a valuable in a variable and running it to get a result. If we take the "value of" something, we get a bunch of code. (You tell JS to run a function by using ().)

// Struggling to follow when Ben "chugs ahead"

Even though there are tons of shortcuts to getting code to do things (like — / a = a-1), it’s useful, perhaps, to write things lucidly first then optimize later. (Knuth etc)

// try to find a list of these things

Arguments to a function are not strictly variables.

Variable Scope // line 84

If Statements

= set a variable
== "check it. is it equal?

Events

- set up events that call functions when you need them. e.g. onClick

// Then, the class decided to try and construct a flower using the Raphael JavaScript library. I foolishly thought I was ready to improvise. I was wrong. Had no idea really where to start to tell the program what I wanted.

How I Would Write a Flower

  1. Set up canvas
  2. Draw first petal: Pick size; Pick color(s)
  3. Rotate
  4. Draw another petal
    repeat
  5. Stop when I get to first petal

// It was really fun to watch everyone’s different approaches to solving this problem. I think that’s been one of the biggest take-aways of this course for me: that there are lots of different ways to make something happen. Ben eventually got a flower on screen. He drew 4 petals in a cross, then rotated them. Purty!

CS404, week3.js

Some light reading…

// probably contains errors

// trying to write a program that greets class members in various languages randomly

var multiplyString = function(name, greeting, num) {
	var str = "";
	var i = 1;
	while (i <= num) {
		str = str + greeting + ', ' + name + "!!!\n";
		i = i + 1;
	}
	return str;
};

var cs404Victims = ["George", "Arena", "Dinah", "Mike", "Ben"];
var greetings = ["Hola", "Guten tag", "Holla", "Bonjour", "Whassup"];
var results = "";

var j = 0;
while (j < cs404Victims.length) {
	results = results + multiplyString(cs404Victims[j], greetings[j], 1) ;
	j = j + 1;
}
results;

// NOT FINISHED - trying to modify the stuff above

// define greetings

var greetings = ["Hola", "Guten tag", "Bonjour"];

// define class members

var cs404Victims = ["George", "Dinah", "Arena", "Mike", "Ben"];

// write greetings to each person 

var multiplyString = function(greeting, name, num) {
	var str="";

	// set up a loop for writing out the greetings

	for (var i=1; i <= num; i = i + 1) {
		str = str + greeting + ', ' + name + "!!!\n";
	}
	return str;
};

// define the variable foo as an empty string

var foo = "";

// work out how to the random bits

foo += multiplyString();

// print! (the last value in the script)

foo;

CS404, week 3

CS404 – Session 3
9/23/2010

http://pastebin.com – not a CVS, but useful for sharing code snippets

Anti-aliasing (Moire happens when you have lots of anti aliasing jammed up against each other. Gets shimmery.)

Why use a Version Control System?
1. Share code
2. Give yourself a backup (resilience/redundancy)
3. Diff log (see change history)

CVS – Concurrent Versioning System
RVS > CVS > Subversion > Git

Git: Distributed (offline), open source, popular
- “open source is much more secure”
- Created http://github.com/george08/CS404
- Recurring theme – give yourself tools to understand your work later (comments in code, commit messages etc)
- “Fork” assumes you’ll never merge back, “Branch” assumes you will

JavaScript!

- https://developer.mozilla.org/en/JavaScript/Reference
- http://openlibrary.org/works/OL15143763W/JavaScript
- http://openlibrary.org/works/OL2040129W/Dynamic_HTML

KDE & Gnome are popular GUIs for the Linux OS. Konqueror was a browser that split off from KDE, which was in turn forked by Apple into Safari.

http://webkit.org/

JS Literals: How to describe what goes into a variable in your source code. E.g. 1200 here:

var CANVAS_WIDTH = 1200;
OR
var NAME (chunk in RAM) = Literal

foo is the label for the RAM which stores the 1200 across (let’s say), 2 bytes, or 16 bits. RAM usage doesn’t have to be contiguous. (eg Haskell)

// Variables help you shove a literal into RAM so you can use it later.

8 bits = 1 byte

Data Types / Literals
1. Booleans – true/false

2. Number – 1200; a decimal literal in that case. -43 / 157.57 / scientific notation

3. Strings – can be huge / binary data / just a string of characters
// woah! playing in the REPL. defined the foo var, then called its literal. woo!

4. Arrays – Always a list, from 0 to whatever; can have an empty array // don’t really understand why that’s useful
Variables define the things in each of the parts of the index. Look for ["square brackets", "and", "comma-separated lists"];

6. Functions: A new feature for the language. Take code that’s being written repeatedly, and bundle it up into a “function” that follows a contract. Writing functions == writing an API.

// FUCK.Cleared console log by mistake. Really wanted to save that :(

How to write a Function Literal:

var multiplyString = function () {
};

multiplyString();

Everything’s built on functions in JS. “We’ll go slow.”
// we’re going a bit fast. I’m struggling with a lack of context in the code samples we’re writing.

Functions have arguments that change their variables each time the function’s run.
“Magic string literals” are useful bits that are hard to type on a keyboard, like a line break: “\n” (see JS reference)

5. Objects (wait until next week)

Your JS file is effectively a long string. In that string, we have layout and code, sub-strings.

2 types of Loops

1. For loops – (remember Week 2)

for ( initial ; condition ; update ) {

}

2. While loops – while(condition){loop body} – you can also simplify a for loop into broken down steps in a while
Foreach

// wondering if the examples aren’t useful because they’re nonsense?
// today was above my head

“Smell” = repetitive code

http://malevole.com – choose between programmer or serial killer

CS404, week 2

CS404 – Session 2
9/16/2010

// begin with recap of how to choose a programming language – spectrum from last week
Speed isn’t the same as Scalability
Speed is closer to the hardware. As close to the hardware as possible.
Scalability is, given that you’ve built a project in a platform, how easy is it to throw hardware at this?

Each of the languages in the spectrum are “Turing complete” so can emulate any of the others. The fact that they’re all common means that they all fit pretty well into the spectrum for choice.

Universal Turing Machine can pretend to be another machine. When you have a general purpose computer, being able to run any software on it, improvements to the hardware itself means that all the software is benefited. Division of labor.

// imagining programmable soldiers

Limitations of Computability
The Halting Problem: If you write a program that takes another program as input. The first program can’t determine when the input will ever finish. (Maybe user hitting CTRL Q will help.)

Moore’s Law – computing power is exponential.

// maybe CPU isn’t even the right unit anymore. In the cloud, you’re not restricted to 1 CPU… One of the catches though, is that it makes the software more complex, with networking management etc

Kurzweil’s Singularity – we’ll get to a point when humanity changes inevitably, for the better. As a concept, it’s idealistic.

The software line over time isn’t exponential. Speed/style/productivity hasn’t really changed in 50 years.

“What’s the difference between a nerd and a geek?”
“I don’t know.”
“You don’t know??”

Protagonists
(All socially awkward)
Hacker: take your VCR apart, and put it back together with a toaster attached (70s) -> people who broke into computers
Cracker: light side of the force; white hat
Geek: Circus freak; obsessive
Guru: grey haired hacker
Phreak: security focussed hacker that breaks the phone system; 2600 group; old skool == MIT
Hacking v.: writing software, bashing out something that works, not often in the context of a large organisation. “It dices, it slices”

// getting close to writing software!

More glossary

  • To compile: Move from one language level to another, like going from a language like C++ to Machine Language. Assembly is an inscrutable language. A few steps above a Turing Machine. It can be compiled into Machine language. Reason to suffer through Assembly? It’s super fast.
    // looking for analogies – surgery vs amputation?
  • To interpret / To evaluate: Emulates a machine (?). If a browser runs through some JS and doesn’t spit out an executable, it’s interpeting it. Not compiling, just running it. Server side/Client side? The question is, where is the interpreter?
  • To translate (Gimpertesque): Writing software that converts to something else. A lot of software is converted to C first (the workhorse). We have powerful C compilers on most platforms. Take a higher level language and translate it to C. (Not compilation since you can’t run it.) This is porting. Often a language is ported to something else to make it easier to run on another platform. Like, Processing is effectively translated into Java which then gets interpreted by a Virtual Machine that gets compiled into Machine Language.

Commenting your code is for you, not others. Unless you’re a savant, of course.

—-
Machine language is specific (tightly coupled) to a particular chip, a CPU. Pentium whatever blah blah or 8 bit Nintendo. Documented by the manufacturer.
An .exe won’t run on a Mac. You need a different version.

Especially back in the 8bit Nintendo era, a cartridge is basically a store for machine language. Stick the cartridge in the slot, and the code runs straight on the CPU.

Let’s say there was Mario Brothers source code sitting on some hard drive in Nintendo. Converted from C, runs on the 8bit. 20 years later, we all have the binaries (machine code) in a file. So, you need an interpreter that pretends to be an 8bit Nintendo machine. Does this on the fly, usually.

Class browsers: 4 use Firefox, 3 use Chrome

API (remote or local) vs
Library (local) vs
Software as a Service (remote) vs
DLL (local)

(All mean that same thing == code you haven’t written that you can use. the distinction is where the code sits or runs.)
Calling this code happens using a protocol or agreement, made available through a contract. Wikipedia – protocol = web-based

REPL: Read-Eval(uate)-Print-Loop

Open Chrome. Developer Tools. Console. Write alert("hello world");
Woo!

Data Types
- a chunk of memory that’s usually contiguous
- memory is not just where you store data, it’s where your programs are too

Class begins futzing with first “blank slate file” which calls raphael.js
Download and get it rendering locally
Futz with variables

Variable == fast, temporary place to store some information
Calling a var means “set aside some memory, that I’m going to refer to later with this variable name” AND put this information into that chunk of memory

Simplest data type = true or false (Boolean)
Next = a number (float/integer)
String = a bunch of characters “in quotes”
Functions
Hashes or Objects
Arrays

// I can put Math.PI in my Chrome REPL and get a value for Pi!
Can use allcaps in vars to illustrate things that might not change, and at the top, to “set the scene”… things outside functions.

JS is case-sensitive

Control Flow Construct sets up loops
for (how many times) {run this}

for(initiation;condition;update){
var loopy bits;
var loopy bits;
}

is condition true now or false? if true, run loop again.
imagine update sits at the bottom of the script…

var t = 6;
t = t + 2;
t += 2;

functionname (defining stuff);

Homework:
- try to generate random colors in RGB (Raphael style)
- try to generate random sizes

16-bit arithmetic logic unit (ALU) built in Minecraft

This is the first part of a planned 16-bit computer that will run entirely in Minecraft. That computer will be “Hack” compatible, which is to say that it’ll run code meant for the Hack machine described in The Elements of Computer Systems (an awesome book that you should look up if you are at all interested in how computers work).

This is super fantastic. It’s a wonderful way to visualize what Ben taught the class about switches, binary, bits and bytes in Session 1. I also enjoy just listening to the way this chap describes what he’s done. (via Boing Boing)

Note Recursion, week 1

Reading George’s notes on the first CS404 session was a humbling exercise, but most of the concepts came across solidly. Here are some of my own thoughts on her notes:

The old saw that computers only do math is mostly true, especially the math that is hard to do by hand. But it is easy to come away thinking that writing software is just about doing math. Computer science and software is imperative knowledge, a description of a machine or process. While proper mathematics is declarative knowledge, a language describing what is true.

Teleological histories make me crawl, but thinking about computation as moving us up (down?) Maslow’s hierarchy of needs is helpful. As we write better software, society can address different needs through automation. Me forecasting about the coming mass automation of labor in the 20′s and health (medicine) in the 30′s is clearly so knee-jerk that it might be right.

Turing’s bombe was not yet a universal (general purpose) computing machine. It was a gadget that helped the good guys win, and rightfully made Turing a national hero. Read more on Turing’s tragic end, to understand why the idea of a homophobic programmer is absurd.

A Turing machine’s state register is more a little chunk of memory than the rules the machine follows. The rules are more like the instruction set of a modern CPU.

The economics of the software / hardware distinction got glossed over during my own undergrad, but it could not be more important. When machines are general purpose, when these universal Turing machines can simulate any other machine, we get general improvements in performance and efficiency from specific improvements in hardware. In class I referred to general purpose hardware improvements as “economies of scale,” but this is not quite right. (Like all traders and economists, my economics is rusty.) More appropriate to consider this as productivity gains that quickly spread through the economy of automated work (software).

Software is a description of a process, imperative knowledge about how to get something done. In other words, a machine. I use the words “machine” and “software” interchangeably, so making physical logic and logic physical is clearer. This also reinforces the difference between computer science and math.

I should have segued into logic gates more smoothly. Connecting zillions of logic gates is a decent way to build universal Turing machines in physical form, but probably not the only way. And most importantly, there is nothing special about using electricity to build logic gates. In the future we might use biology or optics or quantum mechanics to build universal Turing machines. Maybe in my future CS404 classes, we will build a half adder with Tinker Toys.

I remember being curious about why analog computers do not exist. Well they do exist, but analog computers cannot scale because signal noise demands increasingly-perfect sensors. Our amazing CS404 class did not seem to notice. Heisenberg would chuckle.

Running out of symbols when counting is not a good way to explain number systems to artistically minded people. I thought visualizing this would be easy, and boy was I wrong. Doing a bit more arithmetic, and thinking in terms of columns and powers of X worked much better. So the integer 312 is 3 one-hundreds, plus 1 ten, plus 2 ones. The integer 312 is 1 two-hundred-and-fifty-six, plus 1 thirty-two, plus 1 sixteen, plus 1 eight. Thanks, Dinah!

Convention and standards are a good way to start thinking about libraries, API’s and abstraction. They are the social agreements between people that a particular chunk of memory should hold an IEEE floating point; or 01100110 means “f” in ASCII; or that the CPU will do a JMP if you put such-n’-such opcode at the IP pointer; or that the Yahoo! geocoding API will lookup an address. All you need is agreement.

For more on the U.S. intelligence community’s approach to cryptography “export” and munitions, see this Wikipedia entry.

Abstraction is giving yourself the chance to ignore the details. Expose just enough of the knobs and switches of a machine to keep the big picture in the user’s head. This is a simple way to describe a big concept. Computer science is the combination of mechanics, abstraction and economics that lets you recognize the details you can ignore.

The economics of Escoffier’s kitchen brigade turned out to be a solid way to introduce delegation and abstraction. One of the student “chefs” was worried about cross-contamination. I laughed at first, but there are easy parallels with clean room reverse engineering and closed API’s. Software unit testing is a sous tasting plates as they fly over the pass, etc.

We wrapped up the first week with how to choose a programming platform or language, though the language is usually chosen for us when a software project is in-progress. Active programming languages are all good at something, so I introduced a set of “values” along with the Sapir-Whorf Hypothesis. Sapir-Whorf explains why programmers tend to defend the language de jeur so passionately. When Java is your hammer, every nail is a cup of coffee. Some of the values we talked about were scalability (Haskell), terseness (Lisp), execution speed (C), control & real-time (assembly), hire-ability (VB), API size (Java) and CYA-ness (.Net).

At least one student was surprised that the programming language I chose to use for my day job (Ruby) will be completely irrelevant in 10-20 years. Progress!

CS404, week 1

In the interests of not being paralysed by awaiting perfection, here are my notes from CS404 Session 1, held on September 9, 2010 at the Internet Archive. Not that /* italicised things */ are “thoughts in my head.”

CS404
9/9/10

Why computers? They help with maths that’s hard to do by hand.

  1. Computation 30s-40s
  2. Communication 80s-90s
  3. Art now
  4. Labor 2020s-
  5. Health 2030s-

Bombe – a machine that cracked a the Enigma code through computations.

Turing Machine – 1937 – Imagine a tape infinitely long covered in symbols. a Head reads symbol, reads rules (a “register of state”), then processes a direction for the head. Establishes what’s “computable.”

When you can through people at creating general purpose machines/hardware, improvements will affect everyone. Gives you an economy of scale to move your focus to software.

/* Having building blocks that are rudimentary allow me to extrapolate. Good! */
/* Ben’s use of the word “machine” seems interchangeable with “software” */

Chips & Circuits

Intro to Hillis; universal building blocks; mechanical basis of an OR function.

/* Read “The Pattern on the Stone” */

Chips & Circuits

“If A OR B has pressure, C will have pressure”

“OR” Truth table
A B C
on on on
on off on
off on on
off off off

“If D is pushed, E is pulled” – “If D is pulled, E is pushed” – NOT function

“NOT” Truth Table
D E
on off
off on

/* Binary means EVERYTHING is either ON or OFF */
/* Making physical logic is a big part of Comp Sci */

AND NOT OR gates can be strung together in infinite ways.

XOR – Inputs = One has to be true and not the other. If both are true, the gate is OFF.

Decimal
0 1 2 3 4 5 6 7 8 9

Octal 0
Hexidecimal 0xA
Binary – /* Stumbled on binary arithmetic */ – the important thing is, you can count with just 0s and 1s.

“Symbols are arbitrary. All you need is agreement.” – Ben

Encoding has nothing to do with binary. It’s simply a mapping between something and something else. www.asciitable.com

There’s a number for every symbol, and it’s simply a protocol. Stored in a memory of the computer.

8bit was an arbitrary initial choice
16bit
32bit
more complex numbers
1024bit encryption – it’s a WEAPON munition in the USA

—-
break
—-

Computer Science is about making a process and not a formula physical (representing it in a machine), and also about managing complexity. == abstraction. It’s also about division of labor. (Give a process to whatever’s best at doing it. Economics)

Abstraction == Intentionally ignoring the guts/details because you can’t keep it in your head. == engineering

“Computer Science” is a combination of robotics, engineering, and economics. Not a science. It’s about choosing to forget the details. To recognize the details you can ignore.

Enter the kitchen. Imagine you’re a chef trying to delegate things…
Why would we want to let go of a piece of work? How do you know when not to do something yourself?

Someone else is better, it’s a shitty job, clean/dirty work area etc.
Parallelism, Expertise, Cross-contamination, Testing

How to choose a language/platform

  1. Terseness: How much can I do with a little bit of code? How much can I get out of the API with little pieces of code?
  2. API: the library; tools out of the box (How do I run your code from mine?)
  3. Portability: how easily can I run this code on other platforms?
  4. Speed/Performance: how fast does the code run?
  5. Scaling: How easy is it to throw more hardware at this code? Do you have to change the code when you add another machine?
  6. Hiring: How easy is it to hire people who write this language?

Sapir-Whorf Hypothesis – your language might affect the way you think
If something’s “Turing Complete” why does the language matter?
Paul Graham – If you write in Java, Java’s your hammer
Read Babel 17 – inspiration for Ruby.

Languages sit on a spectrum, which gets closer to the hardware and more demanding as you move down.

Choosing Software, the spectrum
(Worth viewing this larger to see the spectrum.)

Sapir-Whorf Haskell D.S.L. (Matlab, R)
Terseness Lisp, Scheme
Expressive Ruby, Python, JavaScript, Perl
Solving New Problems Java, C#, Obj.C
Stressing Hardware C++, Pascal

Speed C, Graphics Processing
Power Assembly, VM
Squeezing most out of hardware
Real-time Machine Language, FPGA

C++ is a syntactical extension of C
Prototype might mean quick to write, slow to run.

(Please excuse jerky end.)

Winners Announced!

Well, not winners per se, unless you count attending the inaugural, prototypical CS404 class a win. We’re looking forward to next week, September 9, to hold the first class, and will definitely report back with notes on progress.

I’ve been dreaming of a few apps I’d like to try and build, and worry slightly that I’m putting the cart before the horse. Something to browse all the hikes of Mt. Tam, or perhaps something to browse two separate classification systems (one visual, one literary) for the same word or concept. We’ll see…

Gosh!

After a pretty casual tweet, and a couple of pages on the internet, we were pretty surprised that the idea of CS404 generated so much interest! Even though we assumed plenty of Bay Area web savvies would want to pick up programming skills, the response has still been a nice surprise. Now we need to settle on the details, the when & where & who.

We don’t want this to be a lecture series. The most effective way to pick up programming is to do it in a supportive environment, with other smart people who are also banging away at an editor. CS404 is an in-the-trenches, practical learning project. Everyone will be involved writing code, experimenting and solving specific problems from scratch. Though we will spend plenty of time surveying hip technologies, this project is not meant for the faint of heart. “Delegators” need not apply.

We’ve just sent a note to all the people who have registered interest via email. We’re still working through the structure and timing of CS404, and the first run-through, which we’re thinking of very much as a prototype.

There were a couple of questions that were asked more than once, so we thought it might be worth noting a quick answer here:

Will this course be available online for distance learning?
No. Programmers are grown by tinkering & hacking, not just by reading & thinking. This makes it a poor match for PowerPoint lecture videos.

Will you offer this in London or New Zealand or New York City?
This first CS404 series is a prototype. Let’s talk in a few months! (It doesn’t seem to feel right that whatever we come up with in the prototype should be designed for “franchising.” Feels like it would be much better for any interested instructor to come up with whatever s/he feels is the most suitable course material, along the guidelines we listed out last week. This, of course, is subject to change.)

CS404 Teaser

Did you fly over a university degree? Are you a self-starter who hates studying, but loves learning? Need to know more about the guts of code? Want to be able to talk the talk, but not necessarily become a programmer?

Learn to “speak code” in a series of small classes to casually introduce serious computer science and programming.

Hosted by George Oates (web designer at large, of Flickr fame and the director of Open Library) & Ben Gimpert, (your friendly, evil-genius-instructor-hacker-about-town and professional software engineer who has been writing code since the BBS era).

CS404 will be held at the Internet Archive in San Francisco’s Richmond neighborhood. We are thinking 2–3 hours per week for 8 weeks, with discussions bookending a collaborative workshop with plenty of peeking over shoulders. The only requirements are a laptop hip enough to run a modern browser, and a commitment to actively participate and ask lots of questions.

Topics to cover:

  1. Computer Science: Not really a science, and definitely not about computers
  2. The REPL: Continuous learning through continuous f*ckups
  3. Abstraction & Functions & Sharing Code: Trying to ignore the details
  4. Lists & Queues & Stacks & Trees & Maps & Tagging: Information about information
  5. Files & Interpreters & Compilers: Down the rabbit hole
  6. Languages & Protocols & APIs: What’s in fashion this week?
  7. HTTP & Web Programming: The state of faking state
  8. AI & Machine Learning: And I, for one, welcome our new robot overlords
  9. Regular Expressions & IDEs: Tools to make it all easier

Interested in joining? Please send George & Ben an email!