Skip to content

Made assignment #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions JavaScript/8-timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

const transKey = key => key.toString() + '(' + typeof(key) + ')';
const createKey = keys => keys.map(key => transKey(key)).join('|');

const memoizeTime = (
// Memoize the results of the fucntion
fn, //Function to memoize
timeout = 0 //Time in milliseconds, if not specify time is unlimited
//Returns: memoized function with time expiration cache
) => {
const cache = new Map();
if (timeout) setTimeout(() => {
console.log('Clear cache');
cache.clear();
timeout = true;
}, timeout);
return (...args) => {
const key = createKey(args);
if (cache.has(key)) {
console.log(
`From cache:, ${fn.name}(${(args.join(', '))}) ${cache.get(key)}`
);
return cache.get(key);
}
console.log(`Calculate: ${fn.name}(${args.join(', ')})`);
const res = fn(...args);
if (timeout !== true) cache.set(key, res);
return res;
};
};

//Test
const fib = n => (n <= 2 ? 1 : fib(n - 1) + fib(n - 2));

const memoTimed = memoizeTime(fib, 500);
memoTimed(2);
memoTimed(5);
memoTimed(5);
memoTimed(6);
memoTimed(2);
memoTimed(2);
memoTimed(8);
memoTimed(9);
memoTimed(8);
memoTimed(9);
setTimeout(() => {
memoTimed(10);
memoTimed(2);
memoTimed(10);
memoTimed(3);
memoTimed(16);
memoTimed(3);
memoTimed(10);
memoTimed(3);
memoTimed(2);
memoTimed(2);
memoTimed(10);
memoTimed(10);
}, 1000);
68 changes: 68 additions & 0 deletions JavaScript/9-count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const transKey = key => key.toString() + '(' + typeof(key) + ')';
const createKey = keys => keys.map(key => transKey(key)).join('|');

const memoizeCount = (
// Memoize the results of the fucntion
fn, //Function to memoize
length = 0 //Max record in the cache, if not specify records are unlimited
//Returns: memoized function with max records count and removing least used
) => {
const cache = new Map();
return (...args) => {
const key = createKey(args);
if (cache.has(key)) {
const record = cache.get(key);
console.log(
`From cache:, ${fn.name}(${(args.join(', '))}) ${record.res}`
);
record.count += 1;
return record.res;
}
console.log(`Calculate: ${fn.name}(${args.join(', ')})`);
const res = fn(...args);
if (length && cache.size >= length) {
let minUsage = length;
let minKey;
for (const [key, value] of cache.entries()) {
if (value.count < minUsage) {
minKey = key;
minUsage = value.count;
}
}
console.log(`Delete element: ${minKey}: ${cache.get(minKey).count}`);
cache.delete(minKey);
}
cache.set(key, { res, count: 0 });
return res;
};
};

//Test

const fib = n => (n <= 2 ? 1 : fib(n - 1) + fib(n - 2));

const memoCounted = memoizeCount(fib, 7);
memoCounted(2);
memoCounted(5);
memoCounted(5);
memoCounted(6);
memoCounted(7);
memoCounted(7);
memoCounted(2);
memoCounted(9);
memoCounted(6);
memoCounted(2);
memoCounted(9);
memoCounted(2);
memoCounted(15);
memoCounted(3);
memoCounted(15);
memoCounted(3);
memoCounted(10);
memoCounted(10);
memoCounted(19);
memoCounted(18);
memoCounted(19);
memoCounted(10);
116 changes: 116 additions & 0 deletions JavaScript/a-size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict';

const transKey = key => key.toString() + '(' + typeof(key) + ')';
const createKey = keys => keys.map(key => transKey(key)).join('|');

const sizeOf = (
//Roughly calculate the size of objects
unit, //Unit of information
...args //Array of object
//Returns: number represents the size of object in the units
) => {
let bytes = 0;
const count = (args) => {
args.forEach(el => size(el));
};
function size(
//Calculate size of object
obj //Object of value or reference type
//Returns: number represents the size of object in bytes
) {
if (obj !== null && obj !== undefined) {
const type = typeof obj;
if (type === 'number') {
bytes += 8;
} else if (type === 'string') {
bytes += obj.length * 2;
} else if (type === 'boolean') {
bytes += 4;
} else if (type === 'object') {
const objClass = Object.prototype.toString.call(obj).slice(8, -1);
if (objClass === 'Object' || objClass === 'Array') {
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
size(obj[key]);
}
} else if (objClass === 'Map') {
for (const [key, value] of obj.entries()) {
size(key);
size(value);
}
} else bytes += obj.toString().length * 2;
}
}
}
const format = (
//Convert bytes in the other units
number, //number of bytes
unit //unit in which convert
//Returns: size of object in the other units
) => {
if (unit === 'bytes') return number;
if (unit === 'KiB') return (number / 1024).toFixed(5);
if (unit === 'MiB') return (number / 1048576).toFixed(5);
if (unit === 'GiB') return (number / 1073741824).toFixed(5);
throw new Error('Unknown units');
};

count(args);
return format(bytes, unit);
};

const memoizeSize = (
//Memoize the results of the fucntion
fn, //Function to memoize
maxSize = 0, //Max size of cache, if not specify size is unlimited
unit = 'bytes' //Unit of information, if not specify unit is bytes
//Returns: memoized function with max total stored data size
) => {
const cache = new Map();
return (...args) => {
const key = createKey(args);
if (cache.has(key)) {
console.log(
`From cache:, ${fn.name}(${(args.join(', '))}) ${cache.get(key)}`
);
return cache.get(key);
}
console.log(`Calculate: ${fn.name}(${args.join(', ')})`);
const res = fn(...args);
if (sizeOf(unit, cache, key, res) > maxSize && maxSize) {
const firstKey = cache.keys().next().value;
console.log(`Delete element: ${firstKey}: ${cache.get(firstKey)}`);
cache.delete(firstKey);
}
cache.set(key, res);
return res;
};
};

//Tests

const fib = n => (n <= 2 ? 1 : fib(n - 1) + fib(n - 2));

const memoSized = memoizeSize(fib, 200);
memoSized(2);
memoSized(4);
memoSized(5);
memoSized(6);
memoSized(7);
memoSized(2);
memoSized(3);
memoSized(9);
memoSized(8);
memoSized(2);
memoSized(11);
memoSized(2);
memoSized(14);
memoSized(3);
memoSized(16);
memoSized(3);
memoSized(10);
memoSized(13);
memoSized(19);
memoSized(18);
memoSized(10);
memoSized(10);
63 changes: 63 additions & 0 deletions JavaScript/b-univ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

const fs = require('fs');

const transKey = key => key.toString() + '(' + typeof(key) + ')';
const createKey = keys => keys.map(key => transKey(key)).join('|');
const generateKey = (keys, cb) => {
keys = createKey(keys);
if (cb) keys = keys + '(' + cb.length + ')';
else keys = keys + '(' + cb + ')';
return keys;
};

const memoizeUniv = (
//Memoize the result of both sync and async functions
fn, //Async or sync function to memoize
lib = null //Library of the functions
//Returns: memoized function
) => {
const cache = new Map();
const f = lib ? lib[fn] : fn;
return (...args) => {
let cb = null;
if (typeof(args[args.length - 1]) === 'function') cb = args.pop();
const key = generateKey(args, cb);
if (cache.has(key)) {
const record = cache.get(key);
if (cb) {
console.log('From cache with cb:', cb);
cb(record.err, record.data);
return;
}
console.log(`From cache:, ${f.name}(${(args.join(', '))}) ${record}`);
return record;
}
console.log(`Calculate: ${f.name}(${args.join(', ')})`);
if (cb) {
f(...args, (err, data) => {
cache.set(key, { err, data });
cb(err, data);
});
} else {
const res = f(...args);
cache.set(key, res);
return res;
}
};
};

//Test

const fib = n => (n <= 2 ? 1 : fib(n - 1) + fib(n - 2));

const memoSync = memoizeUniv(fib);
const memoAsync = memoizeUniv('readFile', fs);
console.log(memoSync(5));
console.log(memoSync(5));
memoAsync('4-async.js', 'utf8', (err, data) => {
console.log('data length:', data.length);
memoAsync('4-async.js', 'utf8', (err, data) => {
console.log('data length:', data.length);
});
});
Loading