Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
@purpleP
purpleP / merge.py
Last active February 19, 2019 13:43
merge
from collections import Counter
import hypothesis.strategies as st
import hypothesis as hp
import pytest
def merge(xs, ys):
xs, ys = iter(xs), iter(ys)
none = object()
y = next(ys, none)
from bisect import bisect_left, bisect_right
def pairs_sums_to(k, xs):
xs = [*sorted(xs)]
lo, hi = 0, len(xs) - 1
while True:
left_pos = bisect_left(xs, k, lo, hi)
if left_pos > hi or left_pos == lo:
return
class split:
def __init__(self, delimiter, sequence):
"""
Break a sequence on particular elements.
Return an iterator over chunks.
Arguments:
delimiter if a function, it returns True on chunk separators;
otherwise, it is the value of chunk separator.
@purpleP
purpleP / min_max_open_close
Created October 12, 2017 19:06
Minumum, maximum and first and last from group without `first` and `last` in mysql (it doesn't have them)
create table `values` (
id integer unsigned primary key auto_increment,
value double,
`time` time
);
insert into `values` (time, value) values
('00:00:01', 3),
('00:00:02', 2),
('00:00:03', 1),
@purpleP
purpleP / minmax.hs
Created September 5, 2017 13:33
Efficient algorithm for finding minimum and maximum value from sequence
import Data.Tuple (swap)
import System.Environment (getArgs)
minmax (a:b:xs) = foldl (minmax') (sort' (a, b)) (pairs xs)
where minmax' (curmin, curmax) candidates = (newmin, newmax)
where (cmin, cmax) = sort' candidates
newmin = if cmin < curmin then cmin else curmin
newmax = if cmax > curmax then cmax else curmin
sort' tup@(a, b) = if a <= b then tup else swap tup
import Data.Function.Memoize (memoize)
import Data.List (maximumBy, group)
import qualified Data.Map as Map
import Data.Map.Lazy (empty, insertWith, size, Map)
import Data.Ord (comparing, compare, Down)
import Data.Monoid
import System.Environment (getArgs)
type Solution = (Map Integer Integer, Integer)

If constructing an object is entagled with different cases and conditions it's better to use factory. That's not the case here.

Instead of this.

def make_board(mines, size):
    '''function that uses my created Board methods to create the playing board'''
    board = Board(tuple([tuple([Cell(True,True,True) for i in range(size+1)]) for j in range(size+1)]))
    open_pos = list(range(size-1)*(size-1))
    for i in range(mines):
@purpleP
purpleP / changes.hs
Created March 9, 2017 22:33
attempt at json serialization in haskell
{-# LANGUAGE DeriveGeneric, DeriveAnyClass, StandaloneDeriving #-}
import Data.Map (empty, fromList, insert, delete, Map)
import GHC.Generics
import Data.Aeson
import qualified Data.Text.Lazy.IO as T
import qualified Data.Text.Lazy.Encoding as T
data Task = Task
{ id :: String
@purpleP
purpleP / vim-tmux.md
Created January 29, 2017 00:37
How to run tests with tmux and vim

Running tests with vim and tmux

After using vim and neovim for almost a year now I've come to the point when frustration I had with running test with vim made me think about ideal workflow for running tests (at least in command line, but some rules apply to GUI).

Below you can read my thoughts about ideal workflow, a summary of problems with current workflows (at least that I know of, but I think I've tried them all) and my own new (well, at least I think it's new) solution to them.