Last active
February 5, 2017 01:26
-
-
Save mrosata/659f84a81dd584dc83b33d7a9a26e9c9 to your computer and use it in GitHub Desktop.
Explain the code around 25 mins into https://www.youtube.com/watch?v=4ECoOX5QZEg&lc=z13jjdxj3waze5aam04cdre5llyaspvi25c.1486236089300698
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
/* Explaination for the code around 25mins of | |
* https://www.youtube.com/watch?v=4ECoOX5QZEg&lc=z13jjdxj3waze5aam04cdre5llyaspvi25c.1486236089300698 | |
*/ | |
// We have 4 helper functions | |
const mult = (a, b) => a * b; | |
const sum = (a, b) => a + b; | |
// this is how the curried "apply" behaves. | |
const apply = (fn) => (...args) => fn.apply(null, args); | |
// we don't need to mess up the gist with zip. | |
const zip = `fahghetttaahhh booowwwwttt it`; | |
// We have an array with prices of items, and another with a | |
// number, how many of each item we want to purchase. | |
const prices = [ 99.99, 12.50, 2.00 ]; | |
const quantities = [ 1 , 2 , 5 ]; | |
`So we want to figure out: | |
1 item that costs 99.99 ( 1 * 99.99 === 99.99 ) | |
2 items that cost 12.50 ( 2 * 12.50 === 25.00 ) | |
5 items that cost 2.00 ( 5 * 2.00 === 10.00 ) | |
SUBTOTAL: 134.99 ` | |
" This was the result of the chained functions in video to get the subtotal" | |
let subtotals = zip( prices, quantities ).map( apply(mult) ).reduce( sum, 0 ); | |
" But... wait, what does it all mean? " | |
" .. Let's break it down shall we... " | |
" zip takes 2 arrays ( A and B ) to a single 2D-array --> [ [A[0], B[0]] , [A[1], B[1]] , [A[2], B[2] ]... ]" | |
const priceQty = zip( prices, quantities ) //--> [ [ 99.99, 1 ] , [ 12.50, 2 ] , [ 2.00, 5 ] ] | |
` Let's simplify subtotals a bit, | |
" zip(prices, quantities).map( apply(mult) ).reduce( sum )" is equal to: ` | |
subtotals = priceQty.map( mult.apply(null) ).reduce( sum, 0 ); | |
` | |
priceQty.map( mult.apply(null) ) is equal to: ` | |
[ mult.apply(null, [ 99.99, 1 ]) , mult.apply(null, [ 12.50, 2 ]) , mult.apply(null, [ 2.00, 5 ]) ].reduce( sum, 0 ); | |
` | |
This is equivelant to writing ` | |
[ mult( 99.99, 1 ) , mult( 12.50, 2 ) , mult( 2.00, 5 ) ].reduce( sum, 0 ); | |
` | |
Which in turn is equivelant to writing ` | |
[ 99.99, 25, 10 ].reduce( sum, 0 ); | |
" The above reduction calls sum 3 times... Now for the play by play: (// code down here won't run //) " | |
[ 99.99 , 25 , 10 ].reduce(sum, 0 ); // --> priceQtys.reduce((a, b) => a + b, 0) | |
[ /*****/ 25 , 10 ].reduce(sum(0, 99.99 )); // --> 0 + 99.99 === 99.99 | |
[ /**********/ 10 ].reduce(sum(99.99, 25 )); // --> 99.99 + 25 === 124.99 | |
[ /*************/ ].reduce(sum(124.99, 10 )); // --> 124.99 + 10 === 134.99 | |
` and now we have 134.99 just like we wanted ` | |
` hope this helped you a little bit ` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment