- Introduction
- Day-To-Day Basics
- Equipment
- Appendix A: Preferences
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
#!/usr/bin/env ruby | |
require 'set' | |
class Monitor | |
@@max_procs = 3 | |
def initialize(worker_class) | |
@worker = worker_class.new | |
@interrupted = false | |
@threads = [] |
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
% Returns [2,4,6,8,10] | |
% Yes, you could do it with a list comprehension. It's just an example! | |
lists:map([1,2,3,4,5]) do |N| | |
N * 2 | |
end. | |
% Convert an IP address to an integer | |
lists:foldl(0, [127,0,0,1]) do |Octet, Acc| | |
Acc bsl 8 bor Octet | |
end. |
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
#!/usr/bin/env ruby | |
require 'fiber' | |
# This state machine corresponds to the regular expression /ab*(c|d)/ | |
@start = Fiber.new do | |
loop do | |
case (@string.next rescue nil) | |
when 'a' then @state1.transfer |
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
(ns tetris.core | |
(:import (java.awt Color Dimension BorderLayout) | |
(javax.swing JPanel JFrame JOptionPane JButton JLabel) | |
(java.awt.event KeyListener)) | |
(:use clojure.contrib.import-static deflayout.core | |
clojure.contrib.swing-utils) | |
(:gen-class)) | |
(import-static java.awt.event.KeyEvent VK_LEFT VK_RIGHT VK_DOWN VK_UP VK_SPACE) |
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
type exp = | |
IntExp of int | |
| Builtin of (exp -> env -> exp) | |
| VarExp of string | |
| FunExp of string * exp | |
| LetExp of string * exp * exp | |
| IfExp of exp * exp * exp | |
| AppExp of exp * exp | |
and | |
env = (string * exp) list |
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
(* translated from http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html *) | |
(* implement trampolining *) | |
type 'a bounce = Done of 'a | Call of (unit -> 'a bounce) | |
let rec trampoline = function | |
| Call thunk -> trampoline (thunk()) | |
| Done x -> x | |
(* define some functions which use them *) |
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 Object | |
def _(name, *partial_args) | |
Proc.new do |*new_args| | |
send name, *partial_args.map {|arg| arg == :_ ? new_args.shift : arg} | |
end | |
end | |
end | |
# Practical examples: | |
[1,2,3].each &_(:puts, :_) |
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
(ns resource | |
"Automatic resource cleanup." | |
(:import (java.lang.ref ReferenceQueue PhantomReference))) | |
(def ^:private queue (ReferenceQueue.)) | |
(def ^:private cleanup-fns {}) | |
(defn resource | |
"Returns a reference to x. At some point after the reference is |
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
/** | |
* |@| is a helper function that helps you accumulate applicative functors. It gives you an ApplicativeBuilder (it's part of | |
* the implementation though) through which you accumulate your applicatives just as you would using the Builder pattern of | |
* the GoF in the OO world. Once you have all the applicatives you can pass it a function that will be applied to all the | |
* values that you have accumulated so far. e.g. | |
*/ | |
scala> (1.some |@| 2.some) apply {_ + _} | |
res1: Option[Int] = Some(3) |