| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"> | |
| <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css"> | |
| </head> | |
| <body> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js" type="text/javascript"></script> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/react/0.11.0/react.js"></script> |
While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.
JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it much simpler to think about both the old list and the new one, what they contain, and
| 'use strict'; | |
| const authentication = require('feathers-authentication'); | |
| const jwt = require('feathers-authentication-jwt'); | |
| const local = require('feathers-authentication-local'); | |
| const oauth2 = require('feathers-authentication-oauth2'); | |
| const GithubStrategy = require('passport-github'); | |
| // Bring in the oauth-handler | |
| const makeHandler = require('./oauth-handler'); |
| // [B](f: (A) ⇒ [B]): [B] ; Although the types in the arrays aren't strict (: | |
| Array.prototype.flatMap = function(lambda) { | |
| return Array.prototype.concat.apply([], this.map(lambda)); | |
| }; |
| // Some common IE shims... indexOf, startsWith, trim | |
| /* | |
| Really? IE8 Doesn't have .indexOf | |
| */ | |
| if (!Array.prototype.indexOf) { | |
| Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) { | |
| "use strict"; | |
| if (this === null) { | |
| throw new TypeError(); |
| /** | |
| * we are going to upload file to s3 via node js by using | |
| * aws-sdk - required | |
| * busboy - required | |
| * uuid - optional - for image renaming purpose | |
| * with this library you can upload any kind of file to s3 via node js. | |
| */ | |
| const AWS = require('aws-sdk'); | |
| const UUID = require('uuid/v4'); |
| function numberToOrdinal(i) { | |
| var j = i % 10, | |
| k = i % 100; | |
| if (j == 1 && k != 11) { | |
| return i + "st"; | |
| } | |
| if (j == 2 && k != 12) { | |
| return i + "nd"; | |
| } | |
| if (j == 3 && k != 13) { |
| function countChange(money, coins) { | |
| if (money == 0) return 1; | |
| if (money < 0 || coins.length == 0) return 0; | |
| return countChange(money - coins[0], coins) + countChange(money, coins.slice(1)); | |
| } |
Note: This document was written for someone who already knows React fairly well, because it was initially a list of suggested talking points for a presentation they were planning. Ideally this document should be expanded to include clear examples for the final target audience, which is React beginners.
// Constructor
this.state = { foo: 0 };
