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
(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) |
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
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) |
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
if ENV["SELENIUM_HEADLESS"] == 'true' | |
require "headless" | |
Before('@selenium') do | |
@headless = Headless.new | |
@headless.start | |
end | |
After('@selenium') do | |
@headless.destroy if @headless.present? |
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 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!"))) |
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
# 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." |
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
;; 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)) |
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
;; 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 |
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
;; 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 |
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
;; 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)) |
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
;; 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)) | |
NewerOlder