Skip to content

Instantly share code, notes, and snippets.

View ungarson's full-sized avatar
🎯
Focusing

Yo ungarson

🎯
Focusing
View GitHub Profile
@ungarson
ungarson / dijkstraShortestWay.js
Last active June 15, 2019 05:59
dijkstraShortestWay.js - there is a function which uses dijkstra algorithm to find the shortest way in graph
/**
* This function uses dijkstra algorithm to find the shortest way in graph.
* So for this function to work properly, graph must be directed and acyclic with positive weights only
*/
export default function findShortestWayIn(graph) {
const minimumCosts = {};
let queue = [{}];
const visitedPointsFromSpecificParent = {}; // to detect a cycle in a graph
return {
@ungarson
ungarson / simpleGenerator.js
Last active June 15, 2019 08:55
Simple javascript generator. It is not iterable through for of.
export default function generator() {
const valuesToReturn = generator.prototype.valuesToReturn;
return {
value: null,
done: null,
next() {
const returnThis = valuesToReturn.pop();
return {
value: returnThis,
@ungarson
ungarson / kNearestNeighbors.js
Last active June 22, 2019 06:50
k-nearest neighbors in js
function findMetrics(values) {
return Math.hypot(...values);
}
export function updateWithMetrics(usersVotes) {
if (obj(usersVotes).doesntExist()) return null;
for (let i = 0, len = usersVotes.length; i < len; i++) {
const currentUser = usersVotes[i];
@ungarson
ungarson / findAmountOfSmallestUnitLengthClosedIntervals.js
Created July 6, 2019 06:11
find amount of smallest unit length closed intervals in javascript
export default function findAmountOfSmallestUnitLengthClosedIntervals(sortedPoints) {
if(sortedPoints.length <= 1) return 1;
let amount = 0;
for (let i = 0, len = sortedPoints.length - 1; i < len;) {
let unitClosed = false;
for (let z = i + 1; z < sortedPoints.length; z++) {
if (sortedPoints[z] - sortedPoints[i] < 1) {
unitClosed = true;
if (z === sortedPoints.length - 1) {
@ungarson
ungarson / AsyncCollector.js
Created July 7, 2019 11:38
Async collector in javascript
export default class AsyncCollector {
gotData = [];
constructor(timeout, ...funcs) {
return new Promise((resolve, reject) => {
const promisesOfFuncs = funcs.map(item => item());
Promise.all(promisesOfFuncs)
.then(values => this.gotData = values)
.then(() => {
setTimeout(() => {
resolve(this.gotData);
@ungarson
ungarson / generateIDS.js
Created July 7, 2019 12:00
ID generation in javascript
export default function* generateIDS(fromID) {
while (true) {
yield fromID++;
}
}
// Example of usage
// const generator = generateIDS(0);
// const MichaelID = generator.next().value;
// const DimaID = generator.next().value;
@ungarson
ungarson / RandomNameGenerator.js
Last active July 13, 2019 08:53
Random name generator in javascript using iterators
export default class RandomNameGenerator {
constructor(amountOfNames, lengthOfNames) {
this.amountOfNames = amountOfNames;
this.lengthOfNames = lengthOfNames;
this.names = new Array();
};
makeName(stringLength) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
@ungarson
ungarson / idGeneration.js
Created July 13, 2019 08:52
Passing an argument to a generator function in example of generating IDs
export default function* idGeneration(start = 0) {
while(true) {
const newID = yield start++;
if (typeof newID !== 'undefined') {
start = newID;
yield start++;
}
}
}
@ungarson
ungarson / compose.js
Created July 20, 2019 06:09
Composition of asynchronous functions in javascript
export default function compose(...fns) {
return async x => {
let res = x;
for (const fn of fns) {
res = await fn(res);
}
return res;
}
}
@ungarson
ungarson / RangeHas.js
Last active July 21, 2019 06:39
Make range object by creating has method in the object's proxy in javascript
let range = {
start: 0,
finish: 10
}
range = new Proxy(range, {
has(target, property) {
if (property >= range.start && range.finish >= property) return true;
else return false;
}