Skip to content

Instantly share code, notes, and snippets.

View paolino's full-sized avatar

Paolo Veronelli paolino

  • Cardano Foundation
  • sesimbra, portugal
View GitHub Profile
@paolino
paolino / Closures.hs
Last active July 16, 2016 15:36
Exercising closures.
module Closures where
-- Learn to mimic the concept of (oop) encapsulation.
-- From paolino, with love to his uscs (summer school 2016) mates
--------------------------------
-- Library for place holders ----
---------------------------------
@paolino
paolino / andrew'smonotone.hs
Created July 21, 2016 19:40
computation of convex hull
{-https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain-}
{-# language ViewPatterns #-}
{- perimeter of convex hull of points -}
import Data.List
type P = (Double,Double)
(.-.) :: P -> P -> P
(x1, y1) .-. (x2, y2) = (x1 - x2, y1 - y2)
{-# language ViewPatterns #-}
import System.IO (hSetBuffering, BufferMode(NoBuffering), stdout)
import Data.Ord (comparing)
import Data.List (minimumBy)
import Control.Applicative ((<$>))
data Dir = SE | E | NE | S | R | N | SW | W | NW deriving (Enum, Show,Eq)
dist (x,y) ((-) x -> cx, (-) y -> cy) = sqrt $ cx ^ 2 + cy ^ 2
import Control.Applicative
import Control.Monad.List
import Control.Monad.State
liftLT :: Monad m => [a] -> ListT m a
liftLT = ListT . return
produceAndCountTuples :: [a] -> [b] -> ListT (State Int) (a,b)
produceAndCountTuples xs ys = do
@paolino
paolino / pipe_average.hs
Created October 7, 2016 21:39
Pipes + fingertrees
{-# language FlexibleInstances #-}
{-# language MultiParamTypeClasses #-}
{-
Keeping a monoid running on a window of the stream
-}
import Pipes
import qualified Pipes.Prelude as P
import Control.Monad.Fix
@paolino
paolino / Results.hs
Last active October 15, 2016 14:27
drive by result
{-# language DataKinds #-}
{-# language TypeFamilies #-}
{-# language MultiParamTypeClasses #-}
{-# language FlexibleInstances #-}
data Operation = Split | Correct
class At (c:: Operation) b a where
type AtPred c (b :: * -> *) a
@paolino
paolino / dmapreflex.hs
Last active October 19, 2016 19:46
where to use dmap in higher order reflex-dom
{-# language RecursiveDo #-}
{-# language ConstraintKinds #-}
{-# language FlexibleContexts #-}
{-# language OverloadedStrings #-}
{-# language TemplateHaskell #-}
{-# language GADTs #-}
import Reflex.Dom
import Data.Text (pack, Text)
import qualified Data.Text as T
@paolino
paolino / stack.yaml
Created October 20, 2016 12:08
reflex + servant + router stack
resolver: lts-7.2
compiler: ghcjs-0.2.1.9007002_ghc-8.0.1
compiler-check: match-exact
setup-info:
ghcjs:
source:
ghcjs-0.2.1.9007002_ghc-8.0.1:
url: http://ghcjs.tolysz.org/ghc-8.0-2016-10-01-lts-7.2-9007002.tar.gz
sha1: a41ae415328e2b257d40724d13d1386390c26322
@paolino
paolino / perfectshuffle.hs
Last active October 22, 2016 21:00
a perfect shuffle using fingertrees
{-# language ViewPatterns #-}
{-# language FlexibleInstances #-}
{-# language MultiParamTypeClasses #-}
-- http://okmij.org/ftp/Haskell/perfect-shuffle.txt
import System.Random (randoms, newStdGen)
import Data.FingerTree (FingerTree, split, Measured (..),
ViewL (..), viewl, fromList)
import Data.Monoid (Sum (..), (<>))
@paolino
paolino / perfectshuffle.hs
Created October 22, 2016 21:06
naive perfectshuffle via bin tree
data Bin a = Leaf a | Bin (Bin a) Int (Bin a) deriving Show
count (Bin _ n _ ) = n
count (Leaf _) = 1
fromListB :: [a] -> Bin a
fromListB = head . head . dropWhile ((>1) . length) .
iterate (collapse id ((+) `on` count) ) . collapse Leaf (\_ _ -> 2)
collapse f g (x:y:rest) = Bin (f x) (x `g` y) (f y) : collapse f g rest