Skip to content

Instantly share code, notes, and snippets.

View afonsomatos's full-sized avatar
🎯
Focusing

Afonso Matos afonsomatos

🎯
Focusing
  • Lisboa, Portugal
View GitHub Profile
@afonsomatos
afonsomatos / quiz.sh
Last active August 29, 2015 14:24
Quiz in bash
#!/bin/bash
# Try to guess number
# Function that generates numbers based on range
function random {
num=$(($RANDOM%$1+1))
echo $num
}
function noargs {
@afonsomatos
afonsomatos / sum-arr.js
Last active August 29, 2015 14:24
Sum of big integer strings
/*!
* Big Integer Sum Algorithm
*
*
* Result sum( '5' * 10^7, '8' * 10^7)
* real 0m2.995s
* user 0m2.928s
* sys 0m0.060s
*/
@afonsomatos
afonsomatos / CollectionsES6.js
Created June 18, 2015 12:22
New Collection data structures and methods
// <---------------> Array methods <------------------->
// Array.from(arrayLike or iterable, mapFunc?, thisArg?)
Array.from("hello", c => c.toUpperCase());
// Array.of(...items)
Array.of(...'hello');
// -- Array prototype methods
// Array.prototype.keys() => Returns iterator of keys
['a', 'b', 'c'].keys(); // 0.. 1.. 2..
// Array.prototype.values() => Returns iterator of values
['a', 'b', 'c'].values(); // 'a'.. 'b'.. 'c'..
@afonsomatos
afonsomatos / ModulesES6.js
Last active August 29, 2015 14:23
Importing and exporting modules in es6
// --- All ways to import a module (main.js)
import MyDefault from './m1';
import { square, diag } from './m1'
import { square as sq, diag as dg} from './m1'
import * as lib from './m1'
import { default as foo } from './m1'
import Animal from './m1';
import './m1' // loads the module (executes the body)
// but doesn't import anything
import MyDefault, * as lib from './m1'
@afonsomatos
afonsomatos / TemplateStringES6.js
Last active August 29, 2015 14:23
Overview of template strings and tagged templates on ES6
// Template strings
`Hello my name is ${firstName} ${lastName}`
// Mult-line
`This is a multi-line
string that appears
to be very cool perhaps
it is.`
// Tagged template
tagFunction`Hello\n ${firstName} ${lastName}`
// Implement tagFunction
@afonsomatos
afonsomatos / SymbolES6.js
Created June 17, 2015 11:52
New primitive Symbol on ES6
// Primitive value
typeof Symbol(); // 'symbol'
// Unique
Symbol() === Symbol(); // false
// Invoked without `new`
new Symbol(); // TypeError: Symbol is not a constructor
// Set description of Symbol
let sy = Symbol('personal primitive');
String(sy); // 'Symbol(personal primitive)'
// Symbols as property keys and method names
@afonsomatos
afonsomatos / StringES6.js
Created June 17, 2015 09:46
Most important String features in ES6
// If strings starts with substr
'myString'.startsWith('my', 0 /* start searching */); // true
// If string ends with substr
'myString'.endsWith('ring', 8 /* end searching */); // true
// If strings has another substr
'myString'.includes('str', 0 /* start searching */); // true
// Multiply string
'woof '.repeat(5);
// -- Template strings
// String interpolation
@afonsomatos
afonsomatos / MathES6.js
Last active August 29, 2015 14:23
Math methods and constants in ES6
// Get sign of number (-1, +1) or NaN or (-+)zero
Math.sign(-8); // -1
Math.sign(2); // 1
Math.sign(0); // 0
Math.sign(-0); // -0
Math.sign(NaN); // NaN
// Remove decimal fraction of x
Math.trunc(Math.PI); // 3
Math.trunc(-3.99999); // -3
// Cube root of x
@afonsomatos
afonsomatos / NumberES6.js
Last active August 29, 2015 14:23
Numbers' methods and new literals in es6
// Octals 0 + o + integer
0o8
// Binary 0 + b + integer
0b11
// ParseInt doesn't support binary literals
parseInt('0b11', 2); // 0
// Instead use Number()
Number('0b11'); // 3
// Or remove prefix
parseInt('11', 2); // 3
@afonsomatos
afonsomatos / colorful.html
Created June 15, 2015 17:44
Colorful tag (this is a joke)
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>
<colorful>YOU HAVE WON CLICK FOR PRIZE</colorful> <BR>