Skip to content

Instantly share code, notes, and snippets.

@xfire
xfire / gist:111923
Created May 14, 2009 21:07
python decorator example
#!/usr/bin/env python
#
# vim:syntax=python:sw=4:ts=4:expandtab
import functools
def wrap(orig_func):
# now some funky python magic
@functools.wraps(orig_func)
def outer(new_func):
@xfire
xfire / Functor.hs
Created July 2, 2010 15:30
haskell functor examples
data Tree a = Node (Tree a) (Tree a)
| Leaf a
deriving Show
instance Functor Tree where
fmap g (Leaf v) = Leaf (g v)
fmap g (Node l r) = Node (fmap g l) (fmap g r)
sampleTree :: Tree String
sampleTree = Node (Node (Leaf "hello") (Leaf "foo")) (Leaf "baar")
class Foo(val a: Int, val b: String) {
private var _c: Int = 42 // fuer die meisten default
def c: Int = _c
def this(a: Int, b: String, c: Int) = {
this(a, b)
_c = c
}
}
@xfire
xfire / gist:742493
Created December 15, 2010 19:43
view bound with type constructor compilation error
trait Foo[TC[_], V]
/* compilation error
* -----------------
* view_bound_error.scala:11: error: type T takes type parameters
* def viewBound[T[V] <% Foo[T, V], V](value: T[V]) = value
* ^
* one error found
*/
def viewBound[T[V] <% Foo[T, V], V](value: T[V]) = value
@xfire
xfire / TreeZipper.hs
Created July 6, 2011 18:35
tree zipper
-- little helper function
-- f3 (f2 (f1 x)) == x -: f1 -: f2 -: f3
(-:) x f = f x
-- our data structure
data Tree a = Empty
| Node (Tree a) a (Tree a)
deriving Show
-- a step on the way through our tree.
@xfire
xfire / ListZipper.hs
Created July 6, 2011 19:22
list zipper
-- little helper function
-- f3 (f2 (f1 x)) == x -: f1 -: f2 -: f3
(-:) x f = f x
-- the list zipper simply split the list in two parts.
type ListZipper a = ([a], [a])
makeListZipper :: [a] -> ListZipper a
makeListZipper l = (l, [])
@xfire
xfire / TreeZipperSafe.hs
Created July 7, 2011 15:48
safe tree zipper using maybe
import Data.Foldable as F
-- our data structure
data Tree a = Empty
| Node (Tree a) a (Tree a)
deriving Show
-- a step on the way through our tree.
-- either going to the left or to the right.
data Direction a = DLeft a (Tree a)
@xfire
xfire / ibm_temp.hs
Created July 12, 2011 19:27
xmobar util to read temperature on lenovo notebooks
{- xmobar util to read temperature on lenovo notebooks
-
- usage:
- , Run Com "/home/fire/.xmonad/ibm_temp" ["40", "#B6B4B8", "red"] "temp" 10
-}
import System( getArgs )
import Data.Maybe
import Control.Monad
import Control.Applicative
@xfire
xfire / MonadTrans.hs
Created July 13, 2011 16:02
monad transformer stack example
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Writer
-- our monad transformer stack
-- a reader with an prefix string for the log
-- a state with the current value as integer
-- a writer to log the work done
-- all in a IO monad
type Prefix = String
@xfire
xfire / MonadTransV2.hs
Created July 13, 2011 18:15
monad transformer stack example, newtype version
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Writer
-- our monad transformer stack
-- a reader with an prefix string for the log
-- a state with the current value as integer
-- a writer to log the work done