function square(x) {
return x * x;
}
let x = 0;
// every ten milliseconds change x to a different random value between 0 and 99
setInterval(() => x = Math.floor(Math.random() * 100)), 10)
// assume timestamp() returns some string-formatted representation of Date.now()
function logX() {
console.log(`The value of x at time ${timestamp()} was ${x}`)
}
function square(x) {
return x * x
}
function raise(x, power) {
return Math.pow(x, power)
}
function raise(x, power) {
return Math.pow(x, power)
}
const raiseTwoByPower = raise.bind(null, 2)
// sometimes we use partial application
const sum = (a, b) => a + b
const addThree = sum.bind(null, 3) // a is now always equal to 3
// sometimes we use currying
const sum a => b => a + b
const addThree = sum(3) // again, a is now always equal to three
// sometimes we use object orientation!
class Adder {
constructor(a) {
this.a = a
}
add(x) {
return this.a + x
}
}
let addThree = new Adder(3).add // same thing!
// sometimes we use mutable global scope!
let addend = 3
const addThree = x => addend + x // again, we get the same relational structure
// (this one is less 'pure' because `a` never existed)
const sum = (a, b) => a + b
// sure this works:
sum(2, 2) // 4
// but then we get something like this:
sum(2, ' apples') // '2 apples'
public float sum(float a, float b) { return a + b }
const format(string, formatter) {
if (formatter) return formatter(string);
return value;
}
// some sample formatters
const toAllCaps = x => x.toUpperCase();
const stripSpaces = x => x.split(' ').join('')
const reverse = x => x.reverse()