Last active
May 8, 2021 15:40
-
-
Save craftybones/a60c2222a9100cb6af4144637f9a0c88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run on node repl | |
// In node, type in | |
// .load factorial.js | |
// editor | |
// This side will be pure lambda calculus | |
const identity = (x) => x; | |
const constantlyFirst = (x) => (y) => x; | |
const constantlySecond = constantlyFirst(identity); | |
const True = constantlyFirst; | |
const False = constantlySecond; | |
// lines will only work on the node repl | |
// or require('util') | |
// True[util.inspect.custom] = () => 'True'; | |
// False[util.inspect.custom] = () => 'False'; | |
const not = (x) => x(False)(True); | |
const and = (x) => (y) => x(y)(x); | |
const or = (x) => (y) => x(x)(y); | |
// Numbers | |
const n0 = (f) => (x) => x; | |
const n1 = (f) => (x) => f(x); | |
const n2 = (f) => (x) => f(f(x)); | |
const succ = (n) => (f) => (x) => f(n(f)(x)); | |
const inc = (x) => x + 1; | |
const toInt = (n) => n(inc)(0); | |
const add = (m) => (n) => m(succ)(n); | |
const mul = (m) => (n) => (x) => m(n(x)); | |
// Data structures :) | |
const pair = (x) => (y) => (f) => f(x)(y); | |
const first = (p) => p(constantlyFirst); | |
const second = (p) => p(constantlySecond); | |
const pairMul = (p) => pair(mul(first(p))(second(p)))(succ(second(p))); | |
const fact = (n) => first(n(pairMul)(pair(n1)(n1))); | |
const n5 = succ(add(n2)(n2)); | |
console.log("Fact 5: ", toInt(fact(n5))); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Our session will require you to read a little JS. | |
// You should know most of it, but if you haven't | |
// worked on ES6, this will help you get a gist of it | |
// Js now uses const instead of var | |
const maxValue = 100; | |
// setting maxValue = 300 will cause a compiler error. | |
// There are arrow functions | |
const double = (x) => x + x; | |
// same as | |
const double_ = function(x) { | |
return x + x; | |
}; | |
// They mostly behave like regular functions | |
// But they evaluate 'this' differently. | |
// We won't need that for now. | |
// arrow functions can also return arrow functions | |
const adder = (x) => (y) => x + y; | |
// same as | |
const adder_ = function(x) { | |
return function(y) { | |
return x + y; | |
}; | |
}; | |
const addTwo = adder(2); | |
// what will addTwo(3) be? | |
adder(3)(4); | |
// This is the level you need to be able to read code at |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment