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
-- When your code grows too big for a line: | |
separateParens = foldr (\ head tail -> if head == '(' then head : ' ' : tail else if head == ')' then ' ' : head : tail else head : tail) [] | |
-- Your natural instinct is to add new lines and comments: | |
separateParens = | |
foldr (\ head tail -> | |
if head == '(' | |
then head : ' ' : tail -- inserts space after left parens |
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 unfold | |
"Next and done? are functions that operate on a seed. next should | |
return a pair, [value new-seed]; the value half of the pair is | |
inserted into the resulting list, while the new-seed is used to | |
continue unfolding. Notably, the value is never passed as an | |
argument to either next or done?." | |
[next done? seed] | |
((fn unfold* | |
[seed] | |
(lazy-seq |
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 enlive->hiccup [node] | |
(if-not (map? node) | |
node | |
(let [{:keys [tag attrs content]} node | |
children (map enlive->hiccup content)] | |
(if (empty? attrs) | |
(vec (cons tag children)) | |
(vec (cons tag (cons attrs children))))))) | |
(defn hiccup->enlive [node] |
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 with-timeout [thunk] | |
(let [task (FutureTask. thunk) | |
thr (Thread. task)] | |
(try | |
(.start thr) | |
(.get task 10 TimeUnit/SECONDS) | |
(catch TimeoutException e | |
(.cancel task true) | |
(.stop thr (Exception. e)) | |
nil)))) |
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 libpsm.string | |
(:require [clojure.string :as str])) | |
(let [triml-with-pipe (comp #(str/replace-first % "|" "") | |
str/triml)] | |
(defn mstr* | |
"Multiline string with explicit left margin." | |
[s] | |
(str/join "\n" (map triml-with-pipe | |
(str/split-lines s))))) |
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
; Copyright (c) Rich Hickey. All rights reserved. | |
; The use and distribution terms for this software are covered by the | |
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | |
; which can be found in the file epl-v10.html at the root of this distribution. | |
; By using this software in any fashion, you are agreeing to be bound by | |
; the terms of this license. | |
; You must not remove this notice, or any other, from this software. | |
(set! *warn-on-reflection* true) |