Skip to content

Instantly share code, notes, and snippets.

@bmabey
bmabey / pi.clj
Created August 11, 2011 17:37 — forked from davidbody/pi.clj
Calculate Pi in Clojure
(defn calculate-pi
"Calculates Pi using the approximation 4 * (1 - 1/3 + 1/5 - 1/7 + ...)"
[iterations]
(let [odd-numbers (filter odd? (iterate inc 1))]
(* 4.0
(apply + (map / (cycle [1 -1]) (take iterations odd-numbers))))))
(println "calculated pi =" (calculate-pi 100000))
(println "Math/PI =" Math/PI)
module LazyInit
class Delay # < BlankSlate or similar so other method calls aren't answered by Delay
def initialize(obj, var_name, &block)
@parent_obj, @var_name, @block = obj, var_name, block
end
def method_missing(method, *args, &block)
realized_object = @block.call
@parent_obj.instance_variable_set(@var_name, realized_object)
realized_object.send(method, *args, &block)
@bmabey
bmabey / env.rb
Created August 27, 2010 19:59 — forked from albertoperdomo/env.rb
if ENV["SELENIUM_HEADLESS"] == 'true'
require "headless"
Before('@selenium') do
@headless = Headless.new
@headless.start
end
After('@selenium') do
@headless.destroy if @headless.present?
(ns memory-leak
(:use [lt.map-pipeline]
[clojure.contrib.seq-utils :only [fill-queue]]))
(defonce quit? (atom false))
;; Doesn't leak
@(future (let [dates (repeatedly (fn []
(java.lang.Thread/sleep 1)
(when @quit? (throw (Exception. "quitting!")))
# In a clojure REPL you can get information about a function like so:
# user=> (doc identity)
# -------------------------
# clojure.core/identity
# ([x])
# Returns its argument.
# nil
# user=> (source identity)
# (defn identity
# "Returns its argument."
@bmabey
bmabey / ex1_8.scm
Created September 23, 2009 05:01 — forked from jimweirich/gist:189270
SICP Exercise 1.8
;; Exercise 1.8. Newton's method for cube roots is based on the fact that if y is an approximation to the
;; cube root of x, then a better approximation is given by the value
;; ((x / y^2) + 2y) / 3
;; Use this formula to implement a cube-root procedure analogous to the square-root procedure. (In
;; section 1.3.4 we will see how to implement Newton's method in general as an abstraction of these
;; square-root and cube-root procedures.)
; square and within-delta? seem reasonably helpful/general so I'm not boxing them in to cbrt
(define (square x)
(* x x))
@bmabey
bmabey / ex1_7.scm
Created September 23, 2009 02:32 — forked from jimweirich/gist:189261
SICP Exercise 1.7
;; Exercise 1.7.
;; The good-enough? test used in computing square roots will not be very effective for
;; finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost
;; always performed with limited precision. This makes our test inadequate for very large numbers. Explain
;; these statements, with examples showing how the test fails for small and large numbers. An alternative
;; strategy for implementing good-enough? is to watch how guess changes from one iteration to the
;; next and to stop when the change is a very small fraction of the guess. Design a square-root procedure
;; that uses this kind of end test. Does this work better for small and large numbers?
; The problem with the good-enough? procedure for small numbers is that there radicant is much smaller than
@bmabey
bmabey / gist:189877
Created September 20, 2009 18:44 — forked from jimweirich/gist:189259
SICP Exercise 1.6
;; Exercise 1.6.
;; Alyssa P. Hacker doesn't see why if needs to be provided as a special form. ``Why can't I
;; just define it as an ordinary procedure in terms of cond?'' she asks. Alyssa's friend Eva Lu Ator claims
;; this can indeed be done, and she defines a new version of if:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
;; Eva demonstrates the program for Alyssa:
(new-if (= 2 3) 0 5)
5
@bmabey
bmabey / ex1_5.scm
Created September 19, 2009 22:09 — forked from jimweirich/gist:189248
;; SICP Exercise 1.5
;; Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is
;; using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
;; Then he evaluates the expression
; (test 0 (p))
@bmabey
bmabey / ex1_4.scm
Created September 19, 2009 21:43 — forked from redsquirrel/gist:189193
SICP Exercise 1.4
;; SICP 1.4
;; Exercise 1.4. Observe that our model of evaluation allows for
;; combinations whose operators are compound expressions. Use this
;; observation to describe the behavior of the following procedure:
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))