This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Tree | |
( | |
Tree, | |
fromList, | |
singleNode, | |
insert, | |
node, | |
toList | |
) where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Set | |
( | |
fromList, | |
intersection, | |
union | |
) where | |
import qualified Tree | |
data Set a = Set (Tree.Tree a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |