Skip to content

Instantly share code, notes, and snippets.

View newlawrence's full-sized avatar

Alberto Lorenzo Márquez newlawrence

View GitHub Profile
@newlawrence
newlawrence / plural_maker.hs
Last active February 7, 2017 21:25
A proof of concept of a small plural sentences creator
import qualified Data.Char as Char
data Things t = Things {thing :: t, quantity :: Int}
instance (Show t) => Show (Things t)
where
show t = if quantity t == 1 then get t else get t ++ "s"
where
get = filter (not . (`elem` "\"")) . show . thing
makeSentence :: (Show t) => [Things t -> String] -> t -> Int -> String
@newlawrence
newlawrence / little_parser.py
Last active May 29, 2017 12:10
A quick and dirty implementation of a math expression parser in Python
import math
def parse(expression, variables=None, context=vars(math)):
variables = ','.join(variables) if variables is not None else ''
context = dict(context) if context is not None else {}
return eval('lambda {}: {}'.format(variables, expression), None, context)
@newlawrence
newlawrence / memoized_property.py
Last active January 13, 2017 14:51
A simple decorator to define memoized properties in Python
import weakref
class memoized_property(property):
def __init__(self, *args, **kwargs):
super(memoized_property, self).__init__(*args, **kwargs)
self.data = weakref.WeakKeyDictionary()
def __get__(self, instance, owner=None):