Skip to content

Instantly share code, notes, and snippets.

@tokdaniel
tokdaniel / id.js
Created September 5, 2019 13:46
Relatively safe ID generator, fixed length option
const ID = () => (Date.now() + Math.random().toString(36)
.substr(2, 9))
.split('')
.sort(() => (0.5 > Math.random() ? -1 : 1))
.join('');
const createFixedLengthID = (length) => ID().padStart(length, ID())
@tokdaniel
tokdaniel / hooks.ts
Last active November 19, 2019 00:02
import { useState, useEffect, useRef, EffectCallback } from 'react';
import throttle from 'lodash/throttle';
import debounce from 'lodash/debounce';
// Ties two states together, and tracks only if a specific case occur.
export const transitionTracker = <A, B>(from: A, to: B) => {
let marker = 0;
return (state1: A, state2: B) => {
if (from === state1 && to === state2) {
// These are some nice-to-have utilies which js doesn't provide by default
// creates an array of numbers as [m..n]
const range = (m: number, n: number): number[] => Array.from(Array(n - m + 1).keys()).map(n => n + m)
// combines functions from right to left (opposite of pipe)
const compose = <R>(fn1: (a: R) => R, ...fns: Array<(a: R) => R>) =>
fns.reduce((prevFn, nextFn) => value => prevFn(nextFn(value)), fn1);
// if `value` is anything other than a number this is an identity, and modulus otherwise
@tokdaniel
tokdaniel / fizzbuzz.purs
Last active July 3, 2020 15:16
FizzBuzz in Purescript
module Main where
import Prelude (bind, mod, pure, (<$>), (<<<), (==))
import Data.List (List, (..))
import Data.Either (Either(..))
isDivisibleBy :: Int -> String -> Either String Int -> Either String Int
isDivisibleBy divider replacer value = do
v <- value
if mod v divider == 0
// How many steps a Knight takes on an infinite chessboard from O(0,0) to P (x,y)
// With O(1) time complexity
const knightsMetric = (x, y) => {
let normalizedX = Math.abs(x);
let normalizedY = Math.abs(y);
// Normailze coordinates, so that 0 <= y <= x
if(normalizedX < normalizedY) {
[normalizedY, normalizedX] = [normalizedX, normalizedY]
// Simple implementation of [].reduce
Array.prototype.myReduce = function (reducer, initialValue) {
let result = initialValue
this.forEach(item => {
result = reducer(result, item)
})
return result
.hamburger {
display: flex;
flex-direction: column;
cursor: pointer;
}
.bar {
margin: 2px;
width: 24px;
height: 4px;
const range = (m: number, n: number): number[] => Array.from(Array(n - m + 1).keys()).map(n => n + m)
const isEvil = (n: number): boolean => (
n.toString(2).split('').filter((x) => x === '1').length % 2 == 0
)
range(0, 50).filter(isEvil)
// print
console.log(range(0,50).filter(isEvil).join('\n'))
from functools import reduce
def compose2(f, g):
return lambda *a, **kw: f(g(*a, **kw))
def compose(*fs):
return reduce(compose2, fs)
def safeMod(input, divider):
return input % divider if type(input) == int else input
@tokdaniel
tokdaniel / contracts...Raffle.sol
Created March 15, 2023 08:08
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "./interfaces/IMetaData.sol";