Skip to content

Instantly share code, notes, and snippets.

View dwickstrom's full-sized avatar
🐎

David Wickström dwickstrom

🐎
View GitHub Profile
@dwickstrom
dwickstrom / foo.py
Created June 22, 2016 10:00
Workaround for the inability to access the full context inside a block inside a StreamField in Wagtail
{% for block in self.body %}
<section class="{{ block.block_type }}">
{% if block.block_type == 'fooblock' %}
{% get_foo block.value request %}
{% else %}
{{ block}}
{% endif %}
</section>
{% endfor %}
;[true, true, false].every(id => id) // false
;[true, true, true].reduce((x, y) => x && y) // true
@dwickstrom
dwickstrom / map.py
Last active September 21, 2016 16:32
Some sort of fmap implementation in Python
import inspect
def fmap(f, functor):
def _dict_map(f, functor):
result = {}
for key, value in functor.items():
result[key] = f(value)
return result
def _object_map(f, functor):
# filter_by :: [Kwarg] -> lambda QuerySet
def filter_by(**kwargs):
return lambda qs: qs.filter(**kwargs)
# filter_by_type :: QuerySet -> QuerySet
filter_by_type = lambda x: filter_by(type=x)
# filter_by_project :: QuerySet -> QuerySet
import functools
def compose(*functions):
return functools.reduce(lambda f, g: lambda x: f(g(x)), functions, lambda x: x)
def curry(fn):
def curried(*args, **kwargs):
if len(args) + len(kwargs) >= fn.__code__.co_argcount:
return fn(*args, **kwargs)
@curry
def safe_cast(to_type, value):
try:
return Just.of(to_type(value))
except:
return Nothing
to_float = safe_cast(float)
to_int = safe_cast(int)
to_str = safe_cast(str)
@dwickstrom
dwickstrom / django-model-fields.py
Created December 27, 2016 12:31
Get attribute settings off a model
# get_fields :: Model -> [Field]
def get_fields(model):
return model._meta.get_fields()
# get_field :: String -> Model -> Field
@curry
def get_field(name, model):
return model._meta.get_field(name)
@dwickstrom
dwickstrom / foo.sh
Created May 2, 2017 09:21
Remove dangling images
docker rmi $(docker images -qa -f 'dangling=true')

Keybase proof

I hereby claim:

  • I am dwickstrom on github.
  • I am dwickstrom (https://keybase.io/dwickstrom) on keybase.
  • I have a public key ASC9XLlpE9HjjPKrx6xLsiUrus-PsONoWrT4wmmJass3qwo

To claim this, I am signing this object:

@dwickstrom
dwickstrom / heads-tails.js
Created July 5, 2017 16:21
destructure head/tail from list
// take :: Int -> [a] -> [a]
const take = n => ([x, ...xs]) =>
!x ?
[] :
n === 0 ?
[] :
[x, ...take (n-1) (xs)]