Skip to content

Instantly share code, notes, and snippets.

@xhjkl
xhjkl / actions.js
Last active December 21, 2017 07:03
Action creators acting as action types
//
// Study on how one could use action creators instead of string literals denoting action types.
//
// Usually, in most Redux workflows, one would define string literals to distinguish action types:
export const USER_DID_LOG_IN = 'USER_DID_LOG_IN'
// It hurts performance a little, because comparing strings might be slower than comparing numbers or object identities.
// It also hinders maintenance, as now the maintainer has to update twice as much code should the action name change.
// A very simple solution to both of these issues would be using a closure instead of a string:
@xhjkl
xhjkl / log.h
Created April 7, 2017 20:23
Logical logging for C11
//
// Logging facility
//
// Expecting to be included:
// <stdio.h>
// <inttypes.h>
#define _overload7(one, two, three, four, five, six, seven, x, ...) x
@xhjkl
xhjkl / Draw+LoadImage.swift
Last active April 24, 2020 03:09
OpenGL wrapper for Swift
//
// Convenience extensions to read images into textures.
//
import UIKit
import OpenGLES
extension DrawContext {
/// Make texture with an image in it.
public func makeTexture(id: TextureId? = nil, width: Int? = nil, height: Int? = nil, fromFileContents filename: String) -> TextureId {
@xhjkl
xhjkl / Promise.swift
Last active March 23, 2019 16:01
Promises for Swift
//
// Asynchronous computation control library
// that adds typing and chaining on top of Dispatch.
//
import class Dispatch.DispatchQueue
import struct Dispatch.DispatchQoS
/// Single unit of asynchronous computation.
///
public class Promise<T> {
@xhjkl
xhjkl / ulog.py
Created April 12, 2016 13:50
Logger in about twenty lines
""" Logging micro-facility
"""
import os
from time import time, strftime, gmtime
def log(value=None, *formatting, **named_formatting):
""" Record an event
"""
if value is None:
@xhjkl
xhjkl / retry.js
Created April 7, 2016 17:39
Promise again if could not fulfil for the first time
// Promise to run an operation multiple times
// until first success or until exhaustion of attempts
//
// In case of failure, the enclosing promise
// is rejected using the last error.
//
// Supplied operation could be a promise.
//
// times -- how many times to try,
// 1 means try once and do not retry,
@xhjkl
xhjkl / symbolic.rb
Created November 1, 2015 07:55
Punkish symbolic expression parser
def parse_symbolic(text)
# Return nested list of symbols as parsed from input S-expr
#
# text -- string with an S-expr
#
lexeme = /(?<quote_run>(?<quote>['"])(?<content>.*)\k<quote>(?<!\\))|(?<whole_word>\w+)|(?<bracket>[()])/
text.strip!
text.gsub!(lexeme) { |match|
first_nonempty_index = Regexp.last_match.captures.index { |x| not x.nil? }
@xhjkl
xhjkl / indent.py
Created July 19, 2015 03:46
Indentation context manager
import sys
import functools
import contextlib
@contextlib.contextmanager
def indented_output(indent=4, space=chr(32)):
""" Precede each carriage return with some quantity of spaces.
While nesting `indented_output` contexts, be prepared
@xhjkl
xhjkl / curry.py
Last active February 20, 2016 00:41
Convenient Currying for Python Decorators
from functools import partial, wraps
def curry(f):
""" Represent given n-ary function
as a sequence of unary functions.
Useful for function decorators with arguments.
Each unary function provides a closure around