-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aaron Nguyen
committed
Mar 3, 2022
1 parent
9d1076b
commit e237a85
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use strict"; | ||
|
||
class FunctionalSet extends Set { | ||
filter(predicate) { | ||
return new FunctionalSet([...this].filter(predicate)); | ||
} | ||
map(mapper) { | ||
return new FunctionalSet([...this].map(mapper)); | ||
} | ||
reduce(accumulator, identity) { | ||
return [...this].reduce(accumulator, identity); | ||
} | ||
} | ||
|
||
const set = new FunctionalSet(["Jack", "Jill", "Tom", "Jerry"]).add("Bill"); | ||
console.log(set); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use strict"; | ||
|
||
class BoundedSet extends Set { | ||
constructor(capacity, initialValues) { | ||
super(); | ||
this.capacity = capacity; | ||
|
||
if (initialValues.length <= capacity) { | ||
initialValues.forEach((value) => this.add(value)); | ||
} | ||
} | ||
|
||
add(value) { | ||
if (this.has(value)) return; | ||
|
||
if (this.size < this.capacity) { | ||
super.add(value); | ||
} else { | ||
throw new Error(`exceeded vapacity of ${this.capacity} elements`); | ||
} | ||
} | ||
|
||
static get [Symbol.species]() { | ||
return BoundedSet; | ||
} | ||
} | ||
|
||
const set = new BoundedSet(5, ["Apple", "Banana", "Grape", "Mangoe"]); | ||
|
||
set.add("Orange"); | ||
set.add("Apple"); | ||
|
||
try { | ||
set.add("Tangerine"); | ||
} catch (ex) { | ||
console.log(ex.message); // exceeded capacity of 5 elements | ||
} | ||
|
||
set.delete("Grape"); | ||
set.add("Peach"); | ||
console.log(set.size); // 5 | ||
|
||
const set2 = new BoundedSet(2, ["Apple", "Banana", "Grape"]); | ||
console.log(set2.size); // 0 | ||
console.log(set2); // BoundedSet {capacity: 2} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use strict"; | ||
class Base { | ||
copy() { | ||
const constructor = | ||
Reflect.getPrototypeOf(this).constructor[Symbol.species] || | ||
Reflect.getPrototypeOf(this).constructor; | ||
return new constructor(); | ||
} | ||
} | ||
class Derived1 extends Base { | ||
static get [Symbol.species]() { | ||
return Base; | ||
} | ||
} | ||
class Derived2 extends Base { | ||
static get [Symbol.species]() { | ||
return Derived2; | ||
} | ||
} | ||
const derived1 = new Derived1(); | ||
const derived2 = new Derived2(); | ||
console.log(derived1.copy()); //Base {} | ||
console.log(derived2.copy()); //Derived2 {} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { right } from "./right.mjs"; | ||
import { middle } from "./middle.mjs"; | ||
|
||
middle(); | ||
right(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { right } from "./right.mjs"; | ||
|
||
console.log("executing middle module"); | ||
|
||
export const middle = function () { | ||
console.log("middle called"); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
console.log("executing right module"); | ||
const message = "right called"; | ||
|
||
export const right = function () { | ||
console.log(message); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export const FREEZING_POINT = 0; | ||
|
||
export function f2c(fahrenheit) { | ||
return (fahrenheit - 32) / 1.8; | ||
} | ||
|
||
export const temperaturePoints = { freezing: 0, boiling: 100 }; | ||
|
||
export class Thermostat { | ||
constructor() {} | ||
} | ||
|
||
const FREEZING_POINT_IN_F = 32; | ||
|
||
function c2f(celsius) { | ||
return celsius * 1.8 + 32; | ||
} | ||
|
||
const FREEZING_POINT_IN_K = 237.15, | ||
BOILING_POINT_IN_K = 373.15; | ||
|
||
export { c2f, FREEZING_POINT_IN_K }; | ||
|
||
function c2k(celsius) { | ||
return celsius + 273.15; | ||
} | ||
|
||
export { c2k as celsiusToKelvin }; | ||
|
||
export default function unitsOfMeasures() { | ||
return ["Celsius", "Delisle scale", "Fahrenheit", "Kelvin"]; | ||
} |