Math as a [Practical] Programming Language
Alternatively, “Practical Math for Programmers”.
All programming is math. Literally, programs are types are proofs — it’s math.
And yet, math isn’t programming. Most day-to-day programming plays with the transformation of strings, dates, and structures.
Where Math is Limited
Sequential Processes
Strings
Large, Interconnected Systems
Accessibility & Documentation
Code docs are written for other programmers.
Math docs are written for, well, nobody really.
Practical Classes
Floating-Point Errors
Error-Handling
Where Math Shines
Math hacks!
This presentation is filled with shortcuts you can make with math.
Examples of how to use math in practical programming.
Show how in all these examples, it’s better to design these systems with mathematics on-paper.
If you see , you should think of .
Express elegant representations to make life easier and avoid mistakes.
- First, find a good representation on paper! Math is useful here.
- Implement the representation in your code!
Paper & Terse Notation
### Types & Guarantees
### Rigorous Communication
<{R}->{N}>
### Identity
I personally prefer to have all of my if-statements accompanied by else-statements -- it forces me to address every possible path of the conditional.
Whenever you're selectively applying functions in cases, `identity` makes a great default case.
if a < b:
x = f( x )
elif a > b:
x = g( x )
return x
if a < b:
x = f( x )
elif a > b:
x = g( x )
else:
x = identity( x )
if a < b:
h = f
elif a > b:
h = g
else:
h = identity
return h( x )
### Transforming Numerical Ranges
rescale:(A->B)->(X->Y)
function rescale() {
return ( ( new_max - new_min )
/ ( old_max - old_min )
)
* ( v - old_max )
+ new_max;
};
function rescale() {
const new_range = new_max - new_min;
const old_range = old_max - old_min;
return ( new_range / old_range )
* ( v - old_max )
+ ( new_max )
};
### Mathematical Data Structures
#### Sets
Have you ever checked for duplicates in an array/list?
What unnecessary work! In most languages, we have primitive sets to help with these cases.
Whenever you have groups of things where duplication is undesired and order doesn't matter, consider using a set!
Here's a real-life example:
result = []
for x in xs:
if x in white_list:
result.push( x )
return result
return xs.filter( x => x in white_list )
return intersect( xs, white_list )
And here's another:
cats = cats_query(...)
mammals = mammals_query(...)
for( cat in cats ):
if cat not in mammals:
mammals.push( cat )
return mammals
return union( cats, mammals )
In short, whenever you're comparing lists for membership, that should scream "set operations"!
#### Maps
Whenever you have a list of pairs, or an object where you need duplicate keys, you should think "map"!
#### Trees
#### Categories
Large systems often have a bunch of encapsulated objects. Category theory sometimes helps translate to/from different instances of classes.
#### Rings & Modular Things
If you find yourself needing to access lists of things in strange ways, you'll often need the modulo operator!
while(true):
println( xs[ ++i % xs.length ] )
#### Queues
### Hashing
### Cryptography & Security
### Union Types, GADTs, & Symbols
Most dynamic languages don't give us union types!
x with myEnum[ sym ] >
What else are symbols good for?
### Lambdas
f(x) = x + x
f = λx.x+x
f' = λx.λy.x+y
### Composition & Combinators
Whenever you hear the word "pipeline" you should think of composition!
y = f(x)
z = g(y)
z = f(g(x))
z = compose(f,g)(x)
In JS, it's often better to make your functions curryable!
const = x => y => x + y;
### Domains, Co-Domains, & Ranges
### Animation & Parameterized Functions
### Linear Algebra
### Parallelization & GPUs
### Reversibility
### Common Types
#### Infinity
#### NaN
#### Null
### Fractals & Infinite Curves
### Reduction & Efficiency
### Dates & Time
### Conditionals
if is_something( x ):
x = 0
return 0
return is_something( x )
? x
: 0
### Un-Unrolling Loops, Accumulation & Map/Filter/Reduce
#### Reductions & Folds
### Monads & IO
### Sampling & Approximation
### Statistics
### Ands & Ors with Bitmaps
### Sequences, Series, Streams, and Infinite Lists
### Encoding/Decoding/Serialization/Mapping
### Processes (Pi Calculus)
### Code Analysis (Graph Theory x Systems)
### Graphics
### Signals & Sampling
### State Machines
(ui in particular)
### Colors
### Equivalency Proofs
### Tests, Guards, Estimations, and Error Bounds
### Magic Functions & Constants
#### sin, cos, & tan
#### π
#### e
#### ln & log
### Min & Max
if x < 0:
return 0
else:
return x
return min( 0, x )
return max( min( 0, x ), 100 )
if x < y:
return x
else:
return y
return min( x, y )
max_of_xs = 0
for xs:
if x > max_of_xs:
max_of_xs = x
return max_of_xs
return max.apply( xs )
### Testing
#### Ranges
#### Distributions
### Negativity
<-1^n>
### Floors & Ceilings
### Seeds & Randomness
### Optimization Problems
### Simulations
### Currency, Tax, & Interest
### Complex Numbers
### Geolocation & Trig
### Rationals
### Rederivation
### Graph Theory & Associative Arrays
### Logic & DeMorgan's Law