Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
MgaMPKAy / FstFD.hs
Created March 11, 2014 13:59
Various ways to get first component of tuples.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
import Prelude hiding (fst)
class Fst a b | a -> b where
fst :: a -> b
instance Fst (a, b) a where
@MgaMPKAy
MgaMPKAy / fizzbuzz.js
Last active January 4, 2016 13:39
Fizzbuzz?
(function (p) {
var arr = [];
while (!(function (n) {
return (function (p) {
return p
})(n)(true)(false)
})((function (p) {
return p(function (x) {
return function (y) {
return x
@MgaMPKAy
MgaMPKAy / type.hs
Created January 1, 2014 18:54
Generate type in runtime using existential and GADTs.
{-# LANGUAGE DataKinds, GADTs, TypeOperators, KindSignatures #-}
-- http://www.reddit.com/r/haskell/comments/1q93r2/haskell_the_language_most_likely_to_change_the/cdberll
data Nat = Z | S Nat deriving (Show)
data SNat n where
Zero :: SNat Z
Succ :: SNat n -> SNat (S n)
@MgaMPKAy
MgaMPKAy / list-monoid.hs
Last active January 1, 2016 16:49
Lists form a free monoid.
import Data.Monoid
h x = [x]
{--
h
A ----->[A]
\ |
\ |
@MgaMPKAy
MgaMPKAy / Hanoi.hs
Last active January 1, 2016 02:39
A no so successful attempt to implement a type-level hanoi, goal: http://www.blogjava.net/sean/archive/2009/11/23/303374.html
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-} -- for Show
data Zero
data Succ a
data from :-> to
data act1 :>> act2
infixr :>>
@MgaMPKAy
MgaMPKAy / Parser.hs
Created December 19, 2013 09:37
Bash command history parser
module Parser where
import Data.Generics
import Data.List.Split (splitOn)
import Control.Applicative ((<$>), (<*>))
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Data.Time hiding (parseTime)
-- A customised version of language-sh is needed
import qualified Language.Sh.Parser as P
@MgaMPKAy
MgaMPKAy / SimpleStateMonad.hs
Last active November 2, 2019 18:59
Trampolined state monad in Haskell, translated from RO Bjarnason's Stackless Scala With Free Monads,
newtype State s a = State {runS :: s -> (a, s)}
instance Monad (State s) where
return x = State $ \s -> (x, s)
ma >>= f = State $ \s -> let (a, s1) = runS ma s
in runS (f a) s1
get = State $ \s -> (s, s)
put x = State $ \_ -> ((), x)
@MgaMPKAy
MgaMPKAy / toggle_ptrace
Created October 22, 2013 15:10
Toggle ptrace by setting /proc
toggle_ptrace() {
local enable
mapfile -t enable < /proc/sys/kernel/yama/ptrace_scope
enable=${enable[0]}
if [[ $enable == '0' ]]; then
enable=1
else
enable=0
fi
sudo sh -c "echo $enable > /proc/sys/kernel/yama/ptrace_scope"
@MgaMPKAy
MgaMPKAy / RecursiveFileList.hs
Last active December 22, 2015 20:29
List recursively all files under the giving path.
import Control.Applicative ((<$>))
import System.Directory
-- Because Hugs doesn't have System.FilePath.(</>)
(</>) :: FilePath -> FilePath -> FilePath
parent </> child = parent ++ "/" ++ child
getDirectoryContents' :: FilePath -> IO [FilePath]
getDirectoryContents' dir =
filter (`notElem` [".", ".."]) <$> getDirectoryContents dir
@MgaMPKAy
MgaMPKAy / CopyFile.hs
Created June 20, 2012 03:16
learn to correctly handle excepetions
module Main where
import System.IO
main = do
h1 <- openFile "file1" ReadMode
h2 <- openFile "file2" ReadMode
h3 <- openFile "file3" AppendMode
copy h1 h2 h3
hClose h1