Created
February 2, 2019 05:17
-
-
Save abiodun0/4aefb5f85cfa22d1f633a62f7ac0f27b to your computer and use it in GitHub Desktop.
fundamental abstractions, setters, getters, iterators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Abstraction | |
// Function | |
const x = (x) => x * x | |
// getter | |
const getter = () => 1 | |
// example | |
const getObject = () => ({ | |
name: 'abiodun' | |
}) | |
// setter | |
const setter = (x) => { | |
} | |
// example | |
console.log(x) | |
// getter getter | |
const getterGetter = () => (() => 1) | |
// example | |
function getterContinued() { | |
let i = 1; | |
return function() { | |
i = i+1 | |
return i | |
} | |
} | |
let confirmGetter = getterContinued() | |
console.log(confirmGetter(), 'what do we have') | |
console.log(confirmGetter(), 'what do we have') | |
console.log(confirmGetter(), 'what do we have') | |
console.log(confirmGetter(), 'what do we have') | |
// reset | |
confirmGetter = getterContinued() | |
console.log(confirmGetter(), 'what do we have') | |
console.log(confirmGetter(), 'what do we have') | |
console.log(confirmGetter(), 'what do we have') | |
console.log(confirmGetter(), 'what do we have') | |
// setter setter | |
const setterSetter = (cb) => { | |
} | |
// example callbacks | |
const exampleSetSetter = (cb) => { | |
cb('yooo'); | |
} | |
exampleSetSetter(console.log) | |
// iterables | |
const iterables = () => (() => ({value: 1, done: true})) | |
// examples | |
const exampleIterable = () => { | |
let i = 0; | |
return function() { | |
if(i< 50) { | |
i=i+1 | |
return { | |
value: i, | |
done: false | |
} | |
} | |
return { | |
value: i, | |
done: true | |
} | |
} | |
} | |
const iterable = exampleIterable() | |
// for(let result = iterable(); !result.done; result = iterable()) { | |
// console.log(result.value) | |
// } | |
// using es6 built in iterator | |
const sampleIterator = { | |
[Symbol.iterator]: () => { | |
let i = 0; | |
return { | |
next: () => { | |
if(i< 50) { | |
i=i+1 | |
return { | |
value: i, | |
done: false | |
} | |
} | |
return { | |
value: i, | |
done: true | |
} | |
} | |
} | |
} | |
} | |
const getIterator = sampleIterator[Symbol.iterator]() | |
for(let result = getIterator.next(); !result.done; result = getIterator.next()) { | |
console.log(result.value) | |
} | |
// equivalently | |
for(let x of sampleIterator) { | |
console.log(x, 'this is a let of') | |
} | |
// with generator functions syntatic suger | |
function* upTo50() { | |
let i = 0 | |
while(true) { | |
if (i < 50) { | |
yield i | |
i = i+1 | |
} | |
else { | |
return | |
} | |
} | |
} | |
const countUpto50 = upTo50() | |
for(let result = countUpto50.next(); !result.done; result = countUpto50.next()) { | |
console.log(result.value, 'with generator') | |
} | |
// then former definition of promises and observables |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment