This file contains 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(euler). | |
-export([euler1/0]). | |
euler1() -> | |
lists:sum([N|| N <- lists:seq(1, 999), ((N rem 3) == 0) or ((N rem 5) == 0) ]). |
This file contains 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 screen-scraping | |
(:use [clojure.contrib.duck-streams :only [reader]] | |
[net.cgrand.enlive-html :only [html-resource select text]])) | |
(def url-to-scrape "http://www.domain.com/somewhere/index.php") | |
;; finds the text of the element with id="someId" | |
(defn scrape [] | |
(let [html (-> url-to-scrape reader html-resource)] | |
(-> html (select [:#someid]) first text))) |
This file contains 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- fizz-buzz-step [n] | |
(let [fizzing? (= 0 (mod n 3)) | |
buzzing? (= 0 (mod n 5))] | |
(if fizzing? (println "Fizz")) | |
(if buzzing? (println "Buzz")) | |
(if (not (or fizzing? buzzing?)) | |
(println n)))) | |
(defn fizz-buzz [] | |
(doall (map fizz-buzz-step (range 0 100)))) |
This file contains 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
;; -*- indent-tabs-mode: nil -*- | |
(ns midje.midje-forms.translating | |
(:use clojure.contrib.def | |
[clojure.contrib.seq :only [separate]] | |
[clojure.contrib.str-utils :only [str-join]] | |
midje.metaconstants | |
[midje.semi-sweet :only [all-arrows]] | |
[midje.util thread-safe-var-nesting wrapping form-utils laziness form-utils] | |
[midje.util.file-position :only [arrow-line-number]] |
This file contains 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 playmatch | |
(:use [clojure.core.match.core :only (match)])) | |
(defn len [coll] | |
"calc the length of a sequence" | |
(match [coll] | |
[[]] 0 | |
[[f & r]] (inc (len r)))) |
This file contains 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
case class Writer(value: Int, diary: String) { | |
def flatMap(f: Int => Writer) = { | |
f(value) match { | |
case Writer(result, d) => Writer(result, diary + d) | |
} | |
} | |
def map(f: Int => Int) = Writer(f(value), diary) | |
} | |
// This enables us to say: | |
new Writer(2, "").flatMap { new Writer(3, "three") } |
This file contains 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
case class Writer[A](value: A, diary: String) = { | |
def flatMap[B](f: A => Writer[B]) = { | |
f(value) match { | |
case Writer(result, d) => Writer(result, diary + d) | |
} | |
} | |
def map[B](f: A => B]) = Writer(f(value), diary) | |
} |
This file contains 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
case class Writer[A, D](value: A, diary: D)(implicit m: Monoid[D]) { | |
def flatMap[B](f: A => Writer2[B, D]) = f(value) match { | |
case Writer(result, d) => Writer(result, m.append(diary, d) | |
} | |
def map[B](f: A => B) = Writer[B, D](f(value), diary) | |
} | |
trait Monoid[T] { | |
def zero: T | |
def append(a: T, b: T): T | |
} |
This file contains 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
Writer(5, "").flatMap { num => Writer(num + 3, "added three") | |
.flatMap { Writer(_ * 5, "times five")} | |
// => Writer(40, "added threetimes 5") |
This file contains 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
object Monoid { | |
implicit val StringMonoid = new Monoid[String] { | |
override def zero = "" | |
override def append(s1: String, s2: String) = { | |
val delimiter = if (s1 == zero || s2 == zero) "" | |
else ", " | |
s1 + delimiter + s2 | |
} | |
} | |
} |
OlderNewer