Skip to content

Instantly share code, notes, and snippets.

View fizruk's full-sized avatar
♾️

Nikolai Kudasov fizruk

♾️
View GitHub Profile
@fizruk
fizruk / midpoint.hs
Created April 24, 2015 18:16
Midpoint circle algorithm.
module Main where
-- | Get octant points for a circle of given radius.
octant :: (Num a, Ord a) => a -> [(a, a)]
octant r = takeWhile inOctant . map fst $ iterate step ((r, 0), 1 - r)
where
-- check if we are still in octant
inOctant (x, y) = x >= y
-- go to the next point in the circle
@fizruk
fizruk / docker-push.sh
Created November 10, 2015 17:12
Push to Docker from Circle CI
#!/bin/sh
set -x
CIRCLE_TAG=`git name-rev --tags --name-only $(git rev-parse HEAD)`
docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
if [ "$CIRCLE_TAG" -ne "undefined" ]; then
docker tag user/repo user/repo:$CIRCLE_TAG
@fizruk
fizruk / gst.html
Last active November 11, 2015 15:35
GetShopTV Analytics Code
<!-- GetShopTV Analytics -->
<script>
(function(t,e,n,s,a,c,i){t.GetShopTVAnalyticsObject=a,t[a]=t[a]||function(){
(t[a].q=t[a].q||[]).push(arguments)},t[a].l=1*new Date,c=e.createElement(n),
i=e.getElementsByTagName(n)[0],c.async=1,c.src=s,i.parentNode.insertBefore(c,i)
})(window,document,"script","//api.getshop.tv/static/analytics.js","gst");
</script>
<!-- END GetShopTV Analytics -->
<!-- Manually trigger GetShopTV events wherever applicable -->
@fizruk
fizruk / Model.hs
Last active November 13, 2015 10:41
Enforcing data model constraints on type level.
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
@fizruk
fizruk / BinarySearch.hs
Last active November 17, 2015 10:16
Project Euler #187 in Haskell
module BinarySearch where
import Data.Vector.Unboxed (Vector, (!), Unbox)
import qualified Data.Vector.Unboxed as Vector
-- | Binary search in a Vector.
binary :: (Ord a, Unbox a) => a -> Vector a -> Int
binary x xs = binary' 0 (Vector.length xs - 1)
where
binary' a b
@fizruk
fizruk / Declare.hs
Last active December 11, 2015 23:03
Declare monad transformer
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Control.Monad.Declare.Lazy where
import Control.Monad
import Control.Monad.Trans
import Data.Functor.Identity
import Data.Monoid
@fizruk
fizruk / PDividing.hs
Last active January 29, 2016 02:20
Generic mappend for product-like types using Profunctor analogue of both Decidable and Applicative.
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module PDividing where
import Data.Foldable
import Data.Monoid
import Data.Profunctor
@fizruk
fizruk / DecidingL1.hs
Created January 29, 2016 11:10
Generic foldMap using Decidable1
class Contravariant1 p where
contramap1 :: (a -> b) -> p b x -> p a x
class Contravariant1 p => Divisible1 p where
conquer1 :: p a x
divide1 :: (a -> (b, c)) -> p b x -> p c x -> p a x
class Divisible1 p => Decidable1 p where
lose1 :: (a -> Void) -> p a x
choose1 :: (a -> Either b c) -> p b x -> p c x -> p a x
@fizruk
fizruk / DecidingR1.hs
Last active January 30, 2016 15:01
Generic foldMap through Decidable.
class (Generic1 t, GDecidingR1 q (Rep1 t)) => DecidingR1 q t where
decidingR1 :: Decidable (f b) => p q -> (forall g. q g => f b (g a)) -> f b a -> f b (t a)
instance (Generic1 t, GDecidingR1 q (Rep1 t)) => DecidingR1 q t where
decidingR1 p f r = contramap from1 $ gdecidingR1 p f r
class GDecidingR1 q t where
gdecidingR1 :: Decidable (f b) => p q -> (forall g. q g => f b (g a)) -> f b a -> f b (t a)
class Unit1 a
instance Unit1 a
gfoldMap :: (Deciding1 Unit1 f, Monoid m) => (a -> m) -> f a -> m
gfoldMap = flip . getOp $ deciding1 (Proxy :: Proxy Unit1) (Op mempty) (Op (flip id))