Skip to content

Commit 661a26f

Browse files
author
Vitalii Telychko
committed
fix
1 parent f5bb4f1 commit 661a26f

File tree

8 files changed

+335
-7
lines changed

8 files changed

+335
-7
lines changed

ES6/Array_features/methods.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// 1. Array.from
2+
3+
// const spans = document.querySelectorAll('span.name');
4+
//
5+
// // map(), generically:
6+
// const names1 = Array.prototype.map.call(spans, s => s.textContent);
7+
//
8+
// // Array.from():
9+
// const names2 = Array.from(spans, s => s.textContent);
10+
11+
// function f() {
12+
// const instanceOfArray = Array.from(arguments, x => x * 3);
13+
// console.log(instanceOfArray);
14+
// }
15+
//
16+
// f(1, 2, 3);
17+
18+
// 2. Array.of
19+
20+
// const arr = Array.of(1, 'hello', false, { a: 2 });
21+
// console.log(arr instanceof Array);
22+
23+
// 3. Array.prototype.entries()
24+
// Array.from(['a', 'b'].keys()) => [ 0, 1 ]
25+
26+
// 4. Array.prototype.keys()
27+
// Array.from(['a', 'b'].values()) => [ 'a', 'b' ]
28+
29+
// 5. Array.prototype.entries()
30+
// Array.from(['a', 'b'].entries()) => [ [ 0, 'a' ], [ 1, 'b' ] ]
31+
32+
// [...['a', 'b'].keys()] => via spread operator
33+
34+
// for (const [index, element] of ['a', 'b'].entries()) {
35+
// console.log(index, element);
36+
// }
37+
38+
// 6. Array.prototype.find(predicate, thisArg?)
39+
// 7. Array.prototype.findIndex(predicate, thisArg?)
40+
41+
// [NaN].findIndex(y => Object.is(NaN, y))
42+
43+
// 8. Array.prototype.copyWithin()
44+
45+
// const arr = [0, 1, 2, 3, 4, 5];
46+
// arr.copyWithin(3, 3, 5);
47+
// console.log(arr);
48+
// 9. Array.prototype.fill()
49+
50+
// const arr = ['a', 'b', 'c'];
51+
// arr.fill(7)
52+
// [ 7, 7, 7 ]
53+
54+
// Restrict
55+
56+
// ['a', 'b', 'c'].fill(7, 1, 2); // [ 'a', 7, 'c' ]
57+
58+
// Treat holes like undefined
59+
// Array.from(['a',,'b']); // [ 'a', undefined, 'b' ]
60+
61+
/*
62+
63+
forEach(), filter(), every() and some() ignore holes.
64+
map() skips but preserves holes.
65+
join() and toString() treat holes as if they were undefined elements, but interprets both null and undefined as empty strings.
66+
67+
copyWithin() creates holes when copying holes (i.e., it deletes elements if necessary).
68+
entries(), keys(), values() treat each hole as if it was the element undefined.
69+
find() and findIndex() do the same.
70+
fill() doesn’t care whether there are elements at indices or not.
71+
72+
*/

ES6/Iterables_iterators/examples.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// function objectEntries(obj) {
2+
// let it = Reflect.ownKeys(obj)[Symbol.iterator]();
3+
// return {
4+
// [Symbol.iterator]() {
5+
// return this;
6+
// },
7+
// next() {
8+
// let { done, value: key } = it.next();
9+
// if(done) {
10+
// return { done: true };
11+
// }
12+
// return { value: obj[key] };
13+
// }
14+
// }
15+
// }
16+
//
17+
// // objectEntries({ a: 1, b: 2 });
18+
//
19+
// for(const x of objectEntries({ a: 1, b: 2 })) {
20+
// console.log(x);
21+
// }
22+
23+
24+
// Combinators are functions that combine existing iterables to create new ones.
25+
26+
// function take(n, iterable) {
27+
// const it = iterable[Symbol.iterator]();
28+
// return {
29+
// [Symbol.iterator]() {
30+
// return this;
31+
// },
32+
// next() {
33+
// if(n > 0) {
34+
// n--;
35+
// return it.next();
36+
// } else {
37+
// return { done: true }
38+
// }
39+
// }
40+
// }
41+
// }
42+
//
43+
// const arr = ['a', 'b', 'c', 'd'];
44+
// for (const x of take(2, arr)) {
45+
// console.log(x);
46+
// }
47+
48+
49+
// function zip(...iterables) {
50+
// const iterators = iterables.map(i => i[Symbol.iterator]());
51+
// let done = true;
52+
// return {
53+
// [Symbol.iterator]() {
54+
// return this;
55+
// },
56+
// next() {
57+
// if(!done) {
58+
// const items = iterators.map(i => i.next());
59+
// done = items.some(item => item.done);
60+
// if (!done) {
61+
// return { value: items.map(i => i.value) };
62+
// }
63+
// for (const iterator of iterators) {
64+
// if (typeof iterator.return === 'function') {
65+
// iterator.return();
66+
// }
67+
// }
68+
// }
69+
// return { done: true };
70+
// }
71+
// }
72+
// }
73+
//
74+
// const zipped = zip(['a', 'b', 'c'], ['d', 'e', 'f']);
75+
//
76+
// for(const x of zipped) {
77+
// console.log(x);
78+
// }
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// 1. Custom iterator
2+
3+
// const iterables = {
4+
// [Symbol.iterator]() {
5+
// let step = 0;
6+
// return {
7+
// next() {
8+
// if(step <= 2) {
9+
// step++;
10+
// }
11+
// switch (step) {
12+
// case 1:
13+
// return { value: 'hello', done: false };
14+
// case 2:
15+
// return { value: 'world', done: false };
16+
// default:
17+
// return { value: undefined, done: true };
18+
// }
19+
// }
20+
// };
21+
// }
22+
// };
23+
//
24+
// for(const x of iterables) {
25+
// console.log(x);
26+
// }
27+
28+
// 2. Iterate Object with properties
29+
// function objectEntries(obj) {
30+
// let index = 0;
31+
//
32+
// // In ES6, you can use strings or symbols as property keys,
33+
// // Reflect.ownKeys() retrieves both
34+
// const propKeys = Reflect.ownKeys(obj);
35+
//
36+
// return {
37+
// [Symbol.iterator]() {
38+
// return this;
39+
// },
40+
// next() {
41+
// if (index < propKeys.length) {
42+
// const key = propKeys[index];
43+
// index++;
44+
// return { value: [key, obj[key]] };
45+
// } else {
46+
// return { done: true };
47+
// }
48+
// }
49+
// };
50+
// }
51+
//
52+
// const obj = { first: 'Jane', last: 'Doe' };
53+
// for (const [key,value] of objectEntries(obj)) {
54+
// console.log(`${key}: ${value}`);
55+
// }
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Arrays
2+
// for (const x of ['a', 'b']) {
3+
// console.log(x);
4+
// }
5+
6+
// Strings
7+
// for (const x of 'a\uD83D\uDC0A') {
8+
// console.log(x);
9+
// }
10+
11+
/*
12+
You have just seen that primitive values can be iterable. A value doesn’t have to be an object in order to be iterable.
13+
That’s because all values are coerced to objects before the iterator method (property key Symbol.iterator) is accessed.
14+
*/
15+
16+
// Maps
17+
// const map = new Map().set('a', 1).set('b', 2);
18+
// for (const pair of map) {
19+
// console.log(pair);
20+
// }
21+
22+
// Sets
23+
// const set = new Set().add('a').add('b');
24+
// for (const x of set) {
25+
// console.log(x);
26+
// }
27+
28+
// Note that WeakMaps and WeakSets are not iterable.
29+
30+
// arguments
31+
// function printArgs() {
32+
// for (const x of arguments) {
33+
// console.log(x);
34+
// }
35+
// }
36+
37+
// DOM data structures
38+
// for (const node of document.querySelectorAll('div')) {
39+
// ···
40+
// }
41+
42+
// Plain objects are not iterable
43+
// for (const x of {}) { // TypeError
44+
// console.log(x);
45+
// }
46+
47+
// just ...
48+
// const obj = { first: 'Jane', last: 'Doe' };
49+
//
50+
// for (const [key,value] of objectEntries(obj)) {
51+
// console.log(`${key}: ${value}`);
52+
// }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
The following ES6 language constructs make use of the iteration protocol:
3+
4+
Destructuring via an Array pattern
5+
for-of loop
6+
Array.from()
7+
Spread operator (...)
8+
Constructors of Maps and Sets
9+
Promise.all(), Promise.race()
10+
yield*
11+
12+
*/
13+
14+
// 1. Via Array patterns
15+
// const set = new Set().add('a').add('b').add('c');
16+
//
17+
// const [x,y] = set;
18+
// // x='a'; y='b'
19+
//
20+
// const [first, ...rest] = set;
21+
// // first='a'; rest=['b','c'];
22+
23+
// 2. The for-of loop
24+
// for (const x of iterable) {
25+
// ···
26+
// }
27+
28+
// 3. Array.from()
29+
// > Array.from(new Map().set(false, 'no').set(true, 'yes'))
30+
// [[false,'no'], [true,'yes']]
31+
// > Array.from({ length: 2, 0: 'hello', 1: 'world' })
32+
// ['hello', 'world']
33+
34+
// 4. Spread operator
35+
// > const arr = ['b', 'c'];
36+
// > ['a', ...arr, 'd']
37+
// ['a', 'b', 'c', 'd']
38+
39+
// 5. Maps and Sets
40+
// 6. Promises - all | race
41+
// 7. yield*
42+
// function* yieldAllValuesOf(iterable) {
43+
// yield* iterable;
44+
// }

ES6/Iterables_iterators/overview.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
An iterable is a data structure that wants to make its elements accessible to the public.
3+
It does so by implementing a method whose key is Symbol.iterator.
4+
That method is a factory for iterators.
5+
An iterator is a pointer for traversing the elements of a data structure (think cursors in databases).
6+
*/
7+
8+
// Iterable values - Arrays, Strings, Maps, Sets, DOM data structures (work in progress)
9+
10+
/*
11+
Source: A value is considered iterable if it has a method whose key is the symbol Symbol.iterator
12+
that returns a so-called iterator. The iterator is an object that returns values via its method next().
13+
We say: it iterates over the items (the content) of the iterable, one per method call.
14+
*/
15+
16+
// Example
17+
const arr = ['a', 'b', 'c'];
18+
const it = arr[Symbol.iterator]();
19+
it.next();
20+
it.next();
21+
it.next();
22+
23+
/*
24+
Iterable and iterators are part of a so-called protocol (interfaces plus rules for using them) for iteration.
25+
A key characteristic of this protocol is that it is sequential: the iterator returns values one at a time.
26+
That means that if an iterable data structure is non-linear (such as a tree), iteration will linearize it.
27+
*/

ES6/Parameter_handling/tips.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
function mandatory() {
2-
throw new Error('Missing parameter');
3-
}
4-
function foo(mustBeProvided = mandatory()) {
5-
return mustBeProvided;
6-
}
1+
// function mandatory() {
2+
// throw new Error('Missing parameter');
3+
// }
4+
// function foo(mustBeProvided = mandatory()) {
5+
// return mustBeProvided;
6+
// }

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
<div id="content"></div>
1212

13-
<script src="programming_paradigms/functional_programming/reduce.js"></script>
13+
<script src="ES6/Array_features/methods.js"></script>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)