Skip to content

Instantly share code, notes, and snippets.

View nivrith's full-sized avatar
🎸
Recreational Mathemusician

Nivrith nivrith

🎸
Recreational Mathemusician
View GitHub Profile
@nivrith
nivrith / queue-5-of-7.ts
Created November 20, 2020 04:09
Make your own iterables in javascript
export class Queue<T> {
constructor(private length: number, private data: T[] = []) {}
add(record) {
const length = this.data.unshift(record);
if (length > this.length) {
this.remove();
}
}
@nivrith
nivrith / queue-6-of-7.ts
Created November 20, 2020 04:11
Make your own iterables in javascript
export class Queue<T> {
constructor(private length: number, private data: T[] = []) {}
add(record) {
const length = this.data.unshift(record);
if (length > this.length) {
this.remove();
}
}
@nivrith
nivrith / Our own iterable Range.ts
Last active November 20, 2020 06:21
Make your own iterables - Range
class Range {
constructor(start, stop, step=1) {
this.start = start;
this.stop = stop;
this.step = step;
this.cursor = start - step;
}
[Symbol.iterator]() {
let {start, stop, step, cursor};
@nivrith
nivrith / You can iterate over iterables.ts
Created November 20, 2020 05:45
Iterate over iterables
for (let number of new Range(1, 5)) {
console.log(number);
}
//1
//2
//3
//4
//5
const numbersArray = [...new Range(1, 10)];
// [1, 2, 3, 4, 5, 6, 7, 8, 9 ,10]
@nivrith
nivrith / you can destructure iterables.ts
Created November 20, 2020 05:47
Destructuring Iterables
const [first, ...rest] = new Range(1, 10);
console.log(first); // 1
console.log(rese); // [2, 3, 4, 5, 6, 7, 8, 9, 10]
@nivrith
nivrith / queue-7-of-7.ts
Created November 20, 2020 06:07
Make your own iterable
const queue = new Queue(10);
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
for(let item of queue) {
console.log(item);
@nivrith
nivrith / Iterables in JavaScript.js
Created November 20, 2020 06:25
Iterables in JavaScript
const numbers = [1, 2, 3, 4];
for (const number of numbers) {
console.log(number)
}
// 1
// 2
// 3
// 4
@nivrith
nivrith / optional chaining.js
Created November 27, 2020 09:42
Optional Chaining
let petName;
if(person && person.pet && person.pet.name) {
petName = person.pet.name;
}
//Can be condensed to 
let petName = person?.pet?.name;
@nivrith
nivrith / Default assignment using logical or.js
Created November 27, 2020 09:44
Default Assignment using Logical or
let length= 0;
let name= "";
let length = number || 25;
let name = name || "John";