Skip to content

Instantly share code, notes, and snippets.

@cqfd
cqfd / asink.cljs
Created May 10, 2014 14:42
Manually trampolined core.async thing.
(ns asink.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs.core.async :refer [<! timeout]]
[cljs.nodejs :as n]))
(n/enable-util-print!)
(defn spin []
(go (println "spinning...")
(<! (timeout 1000))
@cqfd
cqfd / Arith.js
Created February 19, 2014 18:06
Beginnings of an arithmetic expression evaluator thing
var assert = require('assert');
/*
* 1 + 2 * 3 --> 7
* Plus(1, Mult(2, 3))
*/
/*
* There are three kinds of arithmetic expressions:
*
@cqfd
cqfd / Main.scala
Created November 19, 2013 16:23
Little example of implementing gotos with Scala continuations.
import scala.util.continuations._
import scala.collection.mutable
object Main {
def main(args: Array[String]): Unit = {
val jumpTable: mutable.Map[String, Unit => Unit] = mutable.Map()
def label(s: String) = shift { k: (Unit => Unit) =>
jumpTable(s) = k
k()
}
@cqfd
cqfd / demo.rb
Created November 17, 2013 20:47
EventMachine websocket + raw TCP combo chat demo thing.
require 'em-websocket'
class ChatServer
def initialize
@clients = []
end
def start!
EM.start_server('0.0.0.0', 45678, RegularTcpConnection) do |conn|
conn.server = self
@clients << conn
@cqfd
cqfd / zulip.rb
Created November 12, 2013 20:40
Starting a Zulip EventMachine client
require 'em-http'
require 'json'
email = "eventmachinebot-bot@students.hackerschool.com"
api_key = "qHoWj0MEvawlKcj6mBly7EgFfWEu1AwJ"
class ZulipBot
def initialize(email, api_key)
@email = email
@api_key = api_key
@cqfd
cqfd / Main.scala
Created November 11, 2013 20:50
Akka EchoService stuff.
package foo
import akka.actor.Actor
import akka.actor.Props
import akka.pattern.ask
import akka.util.Timeout
import scala.util.{Try, Success, Failure}
import scala.concurrent.{ future, Future, Promise, ExecutionContext }
import scala.concurrent.duration._
@cqfd
cqfd / Arith.scala
Last active December 27, 2015 22:49
ScalaChecking arithmetic expressions.
sealed trait Arith
case class Num(n: Int) extends Arith
case class Plus(l: Arith, r: Arith) extends Arith
case class Mult(l: Arith, r: Arith) extends Arith
object Arith {
def interp(e: Arith): Int = e match {
case Num(n) => n
case Plus(l, r) => interp(l) + interp(r)
case Mult(l, r) => interp(l) * interp(r)
@cqfd
cqfd / gist:7387738
Created November 9, 2013 17:32
Minimax tic tac toe stuff.
board = [
0, 0, 0,
0, 0, 0,
0, 0, 0
]
def print_board(board):
print "%s | %s | %s" % (board[0], board[1], board[2])
print "%s | %s | %s" % (board[3], board[4], board[5])
print "%s | %s | %s" % (board[6], board[7], board[8])
@cqfd
cqfd / RecStuff.scala
Created October 19, 2013 01:20
Some recursion stuff in Scala.
import scala.annotation.tailrec
trait PeanoNumber
case object Z extends PeanoNumber
case class S(n: PeanoNumber) extends PeanoNumber
trait BinaryTree
case object EmptyTree extends BinaryTree
case class NonEmptyTree(n: Int, l: BinaryTree, r: BinaryTree) extends BinaryTree
@cqfd
cqfd / recursion.md
Last active December 25, 2015 21:49
Recursion!
  1. Write a recursive function to compute the length of a list.
  2. Write a recursive function to compute the sum of a list of numbers.
  3. Write a recursive function to compute the sum of a binary tree of numbers.
  4. Write a recursive implementation of the map function.
  5. Write a recursive implementation of the filter function.
  6. Write a recursive function that inserts an element in the right place into a linked list. The function should return a new linked list rather than mutate the original list.
  7. The change counting problem from SICP: http://mitpress.mit.edu/sicp/full-text/sicp/book/node16.html
  8. Write a recursive function that enumerates the nodes of a tree in depth-first {pre|in|post} order.
  9. Write a recursive function that computes the "power set" of a set.
  10. Write a recursive function that computes the number of ways of distinct k-element subsets of a set with n elements. In other words, "n choose k".