Everything is composable
- 4. “
In practice, each paradigm comes with its
own way of thinking and there are
problems for which it is the best approach.
- 13. Composability
const dotChainy = str =>
str
.toLowerCase()
.split(' ')
.map(c => c.trim())
.reverse()
.filter(x => x.length > 3)
.join('')
‘Victor and Igor’
- 14. Composability
const dotChainy = str =>
str
.toLowerCase()
.split(' ')
.map(c => c.trim())
.reverse()
.filter(x => x.length > 3)
.join('')
‘victor and igor’
- 15. Composability
const dotChainy = str =>
str
.toLowerCase()
.split(' ')
.map(c => c.trim())
.reverse()
.filter(x => x.length > 3)
.join('')
[‘victor’,‘and’,‘igor’]
- 16. Composability
const dotChainy = str =>
str
.toLowerCase()
.split(' ')
.map(c => c.trim())
.reverse()
.filter(x => x.length > 3)
.join('')
[‘victor’,‘and’,‘igor’]
- 17. Composability
const dotChainy = str =>
str
.toLowerCase()
.split(' ')
.map(c => c.trim())
.reverse()
.filter(x => x.length > 3)
.join('')
[‘igor’,‘and’,‘victor’]
- 18. Composability
const dotChainy = str =>
str
.toLowerCase()
.split(' ')
.map(c => c.trim())
.reverse()
.filter(x => x.length > 3)
.join('')
[‘igor’,‘victor’]
- 20. Composability
const doThing = str => {
const lower = str.toLowerCase()
const words = lower.split(' ')
words.reverse()
for(let i in words) {
words[i] = words[i].trim()
}
let keepers = []
for(let i in words) {
if(words[i].length > 3) {
keepers.push(words[i])
}
}
return keepers.join('')
}
- 21. Composability
const doThing = str => {
const lower = str.toLowerCase()
const words = lower.split(' ')
words.reverse()
for(let i in words) {
words[i] = words[i].trim()
}
let keepers = []
for(let i in words) {
if(words[i].length > 3) {
keepers.push(words[i])
}
}
return keepers.join('')
}
- 24. Composability
“The major contributor to this complexity
in many systems is the handling of state
and the burden that this adds when trying
to analyse and reason about the system.”
Ben Moseley & Peter Marks
Out of the Pit 2006
- 38. Programming !== Math
for(let thing in things) {
if(thing.amount > 100) keepers.push(thing.name)
}
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
while (i < 10) {
text += "The number is " + i;
i++;
}
- 43. f.g = x => f(g(x))
Programming !== Math
- 48. The paradigm shift provoked by Einstein's
theory of relativity has brought the
realization that there is no single
perspective to see the world.
- 59. World Wide Web
Category Theory
objects = webpages
Arrows = hyperlinks
Composition = Links don’t compose
Identity
- 60. World Wide Web
Category Theory
objects = nodes
Arrows = edges
Composition = Edges don’t compose
Identity
Graphs
- 61. Category Theory
objects = sets (or types)
Arrows = functions
Composition = function composition
Identity = identity function
Programming
- 66. Category Theory
“Hey, I know what can be mapped
over. An array can be mapped
over — you can map a function over an
array!”
- 69. Composability
const nextCharForNumberString = str => {
const trimmed = str.trim()
const number = parseInt(trimmed)
const nextNumber = number + 1
return String.fromCharCode(nextNumber)
}
nextCharForNumberString(' 70 ')
//'G'
- 73. const Box = x => ({
map: f => Box(f(x))
})
const nextCharForNumberString = str =>
return Box(str.trim())
.map(trimmed => new Number(trimmed))
.map(number => number + 1)
.map(n => String.fromCharCode(n))
nextCharForNumberString(' 70') //Box('G')
- 74. const Box = x => ({
map: f => Box(f(x)),
fold: f => f(x)
})
const nextCharForNumberString = str =>
return Box(str.trim())
.map(trimmed => new Number(trimmed))
.map(number => number + 1)
.fold(n => String.fromCharCode(n))
nextCharForNumberString(' 70') //'G'
- 80. Promise(5).then(five => five + 2)
//Promise(7)
Promise(5).then(five => Promise(five + 2))
//Promise(7)
Promise(5).map(five => five + 2))
//Promise(7)
- 81. const doThing = () =>
fs.readFile('file.json', 'utf-8', (err, data) => {
if(err) throw err
const newdata = data.replace(/8/g, '6')
fs.writeFile('file2.json', newdata, (err, _) => {
if(err) throw err
console.log('success!')
}
}
- 82. const readFile = futurize(fs.readFile)
const writeFile = futurize(fs.writefile)
const doThing = () =>
readFile('file.json')
.map(data => data.replace('/8/g', '6')
.chain(replaced =>
writeFile('file2.json', replaced))
- 83. const readFile = futurize(fs.readFile)
const writeFile = futurize(fs.writefile)
const doThing = () =>
readFile('file.json')
.map(data => data.replace('/8/g', '6')
.chain(replaced =>
writeFile('file2.json', replaced))
doThing().fork(e => console.log(e),
r => console.log('success'))
- 84. const lib = username =>
getTweets(username)
.map(tweets => truncateTo130(tweets))
.chain(tweets => writeFile('tweets.json', tweets))
lib('@victorvoid')
.chain(f => saveToS3(f))
.fork(e => console.error(e),
r => console.log(r))
- 88. Left('no loaded').fold(() => 'uow, error!',
s => s.toUpperCase())
//'uow, error!'
Right('loaded').fold(() => 'uow, error!',
s => s.toUpperCase())
//'LOADED'
- 92. const getConfig = () => {
try {
return fs.readFileSync('config.json')
} catch (e) {
return null
}
}
const getPort = () => {
const str = getConfig()
if(str) {
const parsed = JSON.parse(str)
return parsed.port
} else (e) {
return 3000
}
}
- 93. const getConfig = () =>
Either.try(fs.readFileSync)('config.json')
const getPort = () =>
getConfig()
.map(JSON.parse)
.fold(e => 3000, c => c.port)
- 94. const getThing = user => {
const address = user.address
if(address){
const zip = address.match(/(d{5})$/i)
if(zip){
const city = cityByZip(zip)
if(city){
return city
} else {
return 'cant find city'
}
}
}
return 'cant find city'
}
- 95. const getThing = user =>
fromNullable(user.address)
.chain(a => fromNullable(a.match(/(d{5})$/i)))
.chain(zip => fromNullable(cityByZip(zip)))
.fold(() => 'cant find city', city => city)
- 102. const Comp = g => ({
fold: g,
contramap: f =>
Comp(x => g(f(x)))
})
- 103. const Comp = g => ({
fold: g,
contramap: f =>
Comp(x => g(f))
})
const heading = str =>
<h1>You are viewing {str}</h1>
const Title = Comp(heading).contramap(s => s.pageName)
- 104. const Comp = g => ({
fold: g,
contramap: f =>
Comp(x => g(f))
})
const heading = str =>
<h1>You are viewing {str}</h1>
const Title = Comp(heading).contramap(s => s.pageName)
Title.fold({ pageName: 'Home',
currUser: {id: '123', name: 'Victor'}})
//<h1>You are viewing Home</h1>