Skip to content

Instantly share code, notes, and snippets.

View ponzao's full-sized avatar

Vesa Marttila ponzao

  • Helsinki, Finland
View GitHub Profile
(require 'clojure.set)
(def connections
["3-4"
"4-9"
"8-0"
"2-3"
"5-6"
"2-9"
"5-9"
(def dictionary
(with-open [rdr (reader "/usr/share/dict/words")]
(conj (set (line-seq rdr)) "clojure")))
(defn correctly-spelled?
[sentence]
(->> (.toLowerCase sentence)
(re-seq #"\w+")
(every? dictionary)))
;; Gathers values up to (and including) the one for
;; which the predicate fails.
(defn take-to
[pred coll]
(when-let [s (seq coll)]
(let [[x & xs] s]
(if-not (pred x)
(list x)
(lazy-cat (cons x (take-to pred xs)))))))
class C { def b = "b" }
trait T extends C { override def b = "trait b" }
val c = new C with T
c.b // => java.lang.String = trait b
(defn- in-transaction [f]
(try
(do (println "starting...")
(let [rv (f)]
(println "commiting...")
rv))
(catch Exception e
(println "rollbacking..."))))
(defmacro transactional [expr]
@ponzao
ponzao / index_of_any.clj
Created March 24, 2011 10:18
Explicit looping solution for the StringUtils.indexOfAny -problem.
(defn index-of-any [coll pred]
(loop [index 0
items coll]
(when (not (empty? items))
(if (pred (first items))
index
(recur (inc index) (rest items))))))
(index-of-any "foobar" #{\c \d \b}) ; -> 3
@ponzao
ponzao / LinkBuilder.scala
Created March 14, 2011 19:18
Inspired by https://gist.github.com/869408. Forking didn't seem to work :(
import scala.collection.mutable.MultiMap
import java.net.URLEncoder
import scala.collection.mutable.{Set => MSet, HashMap, MultiMap}
class LinkBuilder(path: String) {
val attributes = new HashMap[String, MSet[String]] with MultiMap[String, String]
override def toString(): String
= path +
"?" +
@ponzao
ponzao / rss-reader.clj
Created February 20, 2011 23:06
An RSS reader ported from Scala http://www.virtuousprogrammer.com/?p=159.
(ns rss-reader
(:import [java.io FileReader
BufferedReader])
(:require [clojure.xml :as xml]
[clojure.zip :as zip])
(:use [clojure.contrib.zip-filter.xml]
[hiccup.core]))
(defn print-feeds [feeds]
(doseq [[channel articles] feeds]
type ID = Long
type Code = String
class User(val id: ID,
val name: String,
val friends: List[User])
class UserDto(val code: Code,
val name: String,
val friends: List[UserDto]) {