Skip to content

Instantly share code, notes, and snippets.

View honzabrecka's full-sized avatar
👋

Honza Břečka honzabrecka

👋
View GitHub Profile
export function useCallbackInNextRender() {
const [value, set] = useState(null);
useSafeEffect(
(safetyGuard) => {
if (!value) return;
const [callback, args] = value;
callback(safetyGuard, ...args);
},
[value]
const when = (pred, f) => (data) => pred(data) ? f(data) : data
// example
pipe(
when(isOdd, increment),
)
import { useEffect } from 'react'
export default function useReference(lazyRefCreator, deps) {
const ref = useRef()
useEffect(() => {
ref.current = lazyRefCreator()
}, deps)
return ref.current
// require without cache
const __r = (m) => { delete require.cache[require.resolve(m)]; return require(m) }
const reload = () = {
Object.keys(require.cache).forEach((module) => {
delete require.cache[module]
require(module)
})
}
const withLogger = (reducer) => (state, action) => {
const boldStyle = 'font-weight:bold';
console.groupCollapsed(`%caction %c${action.type}`, 'color:#CCC', boldStyle);
console.log('%cprev state', `${boldStyle};color:#9E9E9E`, state);
console.log('%caction ', `${boldStyle};color:#03A9F4`, action);
const after = reducer(state, action);
console.log('%cnext state', `${boldStyle};color:#4CAF50`, after);
console.groupEnd();
return after;
};
// @flow
import * as React from 'react';
import classNames from 'classnames';
// import type { ClassNames } from 'classnames';
type ClassNames = any;
export type ClassNamesOrFunction = ClassNames | ((props: any) => ClassNames);
beforeEach(() => {
cy.window().then((win) => {
cy.spy(win.console, "error");
cy.spy(win.console, "warn");
});
});
afterEach( () => {
cy.window().then((win) => {
expect(win.console.error).to.have.callCount(0);
import hashlib
import os
from os.path import exists, isfile, join, basename
import shutil
def md5(file):
hash_md5 = hashlib.md5()
with open(file, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
@honzabrecka
honzabrecka / AoC2018-day-01.clj
Last active December 2, 2018 14:43 — forked from kepi74/AoC2018-day-01.clj
Advent of Code 2018 day 01
(defn get-final-freq
"Day one: calculate resulting frequency"
[freq_list]
; 1. clojurist prefer to use `-` instead of `_` in names
; 2. I'm not sure how you got and what's inside `freq_list` (I suppose integers, then it's fine)
(reduce + freq_list))
(defn repeated-freq
"Day one: first occur of repeated frequency"
[freq_list]