Skip to content

Instantly share code, notes, and snippets.

@iangregor
iangregor / numbers.md
Last active January 22, 2024 02:29
A little bit about numbers in programming.

A little bit about numbers in programming

Starting with what we know

Typically, as english speaking humans in the modern era, we use a number system called "decimal". In decimal, we have ten different digits: 1, 2, 3, 4, 5, 6, 7, 8, 9, and 0. When we want to express a large number, we write a sequence of these digits to represent how many tens, hundreds, thousands, etc. For example, 25, we know that if we break this down, it's 2 tens plus 5 ones; 125 is 1 hundred, 2 tens, 5 ones. We can keep adding more digits in sequence and it becomes harder to name the positions that the digits are in, but we can understand the number that the sequence of digits represents.

Modern computers use a different system—binary—which has benefits that reduce complexity and and improve efficiency [🐇].

Your pacakge name should be something that represents you and the project. If you have a domain, use that, if not, something like ericjbergan.oregontrail would be good.

package org.example;

@iangregor
iangregor / streams.md
Last active February 6, 2023 01:52
Streams

Streams

A Stream is a way of interacting with data. You can think of the data as a sort of fancy List (for most cases, until you get to advanced stream usage). The difference between a List and a Stream is how you interact with the data: With List, if you want to manipulate the items, you take the item out and then put them item back in—you mutate the list itself (or create a new list entirely). With Streams, you don't take the items out of the Stream and act on them, you provide an action to the Stream and each item in the Stream is acted upon by the action.

E.g., Perform the "action" of doubling every Integer.

Without Streams, each step is manual: get, calculate, set.

List myIntegers = List.of(1,2,3,4);
@iangregor
iangregor / compose.js
Last active October 15, 2019 05:58
A strange way to do function composition.
// The chaining relies on curried functions that it can reference, so here are some basics.
// x,y for input; xs,ys for collections; f for functions.
const fs = {
identity: x => x,
add: x => y => x+y,
times: x => y => x*y,
get: x => xs => xs[x],
map: f => xs => xs.map(f),
fold: (f, zero) => xs.reduce(f,zero),
reduce: f => xs => xs.reduce(f),
let join = (part) => (array) => array.join(part)
let isUpper = (a) => a == a.toUpperCase()
let toInt = (base) => (number) => parseInt(+number, base)
let boolToInt = toInt(10)
let map = (fn) => (array) => array.map(fn)
let add = (x) => (y) => x + y
let pipe = (f) => (g) => (x) => g(f(x))
pipe
(map