I'm so glad you asked! A function is a map between a domain and a codomain!

Not super helpful, right? Is there a better way we can reason about these super important building blocks of computation? Yes!

Crucially, the same input return the same output every time! Sometimes, this is obvious! sum(1, 3) will always return 4, right?

But is this general?

function logtime() { console.log(Date.now()) }

Is that mapping input to output?

As a matter of fact it is, but to understand why it turns out you have to completely change how you think about functions. It's worth it!

My dude I once thought as you did. But here's the trick: input isn't exactly equal to arguments, and output isn't exactly equal to return value. To grok this we must briefly transcend language!

So we have a choice. We can accept that our definition is wrong, or

Think about it this way:

Gosh what a sudden constraint!

So let's think a bit harder. What's breaking?

What we don't see is that the current value at execution time _is an input_. All inherited scope is an input to your function.

Yes, my friend, that's exactly what that means.

1,1,2

1,2,3

1,3,4

1,4,5

2,1,3

2,2,4

etc. all of these numbers exist in an arrangement which provides meaning. Can you see what this table expresses?

Can we imagine this table as infinite?

Trick question: better for what? The second doesn't run.

The next important thing to talk about is data types, and it's cool. :)

1, 1, 2

1, 2, 3

1, 3, 4

2, 1, 3

2, 2, 4

2, 3, 5

etc. So far so good. But like, there's something really convenient about this example - all of the values are integers.

1, 'hi', '1hi'

'foo', 'bar', 'foobar'

etc. Intuitively we understand that once we do this we're no longer a sum function, we're something else, right?

This is called a data type.

But what about horizontally? Can we add and remove columns? What would that mean?

I'm so glad you asked!

1,1,2,+

1,2,3,+

1,3,4,+

2,1,3,+

2,2,4,+

2,3,5,+

A column represents something that can change. If we make the + explicit, we can change it and add the following rows _to the same table_:

1,1,1,x

1,2,2,x

1,3,3,x

2,1,2,x

2,2,4,x

2,3,6,x

I'm here to tell you that abstraction is nothing more than adding a column to your mental table. And it has a dual - concretion!

2,4,+

3,5,+

1,2,x

2,4,x

3,6,x

Does this remind you of something? How do you perform concretion in JavaScript? Let's redo that same operation, but in JavaScript.

if (operation === '+') return input1 + input2

if (operation === 'x') return input1 * input2

}

const concretized = math.bind(null, 2)

It's still an input! We just don't have to think about it.

const sum = (a, b) => a+b

const add2 = sum.bind(null, 2)

OR

value = 2

const addValue = a => a+value

Do you see why these are the same operation?

Hint: tables compose!