Skip to content

Instantly share code, notes, and snippets.

View VinayakBagaria's full-sized avatar
💭
So much to experiment on..

Vinayak Bagaria VinayakBagaria

💭
So much to experiment on..
View GitHub Profile
@VinayakBagaria
VinayakBagaria / cache.ts
Created October 3, 2023 08:29
Eviction LRU
import LruPolicy from './lru';
import { IEvictionPolicy } from './types';
class Cache {
private _evictionPolicy: IEvictionPolicy;
constructor(evictionPolicy: IEvictionPolicy) {
this._evictionPolicy = evictionPolicy;
}
@VinayakBagaria
VinayakBagaria / machine.js
Last active December 19, 2022 07:57
Transition states
const machine = {
initial: 'cart',
states: {
cart: {
on: {
CHECKOUT: 'shipping',
},
},
shipping: {
on: {
@VinayakBagaria
VinayakBagaria / profiler.py
Created November 6, 2021 00:40
Decorator to add profiling to Python code using CProfile
import cProfile
import io
import pstats
is_profiler_enabled = False
def profile(func):
def enabled_wrapper(*args, **kwargs):
pr = cProfile.Profile()
@VinayakBagaria
VinayakBagaria / all_countries.js
Last active October 14, 2020 07:28
Countries
// So each country array has the following information:
// [
// Country name,
// iso2 code,
// International dial code,
// Order (if >1 country with same dial code),
// Area codes (if >1 country with same dial code)
// ]
const defaultCountriesData = [
['Afghanistan (‫افغانستان‬‎)', 'af', '93'],
def longestPalindrome(s):
print(s)
length = len(s)
l = [[1] * length] * length
# 'ABBAACBA'
@VinayakBagaria
VinayakBagaria / Prettier
Created May 24, 2020 18:10
General Configs
module.exports = {
/*
Print spaces b/w brackets in obj literals
true - Example: { foo: bar }.
false - Example: {foo: bar}.
*/
bracketSpacing: true,
/*
use single quotes instead of double.
*/
@VinayakBagaria
VinayakBagaria / Example.js
Created February 7, 2020 10:32
Fade-in component to animate viewing, no extra dependencies required
<FadeIn>
<Element>Element 1</Element>
<Element>Element 2</Element>
<Element>Element 3</Element>
<Element>Element 4</Element>
<Element>Element 5</Element>
<Element>Element 6</Element>
</FadeIn>
@VinayakBagaria
VinayakBagaria / generatearray.js
Created September 15, 2019 22:58
Generate an array with a range of numbers
function range(end) {
return Array.from({ length: end }, (_, index) => index);
}
range(4); // => [0, 1, 2, 3]
/*
map function that simply returns the current index.
*/
@VinayakBagaria
VinayakBagaria / fillarray.js
Created September 15, 2019 22:56
Filling an array, alternative methods
const length = 3;
const resultA = Array.from({ length }, () => ({}));
const resultB = Array(length).fill({});
resultA; // => [{}, {}, {}]
resultB; // => [{}, {}, {}]
resultA[0] === resultA[1]; // => false
resultB[0] === resultB[1]; // => true
@VinayakBagaria
VinayakBagaria / arrayfromclone.js
Created September 15, 2019 22:52
Recursive clone an array
function recursiveClone(val) {
return Array.isArray(val) ? Array.from(val, recursiveClone) : val;
}
const numbers = [[0, 1, 2], ['one', 'two', 'three']];
const numbersClone = recursiveClone(numbers);