Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This code is pseudo JavaScript
- function (x) {print x*x;}
- reduce(function(x,y) (return x+y), [1,2,3]) = 6
- add = function (x,y) return (x+y)
- x = [1,2,3][0]
- Implementation of reduce
- for(i=1; i < [1,2,3].length; i++){
- x = add(x,[1,2,3][i])
- }
- return x
- reduce(add,[1,2,3]) = 6
- how to use the same logic as nested for loops using map
- [[1,2],[3,4]]
- map(function (i) return map(function(ninja) j*2, i), [[1,2],[3,4]])
- less cryptic
- function double (i) {return 2*i}
- function doubleAll (i) {return map(double,i)}
- map(doubleAll,[[1,2],[3,4]])
- Here is some actual JavaScript that amigojapan wrote later to test the map and reduce functions
- <html>
- <head>
- <script type="text/javascript">
- print = function(x){alert(x);}
- sum = function(sum, current){
- return (sum+current);
- }
- function show_prompt() {
- var o="";
- var list=[10,2,3];
- list.map(print, list);
- var total =list.reduce(sum);
- o=total;
- document.getElementById('output1').innerHTML=o;
- }
- window.onload = show_prompt;
- </script>
- </head>
- <div id="output1">No output</div>
- <body></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement