Skip to content

Instantly share code, notes, and snippets.

View melrief's full-sized avatar

Mario Pastorelli melrief

View GitHub Profile
@melrief
melrief / swap.hs
Last active August 29, 2015 13:55
Print swap memory used by each process
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import Data.Char
import Data.List
import Data.Maybe
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as C
import Prelude hiding (readFile)
import scala.language.higherKinds
import scala.math.{pow}
import scalaz._
import Scalaz._
import scalaz.effect._
case class Point(_x : Double, _y : Double)
case class GameUnit(_health : Int, _position : Point)
@melrief
melrief / vimrc
Created July 10, 2014 19:25
Set the current selection indent to the indent of the line above it with <C-Tab> in vim
function! IndentAsPrevLine()
python << EOF
import itertools as I
from vim import *
if current.range.start > 0:
def takewhile(pred,l): return ''.join(I.takewhile(pred,l))
prev_line = current.buffer[current.range.start - 1]
@melrief
melrief / XRandR.hs
Last active August 29, 2015 14:08
XRandR parsing with parsec
{-
Parsing the output of the xrandr command. For example:
$> xrandr
Screen 0: minimum 8 x 8, current 1920 x 1080, maximum 32767 x 32767
eDP1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 309mm x 173mm
1920x1080 60.0*+ 59.9
1680x1050 60.0 59.9
1600x1024 60.2
1400x1050 60.0
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TupleSections #-}
{-
Parsing the output of the xrandr command. For example:
$> xrandr
Screen 0: minimum 8 x 8, current 1920 x 1080, maximum 32767 x 32767
@melrief
melrief / init_sbt_project.sh
Last active August 29, 2015 14:12
This is a simple script to create an sbt template project in a directory
#!/bin/bash -eu
#
# Set up a sbt project. This script requires sbt and bash :).
#
# usage:
# - mkdir <project_name>
# - cd <project_name>
# - ./<this_script_dir>/init_sbt_project.sh
#
# author: Mario Pastorelli <pastorelli.mario@gmail.com>
package csvShapeless
import shapeless._, labelled.{ field, FieldType }, syntax.singleton._
import scala.util.{Try,Success,Failure}
case class Person(name: String, surname: String, age: Int)
object Main extends App {
@melrief
melrief / lines.hs
Created July 19, 2013 19:14
Lazily prepend the line number to each line
import Control.Monad (unless)
import Prelude hiding (catch)
import System.IO (isEOF)
prependLineNums :: Int -> IO ()
prependLineNums n = isEOF >>= flip unless prependLineNum
where prependLineNum = getLine >>= putStrLn . (++) (padTo 5 $ show n)
>> prependLineNums (n+1)
padTo c s = case c `compare` length s of
GT -> s ++ replicate (c - length s) ' '
import Data.Colour.RGBSpace.HSV (RGB, hsv)
nextColor :: (RealFrac a) => a -> a -> a -> (RGB a,a)
nextColor h s v = let h' = (h + golden_ratio_conjugate) % 360
in (hsv h' s v,h')
where golden_ratio_conjugate = 222.4922359499622 -- 0.618033988749895 * 360
dividend % divisor = dividend - divisor *
(fromIntegral $ floor (dividend/divisor))
goldenColors :: (RealFrac a) => a -> a -> a -> [RGB a]
@melrief
melrief / ViewConvexHull.hs
Created August 31, 2013 22:47
An example of Graphics.Rendering.Chart usage from the haskell chart package (https://github.com/timbod7/haskell-chart/wiki). Calculates and shows the convex hull (http://en.wikipedia.org/wiki/Convex_hull) from a finite list of points using the Graham's scan method (http://en.wikipedia.org/wiki/Graham_scan)
-- To build just copy this and convexhull.cabal somewhere
-- and then run cabal build
--
-- Usage Example:
-- > ./dist/build/viewConvexHull/viewConvexHull plot.pdf 1,10 10,5 4,6 7,9 20,1 4,2 5,6 8,2 3,9
--
-- this creates a pdf file called plot.pdf with the plot in the current directory
module Main where
import Control.Arrow