Skip to content

Instantly share code, notes, and snippets.

View fogus's full-sized avatar
💭
attempting to learn how to better learn

Fogus fogus

💭
attempting to learn how to better learn
View GitHub Profile
@fogus
fogus / proctest.rb
Created September 14, 2011 12:27 — forked from janv/proctest.rb
Small sample program to illustrate process management in ruby
#!/usr/bin/env ruby
require 'set'
class Monitor
@@max_procs = 3
def initialize(worker_class)
@worker = worker_class.new
@interrupted = false
@threads = []
@fogus
fogus / examples.erl
Created September 14, 2011 17:06 — forked from tarcieri/examples.erl
Funargs: Ruby-like blocks for Erlang
% 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.
#!/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
@fogus
fogus / tetris.clj
Created September 16, 2011 16:05 — forked from alexander-yakushev/tetris.clj
Tetris implementation in Clojure
(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)
@fogus
fogus / interpreter.ml
Created September 17, 2011 23:52 — forked from gus/interpreter.ml
OCaml Interpreter
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
@fogus
fogus / trampoline.ml
Created September 17, 2011 23:52 — forked from rapha/trampoline.ml
Trampolining in OCaml
(* 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 *)
@fogus
fogus / gist:1225651
Created September 18, 2011 22:40 — forked from headius/partial_application.rb
Partial application that works on all Ruby implementations
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, :_)

Zombies! 2 Survivor's Handbook

Table of Contents

  • Introduction
  • Day-To-Day Basics
  • Equipment
  • Appendix A: Preferences
@fogus
fogus / resource.clj
Created September 26, 2011 02:13 — forked from stuartsierra/resource.clj
Automatic Resource Cleanup with PhantomReferences
(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
@fogus
fogus / gist:1262004
Created October 4, 2011 15:56 — forked from debasishg/gist:1261857
a brief rant about |@| in scalaz
/**
* |@| 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)