Skip to content

Instantly share code, notes, and snippets.

@alexnm
Last active January 10, 2016 15:51
Show Gist options
  • Save alexnm/2d428019135f747bbf9c to your computer and use it in GitHub Desktop.
Save alexnm/2d428019135f747bbf9c to your computer and use it in GitHub Desktop.
Exemplifying ES2015 sytanx
// Basic syntax examples
var add = ( a, b ) => a + b; // function( a, b ) { return a + b; }
var increment = a => a + 1; // function( a ) { return a + 1; }
var random = ( ) => Math.random( ) // function( ) { return Math.random( ); }
// Functional programming unleashed
var array = [ 'alice', 'bob', 'charles', 'dan', 'eugene', 'felicity' ];
var longNames = array.filter( name => name.length > 3 );
var longNamesIndexes = longNames.map( ( name, index ) => index );
var sumOfIndexes = longNamesIndexes.reduce( add );
// One liner mindfuck
var sumOfIndexes = array.filter( n => n.length > 3 )
.map( ( n, i ) => i )
.reduce( ( a, b ) => a + b );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment