Skip to content

Instantly share code, notes, and snippets.

View AlexBaranosky's full-sized avatar

AlexBaranosky AlexBaranosky

  • Cisco Secure Malware Analytics (formerly Threat Grid)
  • Massachusetts
  • 03:58 (UTC -04:00)
View GitHub Profile
-module(euler).
-export([euler1/0]).
euler1() ->
lists:sum([N|| N <- lists:seq(1, 999), ((N rem 3) == 0) or ((N rem 5) == 0) ]).
@AlexBaranosky
AlexBaranosky / enlive-screen-scraping.clj
Created January 17, 2011 01:42
Enlive make screen scraping fun :)
(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)))
(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))))
@AlexBaranosky
AlexBaranosky / transform function
Created June 27, 2011 23:05
idea for refactoring of translating.clj
;; -*- 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]]
@AlexBaranosky
AlexBaranosky / defmatch.clj
Created October 3, 2011 01:53
macro to define match-based functions
(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))))
@AlexBaranosky
AlexBaranosky / writerMonad1.scala
Created October 5, 2011 23:58
Writer Monad 0.1
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") }
@AlexBaranosky
AlexBaranosky / gist:1266120
Created October 6, 2011 00:05
A Bit More Generic Writer MOnad
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)
}
@AlexBaranosky
AlexBaranosky / gist:1266126
Created October 6, 2011 00:07
Typeclassed Writer Monad
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
}
Writer(5, "").flatMap { num => Writer(num + 3, "added three")
.flatMap { Writer(_ * 5, "times five")}
// => Writer(40, "added threetimes 5")
@AlexBaranosky
AlexBaranosky / gist:1266134
Created October 6, 2011 00:10
Improved StringMonoid
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
}
}
}