Skip to content

Instantly share code, notes, and snippets.

View Javran's full-sized avatar

Javran Cheng Javran

View GitHub Profile
@Javran
Javran / gist:3672733
Created September 8, 2012 08:13
an (stupid) attempt of adding logging function to State monad..
import Control.Monad
import Control.Monad.State
type ListZipper a = ([a], [a])
-- move focus forward, put previous root into breadcrumbs
goForward :: ListZipper a -> ListZipper a
goForward (x:xs, bs) = (xs, x:bs)
-- move focus back, restore previous root from breadcrumbs
@Javran
Javran / gist:3673869
Created September 8, 2012 11:29
use State monad and WriterT to implement ListZipper
import Control.Monad
import Control.Monad.State
import Control.Monad.Trans.Writer
type ListZipper a = ([a], [a])
-- move focus forward, put previous root into breadcrumbs
goForward :: ListZipper a -> ListZipper a
goForward (x:xs, bs) = (xs, x:bs)
@Javran
Javran / gist:3673898
Created September 8, 2012 11:30
use Writer monad and StateT to implement ListZipper
import Control.Monad
import Control.Monad.Writer
import Control.Monad.Trans.State
type ListZipper a = ([a], [a])
-- move focus forward, put previous root into breadcrumbs
goForward :: ListZipper a -> ListZipper a
goForward (x:xs, bs) = (xs, x:bs)
@Javran
Javran / gist:3719575
Created September 14, 2012 03:12
project euler p-104, why did this program eat up all my memory space?
import Data.Char
import Data.List
fibSeq :: [(Integer, Integer)]
fibSeq = map (\(n, (a,_)) -> (n, a)) $ iterate (\(n, (a,b))->(n+1, (b,a+b))) (1,(1,1))
fstPandigital :: Integer -> Bool
fstPandigital n = charNum == "123456789" where
charNum = sort $ take 9 $ show n
@Javran
Javran / gist:3728108
Created September 15, 2012 14:06
LYAH last assignment - a simple file system, using Either & WriterT
import Data.List (break)
import Control.Monad
import Control.Monad.Instances
import Control.Monad.Trans
import Control.Monad.Trans.Writer
type Name = String
type Data = String
data FSItem = File Name Data | Folder Name [FSItem] deriving (Show)
@Javran
Javran / gist:3765122
Created September 22, 2012 04:34
anonymous function in c++ !
// compile with args: -lstdc++ -std=c++0x
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <numeric>
int a[16];
int main()
@Javran
Javran / gist:3794183
Created September 27, 2012 14:03
not-a-well-optimized solution for project euler p-14
-- complied with: ghc --make problem-14 -rtsopts
-- run with: ./problem-14 +RTS -K20000000 -RTS
-- time elapsed: 32.592s
import qualified Data.Set as S
-- (set, (x,value))
-- set: a set to keep numbers that has been calculated
-- x: a number
@Javran
Javran / gist:4022506
Created November 6, 2012 04:14
interface vs class itself
public class Test {
public static class A {}
public static class B extends A {}
public static class C extends A {}
public interface Hello {
void hello(A a);
}
public static class Tester implements Hello {
@Javran
Javran / gist:4037813
Created November 8, 2012 09:41
refresh media lib
adb shell am start -a android.intent.action.MAIN -n com.android.development/.MediaScannerActivity
@Javran
Javran / gist:4051704
Created November 10, 2012 16:58
....
f = file( "raw", "r" )
raw = f.read()
f.close()
secret = ''.join( map( lambda x:chr( int(x,2)) , map( lambda x: x.replace("____", "1111") , raw.split() ) ) ) + "="
import base64
g = file("sec", "wb")
g.write( base64.b64decode( secret ) )