Skip to content

Instantly share code, notes, and snippets.

View IwoHerka's full-sized avatar

Iwo Herka IwoHerka

View GitHub Profile
@IwoHerka
IwoHerka / def_delegator.py
Last active July 3, 2018 18:52
Class decorator for declaring delegated methods
class Delegated(object):
def __init__(self, delegated_name, attr):
self.attr_name = attr
self.delegated_name = delegated_name
def __get__(self, instance, owner):
if instance is None:
return self
else:
return getattr(self.delegate(instance), self.attr_name)
@IwoHerka
IwoHerka / random_list.py
Created July 16, 2018 16:44
Generate array of random objects (including other arrays)
import random
from random import choice, randint
from string import digits
# Default constructors for the ``random_list``.
default_constructors = (
object,
dict,
lambda: bool(randint(0, 1)),
lambda: randint(1, 1000),
@IwoHerka
IwoHerka / eval.lisp
Last active May 24, 2019 17:17
Lisp's eval function
(label eval (lambda (e a)
(cond
((atom e) (assoc e a)
((atom (car e))
(cond
((eq (car e) quote) (cadr e))
((eq (car e) atom) (atom (eval (cadr e) a)))
((eq (car e) eq) (eq (eval (card e a)
(eval (caddr e) a)))
((eq (car e) car) (car (eval (cadr e) a)))
(defn reduce*
[f acc coll]
(if (empty? coll)
acc
(recur f (f acc (first coll)) (rest coll))))
(defn map*
[f coll]
(reduce* (fn [acc x] (conj acc (f x))) [] coll))
(defn filter*
[p coll]
(reduce* (fn [acc x] (if (p x) (conj acc x) acc)) [] coll))
@IwoHerka
IwoHerka / map_js.js
Created June 28, 2019 10:05
Map implementation in JavaScript
map = (fn, [head, ...tail]) => (head === undefined ? [] : [fn(head), ...map(fn, tail)])

JavaScript:

map = (fn, [x, ...xs]) => (x === undefined ? [] : [fn(x), ...map(fn, xs)])

Python:

map = lambda f, xs: [f(x) for x in xs]

Haskell:

@IwoHerka
IwoHerka / ex.clj
Created September 14, 2019 18:42
Lightning Hiccup Tutorial Example 1
[:div
[:button#counter-btn
{:class "btn active"
:style {:padding 5}
:on-click (fn [e] (swap! counter inc))}
"Click me!"]]
@IwoHerka
IwoHerka / ex.html
Created September 15, 2019 15:20
Lightning Hiccup Tutorial Example 2
<div>
<button class="btn active"
id="counter-btn"
style="padding: 5px;">
Click me!
</button>
</div>
<!-- add also JavaScript code for
the event listener -->