Skip to content

Instantly share code, notes, and snippets.

View juanmaguitar's full-sized avatar

JuanMa juanmaguitar

View GitHub Profile
@juanmaguitar
juanmaguitar / generators-es2015.js
Last active September 29, 2016 19:28
Generators -ES2015
# Generators - ES2015
http://riadbenguella.com/how-es6-generators-are-changing-how-we-write-javascript/
Generators are special functions that can be run, paused and resumed at different stages of their execution, thanks to the special keyword `yield`.
```javascript
function* myGenerator() {
yield 'first';
let input = yield 'second';
@juanmaguitar
juanmaguitar / callback-promises-generators-async.js
Last active June 20, 2018 10:22
callbacks vs promises vs generators vs async
// https://medium.com/@rdsubhas/es6-from-callbacks-to-promises-to-generators-87f1c0cd8f2e#.q7boouq4o
/* Step 1: Callback hell — N levels deep */
var request = require('request');
var url1='http://httpbin.org/', url2=url1, url3=url1, url4=url1;
function foo(finalCallback) {
request.get(url1, function(err1, res1) {
if (err1) { return finalCallback(err1); }
@juanmaguitar
juanmaguitar / generators-examples-es2015.js
Last active September 29, 2016 19:25
generators examples
/* ------------------------------------------------------- */
/* GENERATORS USING PREV VALUES */
/* ------------------------------------------------------- */
function *foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
}
@juanmaguitar
juanmaguitar / fibonacci-generators-es2015.js
Created September 28, 2016 19:28
fibonacci - generators
function *fibonacci() {
let [prev, current] = [0, 1];
while(true) {
[prev, current] = [current, prev + current];
yield current;
}
}
let seq = fibonacci();
@juanmaguitar
juanmaguitar / maps-es2015.md
Last active September 28, 2016 14:40
Maps - ES2015

Maps (ES2015)

The Map object is a simple key/value map. Any value (both objects and primitive values) may be used as either a key or a value.

> var animalSounds = new Map();
> animalSounds.set("dog", "woof");
Map {"dog" => "woof"}
> animalSounds.set("cat", "meow");
@juanmaguitar
juanmaguitar / iterators-es2015.md
Last active September 28, 2016 15:37
Iterators ES-2015

Iterators (ES-2015)

Iterators

An object is an iterator when it knows how to access items from a collection one at a time, while keeping track of its current position within that sequence.

In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence. This method returns an object with two properties: done and `value.

@juanmaguitar
juanmaguitar / iterators-fibonacci.js
Last active September 28, 2016 10:21
Iterators Fibonacci (ES2015)
let fibonacci = {
[Symbol.iterator]() {
let pre = 0, cur = 1
return {
next () {
[ pre, cur ] = [ cur, pre + cur ]
return { done: false, value: cur }
}
}
},
@juanmaguitar
juanmaguitar / symbols-es2015.md
Last active September 28, 2016 10:24
Symbols (ES2015 Notes)

Symbols (ES2015 Notes)

The symbol is a new primitive type, a unique token that is guaranteed never to clash with another symbol → a kind of UUID (Universally Unique Identifier)

ES2015 symbols are values, but they’re not strings. They’re not objects. They’re something new: a seventh type of value.

The primary reason for the introduction of symbols seems to have been to facilitate adding new functionality to the language without breaking existing code (hidden and unique properties)

Con los symbol podemos añadir propiedades ocultas y unicas a los objetos que nos permiten extender objetos facilmente.

@juanmaguitar
juanmaguitar / guidMaker.js
Last active September 27, 2016 15:00
guidMaker using iterators (infinite)
function guidMaker(){
function generator() {
const S4 = () => (((1+Math.random())*0x10000)|0).toString(16).substring(1);
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
const done = false;
let generated = 0;
@juanmaguitar
juanmaguitar / gist:f596f037234efec39eb1c3a10ce41232
Created September 12, 2016 11:28
BASH:CURL post github w/ data
curl -v -u USERNAME -X POST https://api.github.com/authorizations --data "{\"scopes\":[\"gist\"], \"note\": \"SublimeText 2/3 Gist plugin\"}"