Skip to content

Instantly share code, notes, and snippets.

;; answer from unlogic to 4clojure.com problem 28
(defn flat [node]
(reduce #(concat % (if (coll? %2) (flat %2) [%2]))
()
node))
def ds1 = [1, 2, [3, 4, [5, "a"]]];
def isSequential(x) {
x instanceof Collection
}
def mapcat(f, coll) {
coll.inject([]) { x, y ->
def res = f(y)
if (isSequential(res)) {
@quux00
quux00 / stdin.clj
Created October 2, 2012 02:01
Example of how to read from STDIN in a Clojure program
(ns example.stdin)
(defn -main
"Read from STDIN"
[& args]
(println "Enter text:")
(loop [input (read-line)]
(when-not (= ":done" input)
(println (str "You entered: >>" input "<<"))
@quux00
quux00 / stdin-acc.clj
Created October 2, 2012 02:31
Example of how to read from STDIN in a Clojure program and accumulate the entries in a vector.
(ns example.stdin)
(defn do-something-cool [v]
(println v))
(defn -main
"Read from STDIN"
[& args]
(println "Enter text:")
(defn distance [[x y]]
(+ (Math/abs x) (Math/abs y)))
(defn add-point [m dist v]
(let [m2 (if (m dist)
(update-in m [dist] conj v)
(assoc m dist [v]))]
(let [m3 (if (> dist (:hd m2))
(merge m2 {:hd dist})
m2)]
(defn dist-then-first [pt1 pt2]
(let [dist1 (distance pt1)
dist2 (distance pt2)]
(if (= dist1 dist2)
(> (first pt1) (first pt2))
(> dist1 dist2))))
(defn mk-sorted-sift-fn [max-size]
(fn [sst pt]
(if (< (count sst) max-size)
@quux00
quux00 / gist:4299232
Created December 15, 2012 21:08
Leftist Priority Queue Heap in Clojure
(ns thornydev.lib.leftist-heap)
;; Implementation of a "leftist priority queue" on an immutable
;; heap data structure
;; This code was (manually) transpiled into Clojure from
;; the Scheme version here:
;; http://programmingpraxis.com/2009/05/05/priority-queues/2/
;; ---[ core lib fns ]--- ;;
(ns thornydev.top100.heap
(:require [thornydev.lib.leftist-heap
:refer [pq-first pq-rest pq-insert pq-empty]]))
(defn distance [[x y]]
(+ (java.lang.Math/abs x) (java.lang.Math/abs y)))
(defn dist-lt? [pt1 pt2]
(< (distance pt2) (distance pt1)))
@quux00
quux00 / boring-generators.go
Last active December 10, 2015 11:39
Simple example of Go channels from Rob Pike's 2012 Google IO presentation: http://www.youtube.com/watch?v=f6kdp27TYZs&feature=youtu.be
package main;
import (
"fmt"
"math/rand"
"time"
"os"
)
// Channels-driven concurrency with Go
@quux00
quux00 / go-lightly.util.clj
Last active December 10, 2015 11:58
Part of the go-lightly utility namespace showing the go and go& macros.
(ns thornydev.go-lightly
(:refer-clojure :exclude [peek take])
(:import (java.io Closeable)
(java.util ArrayList)
(java.util.concurrent LinkedTransferQueue TimeUnit
LinkedBlockingQueue TimeoutException)))
;; ---[ go routines ]--- ;;
(def inventory (atom []))