Skip to content

Instantly share code, notes, and snippets.

View yihuang's full-sized avatar
🏠
Working from home

yihuang yihuang

🏠
Working from home
  • crypto.com
  • Shenzhen, China
View GitHub Profile
@yihuang
yihuang / longest.hs
Created March 31, 2012 08:03
find longest continuous sub number sequence.
import Data.List
import Data.Function
isSeq :: [Int] -> Bool
isSeq l =
let l' = sort l
in all (==(-1)) $ zipWith (-) l' (tail l')
longest :: [Int] -> [Int]
longest = maximumBy (compare `on` length)
@yihuang
yihuang / ghc-optimization.hs
Created March 31, 2012 08:51
crazy ghc optimization
module Test (test) where
test :: [Int] -> Bool
test l = all (==(-1)) $ zipWith (-) l (tail l)
{-
ghc -ddump-simpl -O ghc-optimization.hs
after clean up:
@yihuang
yihuang / nested_data_structure.hs
Created April 17, 2012 01:18
adjust nested data structure
module Test where
import qualified Data.Vector as V
import qualified Data.HashMap.Strict as M
import Data.Hashable (Hashable)
type MyData k v = V.Vector (M.HashMap k v)
adjustVec :: Int -> (a -> a) -> V.Vector a -> V.Vector a
adjustVec ix f vec =
@yihuang
yihuang / mongodb-crash.py
Created April 24, 2012 09:13
this code will make mongodb server crash.
import pymongo
db = pymongo.Connection('dev')['fund']
db.system_js.update = '''
function () {
db.test.find().forEach(function(obj) {
continue;
});
}
'''
print db.system_js.update()
@yihuang
yihuang / bug.hs
Created May 3, 2012 15:11
demonstrate a bug
{-# LANGUAGE OverloadedStrings #-}
import Data.ByteString.Char8 ()
import Data.Monoid (mappend)
import qualified Blaze.ByteString.Builder as B
import Data.Conduit.Blaze (builderToByteString)
import Data.Conduit
import qualified Data.Conduit.List as CL
main :: IO ()
@yihuang
yihuang / build-ios-ghc.hs
Created June 24, 2012 01:51
build ghc-ios
diff --git a/Cabal/Distribution/Simple.hs b/Cabal/Distribution/Simple.hs
index 02a5255..c2a0859 100644
--- a/Cabal/Distribution/Simple.hs
+++ b/Cabal/Distribution/Simple.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE CPP #-}
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Simple
@@ -142,7 +143,10 @@ import Control.Monad (when)
@yihuang
yihuang / observer.hs
Created August 6, 2012 14:55
observer pattern in haskell
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.IntMap as M
import Data.IORef
import Data.Unique
{--
- listen :: Event a -> (a -> IO ()) -> IO ()
- fire :: Event a -> a -> IO ()
-}
@yihuang
yihuang / instance_of_typeclass.hs
Created August 16, 2012 14:20
There is an instance for any type class
{-# LANGUAGE GADTs #-}
-- take `Monad` for example
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
-- we can define an instance mechanically:
data IsMonad a where
@yihuang
yihuang / sum.hs
Created August 25, 2012 03:33
sum
sum' :: Int -> Int
sum' n = loop n 0
where
loop 0 x = x
loop n x = loop (n-1) (x+n)
main = print $ sum' 1000000000
@yihuang
yihuang / gist:3850293
Created October 8, 2012 01:42
fight output
$ python fight.py
-- 200(50) 200(50) <--> -- 200(50) 200(50)
200(50) 200(50) -- <--> 200(50) -- --
-- -- 200(50) <--> -- 200(50) 200(50)
R2 -> L5
普攻
反击
伤害 R2 45, L5 90
-- 200(50) 200(50) <--> -- 200(50) 200(50)
200(50) 110(75) -- <--> 155(75) -- --