Skip to content

Instantly share code, notes, and snippets.

View fizruk's full-sized avatar
♾️

Nikolai Kudasov fizruk

♾️
View GitHub Profile
#!/usr/bin/python
import socket
import sys
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
@fizruk
fizruk / Replay.hs
Last active August 29, 2015 14:13
Recording and replaying arbitrary FreeT computation. For what happened next see https://github.com/fizruk/replay-free
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Control.Monad.Trans
import Control.Monad.Free.Class
import qualified Control.Monad.Trans.Free as FT
import qualified Control.Monad.Free as F
@fizruk
fizruk / fmap.hs
Last active August 29, 2015 14:19
Juxtaposition of fmaps.
-- 3
fmap fmap fmap
= fmap . fmap
-- 4
fmap fmap fmap fmap
= (fmap . fmap) fmap
= fmap (fmap fmap)
-- 5
@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 / 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 / heapsort.hs
Last active December 17, 2015 04:19
Heap sort using State monad along with lenses to deal with heap.
{-# LANGUAGE TemplateHaskell, Rank2Types #-}
module Main where
import Prelude hiding (last)
import Control.Lens
import Control.Monad.State.Strict (StateT, evalStateT, put)
import Control.Monad.IO.Class (liftIO)
import Control.Monad (when)
import Control.Applicative ((<$>), (<*>), pure)
@fizruk
fizruk / chat.hs
Created May 10, 2013 16:33
Simple chat with bots using FreeT monad transformers for both bots and environment.
{-# LANGUAGE TypeFamilies, ExistentialQuantification, FlexibleInstances #-}
module Main where
import System.IO (isEOF, hFlush, stdout)
import Data.Char (toLower, isDigit)
import Data.Maybe (isNothing)
import Control.Monad.Trans.Free
import Control.Monad.IO.Class (MonadIO, liftIO)