Skip to content

Instantly share code, notes, and snippets.

@el-hult
el-hult / day4.html
Last active December 27, 2018 14:06
advent of code 2017 day 4 html page.
<!doctype html><html><head><meta charset="utf-8"/><script>
const isValid = raw => {
const words = raw.split(/\s+/)
// writeToBody("raw: " + raw)
// writeToBody("words: " + words)
const checker = (prevAcc, nextWord) => {
const acc = { set: prevAcc.set, count: prevAcc.count };
if( acc.set.has(nextWord) ) { acc.count = acc.count + 1 }
else { acc.set.add(nextWord) }
@el-hult
el-hult / gameOfLife.hs
Created May 7, 2019 19:37
1D Game of Life in the terminal
-- http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html
-- The above post learned me about this neat comonad instance! :D
data U x = U [x] x [x]
right,left:: U x -> U x -- shift focus of the zipper
right (U a b (c:cs)) = U (b:a) c cs
left (U (a:as) b c) = U as a (b:c)
-- the zipper is a functor
@el-hult
el-hult / comonadVectorDiff.hs
Created May 7, 2019 19:46
Finding right angles between 2D vectors using comonads
-- This is using a haskell comonad to find right angles between given geometrical vectors
-- The code is kind of overkill, but it is a cute showcase of comonads
-- a FocusSet. One element in Focus, and all other elements in a list.
data Fs a = Fs a [a] deriving (Show)
instance Functor Fs where
fmap fun (Fs k l) = Fs (fun k) (map fun l)
@el-hult
el-hult / comonadVectorDiff.hs
Created May 7, 2019 19:46
Finding right angles between 2D vectors using comonads
-- This is using a haskell comonad to find right angles between given geometrical vectors
-- The code is kind of overkill, but it is a cute showcase of comonads
-- a FocusSet. One element in Focus, and all other elements in a list.
data Fs a = Fs a [a] deriving (Show)
instance Functor Fs where
fmap fun (Fs k l) = Fs (fun k) (map fun l)
@el-hult
el-hult / typed_currencies_37.py
Last active August 22, 2019 19:22
An example on how one can use the typings module to make currency conversion work like a charm. The code uses only python 3.7 features.
import urllib.request
import json
import typing
import functools
SEK = typing.NewType('SEK',float)
EUR = typing.NewType('EUR', float)
ConversionRate = typing.NewType('ConversionRate',float)
ThreeLetterCurrencyCode = str # ISO4217
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@el-hult
el-hult / random_variables.py
Created September 23, 2019 10:25
Some classes for random variables, in a Pythonic way with inheritance. Fun way to make universal random number generation. Might be inefficient, but still very cool.
# standards
from abc import ABC
from itertools import islice
from collections import Counter
import types
# 3rd party
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
@el-hult
el-hult / random_variables_monkeypatch.py
Created September 23, 2019 10:27
A monkay-patching way to create random variables with overloaded plus operator. Not a pythonic way to do it, since monkey patching is generally frowned upon.
# standards
from itertools import islice
import types
# 3rd party
import numpy as np
class RandomVariable():
def __iter__(self):
class MemorizingNormalizer(nn.Module):
def __init__(self, d, eps, rho):
super().__init__()
self.means = nn.Parameter(torch.zeros(d), requires_grad=False)
self.vars = nn.Parameter(torch.ones(d), requires_grad=False)
self.eps = nn.Parameter(torch.tensor(eps, dtype=float), requires_grad=False)
self.rho = nn.Parameter(torch.tensor(rho, dtype=float), requires_grad=False)
def forward(self, x):
self.means.data = self.means * self.rho + (1 - self.rho) * x.mean(axis=0)
@el-hult
el-hult / cals2tiff.py
Last active June 13, 2021 20:49
Code for converting a CALS raster file to TIFF. Using vanilla Python.
"""
Python script that takes a folder, and for each CALS Raster file, it converts to a TIFF file.
https://en.wikipedia.org/wiki/CALS_Raster_file_format
file ending ".cal"
It so happens that TIFF can be compressed with Group 4 compression (as in faxes), and that is the compression format of CALS Raster images Type 1.