Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Future is Old - Functional Programming in J...

The Future is Old - Functional Programming in JavaScript

Jamison Dance

October 01, 2015
Tweet

More Decks by Jamison Dance

Other Decks in Programming

Transcript

  1. .global main .text main: mov eax, 55 ; load two

    numbers into registers mov ebx, 45 add eax, ebx ; add them (result stored in eax) push eax push ebx ret
  2. function add(a, b) { fireZeMissiles() return a + b }

    var deathToll = 0 function fireZeMissiles() { if (deathToll > 7000000000) { console.log('the world has ended. everyone is dead') return } deathToll += Math.round(Math.random() * 100000000) console.log('Oh no, ', deathToll, ' dead') }
  3. function augment(obj) { obj.myVal = 'foo' return obj } var

    obj = {} augment(obj) console.log(obj) // {myVal: 'foo'}
  4. function augment(obj) { return _.assign({}, obj, {myVal: 'foo'}) } var

    obj = {} var newObj = augment(obj) console.log(obj) // {} console.log(newObj) // {myVal: 'foo'}
  5. var output = [] for (let i = 0; i

    < array.length; i++) { let changedData = changeData(array[i]) output[i] = changedData }
  6. var output = [] for (let i = 0; i

    < array.length; i++) { let changedData = changeData(array[i]) output[i] = changedData } UGH NO WHY GAH
  7. function map(f, arr) { var a = [] for (var

    i = 0; i < arr.length; i++) { a.push(f(arr[i])) } return a }
  8. map - transform an array filter - remove items from

    array reduce - turn array to one value each - do something with each value
  9. function split(delimiter, string) { return string.split(delimiter) } function splitNewlines(string) {

    return split('\n', string) } function splitShrug(string) { return split(`¯\_(ツ)_/¯`, string) }
  10. function split(delimiter) { return function(string) { string.split(delimiter) } } let

    splitNewlines = split('\n') let splitShrug = split(`¯\_(ツ)_/¯`)
  11. let split = curry(function(delimiter, string) { return string.split(delimiter) }) let

    splitNewlines = split('\n') let splitShrug = split(`¯\_(ツ)_/¯`)
  12. function createUser(username) { return { username, password: 'beans' } }

    function hashUserPassword(user) { return { username: user.username, hash: user.password.toUpperCase() } } function createHashedUser(username) { return hashUserPassword(createUser(username)) }
  13. function createUser(username) { return { username, password: 'beans' } }

    function hashUserPassword(user) { return { username: user.username, hash: user.password.toUpperCase() } } let createHashedUser = compose(hashUserPassword, createUser)
  14. elm

  15. Sources • http://www.infoq.com/presentations/Simple-Made-Easy-QCon-London-2012 • https://en.wikipedia.org/wiki/Functional_programming • https://en.wikipedia.org/wiki/Imperative_programming • https://www.youtube.com/watch?v=oYk8CKH7OhE •

    http://haskell.cs.yale.edu/wp-content/uploads/2011/01/cs.pdf • https://www.cs.kent.ac.uk/people/staff/dat/tfp12/tfp12.pdf • Coders at Work • https://en.wikipedia.org/wiki/Lambda_calculus
  16. Images • 2001 vector art - couldn't find original source,

    see http://wallbasehq.com/images/big/ 2001_a_space_odyssey_space_suits_vector_art_wallpaper-3587.jpg • http://vignette3.wikia.nocookie.net/2001/images/4/41/A_itmes.jpg/revision/latest? cb=20111116211153 • https://sjmj91.files.wordpress.com/2013/02/2001space043.jpg • https://flic.kr/p/5J1zrp • http://i.imgur.com/F2rWHbb.gif • https://i.ytimg.com/vi/Tj6n2YhFuqg/mqdefault.jpg • https://www.flickr.com/photos/bohman/16062901379/in/photolist-qtqyXv-86kuSi-7Xi4jn-abhEnd- uujbAG-abhAEy-abeGDt-o6zUda-dSLBvP-94CfH9-abhvMA-bUUme1-cobi7q-8YEbSy-n9e9eN-a1HRJH- dMBF1h-5woynn-7ZBp9H-9aJJ5m-8teEAc-8eDSbB-6ZwXL1-7bXjDd-8feUCk-9yHDJx-aZbzfr-duPLHD- eWYsiy-6TTARf-74HG5i-duNd4J-cWWjFG-abeLZX-v7jH5m-v7jHJ7-uXyiKa-6oWZC3-e1UNqc-8phgVx- v7jF85-5XuumM-8ioqe2-vpfrnX-abeEt2-abhvq5-7SVK8Z-abeLSk-4hEHCs-abeGya • http://assets22.pokemon.com/assets/cms2/img/pokedex/full/001.png