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

TODO

TODO

TODO

TODO

Accessibility & Documentation

Code docs are written for other programmers. Math docs are written for, well, nobody really.

TODO

TODO

TODO

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 this, you should think of that.

Express elegant representations to make life easier and avoid mistakes.

1. First, find a good representation on paper! Math is useful here.
2. Implement the representation in your code!

TODO

TODO

TODO

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   )
};

<!-- <todo: using pos/neg infinity as a max/min> -->

<!-- <todo: piping/composing functions into rescale> -->

Mathematical Data Structures

TODO

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”!

TODO

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 ] )

TODO

TODO

TODO

Union Types, GADTs, & Symbols

Most dynamic languages don’t give us union types!

What else are symbols good for?

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;

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

Conditionals

if is_something( x ):
x = 0
return 0

return is_something( x )
? x
: 0

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

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 )

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO

TODO