This file contains hidden or 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
| {-#LANGUAGE DataKinds #-} | |
| {-#LANGUAGE DeriveDataTypeable #-} | |
| {-#LANGUAGE DeriveFoldable #-} | |
| {-#LANGUAGE DeriveFunctor #-} | |
| {-#LANGUAGE DeriveGeneric #-} | |
| {-#LANGUAGE DeriveTraversable #-} | |
| {-#LANGUAGE GADTs #-} | |
| {-#LANGUAGE GeneralizedNewtypeDeriving #-} | |
| {-#LANGUAGE KindSignatures #-} | |
| {-#LANGUAGE TypeFamilies #-} |
This file contains hidden or 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
| class Option(object): | |
| def __init__(self, is_defined, value=None): | |
| self.is_defined = is_defined | |
| if is_defined: | |
| self.value = value | |
| @classmethod | |
| def Some(cls, value): | |
| return Option(True, value) |
This file contains hidden or 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
| object Chunked { | |
| def getItems(start: Int, count: Int): List[Int] = { | |
| // Simulate doing I/O to get the items. | |
| val items = List.range(start*count, (start*count)+count) | |
| println(s"> Database returned: ${items}") | |
| items | |
| } | |
| def chunked(count: Int): Stream[Int] = { |
This file contains hidden or 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
| {-#LANGUAGE DeriveFunctor #-} | |
| {-#LANGUAGE DeriveFoldable #-} | |
| {-#LANGUAGE DeriveGeneric #-} | |
| {-#LANGUAGE DeriveTraversable #-} | |
| {-#LANGUAGE FlexibleInstances #-} | |
| {-#LANGUAGE StandaloneDeriving #-} | |
| {-#LANGUAGE TypeFamilies #-} | |
| module Snake | |
| ( GameIO |
This file contains hidden or 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 scala.language.higherKinds | |
| // The point of this exercise is to build up a data type isomorphic to List, | |
| // using basic data types like Option and Tuple2. Along the way, we define | |
| // functors, compositions of functors, fixed-points of functors, catamorphisms, | |
| // and tagged types. | |
| // | |
| // I'm new to Scala, so there are probably prettier ways to do many of these | |
| // things. | |
| object Test { |
This file contains hidden or 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 scala.language.higherKinds | |
| object Test { | |
| // Functors | |
| trait Functor[F[_]] { | |
| def fmap[A, B](fa: F[A])(f: A => B): F[B] | |
| } | |
| implicit def OptionFunctor: Functor[Option] = new Functor[Option] { | |
| def fmap[A, B](fa: Option[A])(f: A => B) = fa map f |
This file contains hidden or 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
| // Class Macro with Support for Inheritance | |
| // ======================================== | |
| macro class { | |
| rule { $className($cparam:ident (,) ...) { $cbody } } => { | |
| function $className($cparam (,) ...) { | |
| if (!(this instanceof $className)) { | |
| return new $className($cparam (,) ...); | |
| } | |
| $cbody ... |
This file contains hidden or 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| void print_bits(int i) { | |
| printf("\n "); | |
| int n = sizeof(int) * 8; | |
| for (int j = n-1; j >= 0; j--) { | |
| if (!((j+1) % 8)) { | |
| printf(" "); | |
| } |
This file contains hidden or 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
| // This is the signaling server for relaying messages between Node.js and | |
| // Chrome. Run it with | |
| // | |
| // $ node server.js | |
| // | |
| function SignalingServer() { | |
| var clients = {}; | |
| var WebSocketServer = require('ws').Server; | |
| var wss = new WebSocketServer({ |
This file contains hidden or 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
| // This supports more cases but removes support for if-statements. | |
| macro $do { | |
| // Base Cases | |
| case {_ { $ma:expr } } => { | |
| return #{ | |
| function() { | |
| return $ma | |
| }() | |
| } |