Skip to content

Instantly share code, notes, and snippets.

import csv
import sys
class MachineStuck(Exception):
pass
class UnKnownOperation(Exception):
pass
import Data.Bifunctor
import Data.Char
import Data.Functor.Const
import Data.Functor.Identity
import Prelude hiding (Maybe(..))
newtype BiComp bf fu gu a b =
BiComp (bf (fu a) (gu b))
instance (Bifunctor bf, Functor fu, Functor gu) =>
import re
def _parse_sequence(sequence):
remainder_to_parse = sequence
tokens = []
while len(remainder_to_parse) > 0:
match2 = re.match(r"^(\(.*\))", remainder_to_parse)
if match2 is not None:
token = match2.group(1)
@vincent-prz
vincent-prz / monad.py
Last active August 1, 2018 11:11
Monads in Python: Maybe and IO
from functools import reduce
from typing import List, Generic, Callable, TypeVar, Optional, Union
T = TypeVar('T')
Y = TypeVar('Y')
Z = TypeVar('Z')
class Maybe(Generic[T]):
@vincent-prz
vincent-prz / combineMultiKeyReducers.js
Created August 9, 2017 20:31
Combine Redux reducers like a Tetris ninja
const applyReducer = (inputKeys, reducer, outputKeys, state, action) => {
// [NOTE] - need to handle the case where `state === undefined`.Indeed,
// Redux calls the reducers with an `undefined` state when
// the app is initializing.
const reducerArgs = inputKeys.map(key => (typeof state !== 'undefined' ? state[key] : undefined))
.concat([action])
const reduceResult = outputKeys.length === 1 ? [reducer(...reducerArgs)] : reducer(...reducerArgs)
return outputKeys.reduce(
(acc, key, index) => ({ ...acc, [key]: reduceResult[index] }), {})
}
@vincent-prz
vincent-prz / decorators.py
Created April 19, 2016 14:01
Home made implementation of test parametrization
from sys import modules
import inspect
def currying(elem):
"""
Decorator for currying a function.
f: (x, y) ->f(x, y) is transformed into g: x->f(x, elem)
:param elem:
:return: