Skip to content

Instantly share code, notes, and snippets.

View gaganjakhotiya's full-sized avatar
🏠
Working from home

Gagan Jakhotiya gaganjakhotiya

🏠
Working from home
View GitHub Profile
@gaganjakhotiya
gaganjakhotiya / cache.py
Created August 27, 2020 03:32
In-mem cache
from collections import OrderedDict
from time import time
import enum
class CacheStrategy(enum.Enum):
TTL = "TTL"
LRU = "LRU"
LFU = "LFU"
@gaganjakhotiya
gaganjakhotiya / README.md
Created May 26, 2020 13:52
Role Based Access Control

Role Based Access Control

Assumptions

  1. Resources are programatic data constructs that can optionally have actual data associated with them
  2. Actions are operation that are performed programmatically
  3. A Role has association with zero or more resources and their corresponding actions
  4. A User is associated with zero or more roles
  5. All actions are applicable on all resources
@gaganjakhotiya
gaganjakhotiya / App.tsx
Last active March 4, 2019 20:38
State management for React using inheritance
import React from "react";
import * as Flux from "./Flux";
export default class App extends Flux.Component {
getState(data) {
return { name: data.name };
}
componentWillMount() {
setTimeout(() => Flux.Component.__update_context__({ name: "World" }), 2000); // Async response
@gaganjakhotiya
gaganjakhotiya / pipe.js
Created February 9, 2019 17:51
PIping in JS, the Elm way!
// Elm lang supports creating functions which can be used like an infix operator
// Ex: 1 + 2 |> (\n -> n == 3) -- "|>" is the infix operator and the code returns True
// If we look at the type of this operator, it would look like:
// |> : a -> (a -> b) -> b
// Now, let's try to do this in JavaScript.
// Firstly, we need a piper which would take any data and return a pipe-able data-type
// Next, every function call - when piping - should return a pipe-able data type
// A pipe-able data type is capable of executing a specific function
@gaganjakhotiya
gaganjakhotiya / main.rs
Last active September 6, 2018 17:24
Serde Custom Deserializer
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate serde;
use serde::{
de::{Error, MapAccess, Unexpected, Visitor},
Deserialize, Deserializer,
};
@gaganjakhotiya
gaganjakhotiya / Loop.elm
Created March 19, 2018 21:39
Traditional looping in Elm
module Loop exposing (forEach)
loop : List a -> List b -> (a -> Int -> b) -> List b
loop a b fn =
case a of
[] ->
b
x::xs ->
loop xs (b ++ [(fn x (List.length b))]) fn
@gaganjakhotiya
gaganjakhotiya / checkmod.md
Last active February 4, 2018 11:06
Bash CLI to watch a file and execute commands on file update

checkmod

Bash CLI to watch a file and execute commands on file update

Installation

  • Download the checkmod.sh file
  • Copy the checkmod.sh file and give permissions to execute
$ cp checkmod.sh /usr/bin/checkmod
$ chmod +x /usr/bin/checkmod
@gaganjakhotiya
gaganjakhotiya / Spinner.jsx
Last active December 20, 2017 12:42
Styled Components
import * as React from 'react'
import { StyledComponent } from './StyledComponent'
function Spinner(props) {
return (
<div className={'w--spinner ' + props.customClass}>
<div className="bounce1" />
<div className="bounce2" />
<div className="bounce3" />
</div>
@gaganjakhotiya
gaganjakhotiya / currier.js
Last active February 9, 2019 17:55
Functional programming in JavaScript
function curry(callable) {
return function(...args) {
if (callable.length <= args.length) {
return callable.apply(null, args)
} else {
return curry(callable).bind(null, ...args)
}
}
}
@gaganjakhotiya
gaganjakhotiya / thresholder.js
Created October 30, 2017 05:55
Threshold function calls for a set time-period
function thresholder(callback, holdMillis, resetOnEachCall) {
let timer = null
let args
return function() {
args = arguments
if (timer && resetOnEachCall) {
clearTimeout(timer)
timer = null