Skip to content

Instantly share code, notes, and snippets.

View JustinSDK's full-sized avatar
👨‍👩‍👧
Working from home

Justin Lin JustinSDK

👨‍👩‍👧
Working from home
View GitHub Profile
function(sprite, spriteContainer) {
var spriteMouseDowns = Observable.fromEvent(sprite, "mousedown"),
spriteContainerMouseMoves = Observable.fromEvent(spriteContainer, "mousemove"),
spriteContainerMouseUps = Observable.fromEvent(spriteContainer, "mouseup"),
spriteMouseDrags =
// For every mouse down event on the sprite...
spriteMouseDowns.
flatMap(function() {
return spriteContainerMouseMoves;
}).takeUntil(spriteContainerMouseUps);
function(sprite, spriteContainer) {
// All of the mouse event sequences look like this:
// seq([ {pageX: 22, pageY: 3423, offsetX: 14, offsetY: 22} ,,, ])
var spriteMouseDowns = Observable.fromEvent(sprite, "mousedown"),
spriteContainerMouseMoves = Observable.fromEvent(spriteContainer, "mousemove"),
spriteContainerMouseUps = Observable.fromEvent(spriteContainer, "mouseup"),
// Create a sequence that looks like this:
// seq([ {pageX: 22, pageY:4080 },,,{pageX: 24, pageY: 4082},,, ])
spriteMouseDrags =
// For every mouse down event on the sprite...
@JustinSDK
JustinSDK / YCombinator.hs
Last active March 9, 2017 14:33
Y Combinator in Haskell
import Unsafe.Coerce
y :: (a -> a) -> a
y = \f -> (\x -> f (unsafeCoerce x x))(\x -> f (unsafeCoerce x x))
-- Ref: [Y Combinator in Haskell](http://stackoverflow.com/questions/4273413/y-combinator-in-haskell)
-- a fibonacci example
main = putStrLn $ show $ y (\fact -> \n -> if n < 2 then 1 else n * fact (n - 1)) 5
@JustinSDK
JustinSDK / Map.hs
Created December 17, 2014 03:21
〈Haskell Tutorial(14)減輕型態負擔的型態參數〉的答案
data Map k v = Empty | Cm (k, v) (Map k v)
fromList :: [(k, v)] -> Map k v
fromList [] = Empty
fromList (x:xs) = Cm x (fromList xs)
findValue :: (Eq k) => k -> Map k v -> Maybe v
findValue key Empty = Nothing
findValue key (Cm (k, v) xm) =
if key == k then Just v else findValue key xm
@JustinSDK
JustinSDK / Comp.hs
Created December 22, 2014 07:43
〈Haskell Tutorial(15)Typeclass 定義、實作與衍生〉的答案
instance Comp Integer where
comp x y = x - y
instance (Comp a) => Comp (Maybe a) where
comp (Just x) (Just y) = comp x y
comp (Just _) Nothing = 1
comp Nothing (Just _) = -1
comp Nothing Nothing = 0
quicksort :: Comp a => [a] -> [a]
@JustinSDK
JustinSDK / SubType.hs
Last active August 29, 2015 14:11
Haskell 次型態多型的模擬
data Customer = Customer String Int deriving Show
data Vip = Vip Customer Float deriving Show
class CustBehavior c where
custAction :: Show c => c -> String
class CustBehavior c => VipBehavior c where
vipAction :: Show c => c -> String
instance CustBehavior Customer where
@JustinSDK
JustinSDK / Tree.hs
Last active August 29, 2015 14:13
〈Haskell Tutorial(19)Data.Set 與 Data.Map 模組〉的題目解答之一
module Tree
(
Tree,
fromList,
singleNode,
insert,
node,
toList
) where
@JustinSDK
JustinSDK / Set.hs
Created January 15, 2015 03:06
〈Haskell Tutorial(19)Data.Set 與 Data.Map 模組〉題目解答之二
module Set
(
fromList,
intersection,
union
) where
import qualified Tree
data Set a = Set (Tree.Tree a)
@JustinSDK
JustinSDK / IO.hs
Last active August 29, 2015 14:14
〈Haskell Tutorial(21)來寫些迴圈吧!〉的解答
while :: Bool -> IO () -> IO ()
while cond value = do
if cond then value
else return ()
forever' :: IO a -> IO ()
forever' value = do
while True $ do
value
forever' value