Skip to content

Instantly share code, notes, and snippets.

@russolsen
russolsen / for_calls_each.rb
Created Oct 3, 2016
Demo that in Ruby the for expression is just sugar around a call to the each method.
View for_calls_each.rb
# Demo that for calls each
class ThreeOf
def initialize(value)
@value = value
end
def each(&block)
block.call(@value)
block.call(@value)
View gist:56a0750da28216e18042
require 'oj'
require 'stringio'
require 'json'
class Handler
def hash_start
{}
end
def hash_set(h,k,v)
@russolsen
russolsen / cloforth_repl
Created Sep 3, 2013
The key function in cloforth
View cloforth_repl
(defn repl [env]
(let [dictionary (:dictionary env)]
(if (:quit env)
env
(let [r (:in env)
compiled (comp/compile-statement r dictionary)]
(if (and (coll? compiled) (empty? compiled))
env
(let [result (exec/execute-program env compiled) ;; Get the lazy sequence of states
new-env (last result)] ;; ... and then the last one
@russolsen
russolsen / lisp_repl.rb
Created Jan 26, 2012
And to finish the thought, a REPL using Fogus' lithp.rb and my own s-expression parser.
View lisp_repl.rb
require 'lithp'
require 'lisp_parser'
lisp = Lisp.new
print "> "
while not $stdin.eof?
line = readline
s_expression = SExpressionParser.new(line).parse
p lisp.eval(s_expression)
@russolsen
russolsen / lisp_parser.rb
Created Jan 26, 2012
An S expression (i.e. LISP) parser in 32 lines of Ruby.
View lisp_parser.rb
class SExpressionParser
def initialize(expression)
@tokens = expression.scan /[()]|\w+|".*?"|'.*?'/
end
def peek
@tokens.first
end
def next_token