Skip to content

Instantly share code, notes, and snippets.

View chan.d
import core.sync.mutex : Mutex;
import core.thread : Thread, Fiber;
/**
* chan allows messaging between threads without having to deal with locks, similar to how chan works in golang
*/
shared
class chan(T) {
Mutex lock;
private bool closed_; bool closed() {synchronized (lock) {return closed_;}} void Close() { synchronized(lock) { closed_ = true; } }
@clojj
clojj / gist:646e6b3e002e49d0996f
Last active Aug 29, 2015
combinating ppm images
View gist:646e6b3e002e49d0996f
(defn process []
(let [file-data (IOUtils/toByteArray (io/input-stream "resources/img1.ppm"))
ppm-head (subvec (vec file-data) 0 32)
img-data (partition 3 (subvec (vec file-data) 32))
file-data2 (IOUtils/toByteArray (io/input-stream "resources/img2.ppm"))
img-data2 (partition 3 (subvec (vec file-data2) 32))]
(let [result (map #(map (fn [n] (/ n 2)) (map + %1 %2)) img-data img-data2) ;(r/fold (r/monoid into vector) conj (r/map #(/ (+ % %) 2) img-data img-data2))
result-data (flatten result)
ba (byte-array (concat ppm-head result-data))]
(IOUtils/write ba (io/output-stream "resources/out.ppm")))))
View suspendable_test.clj
(ns clojure.core.suspendable-test
(:require [clojure.core.async :as async]))
(def c (async/chan))
(defn fn1 [ctrl-chan]
(async/go-loop [cmd-old :run cmd-new :run]
(condp = cmd-new
:run (do
(when (not (= cmd-old cmd-new))
@clojj
clojj / persist-starred.groovy
Created Jan 14, 2015
retrieves my starred repos from Github v3 API and stores them in HSQL
View persist-starred.groovy
@GrabResolver(name='gstorm', root='http://dl.bintray.com/kdabir/maven')
@GrabConfig(systemClassLoader = true) @Grab('gstorm:gstorm:0.6')
import gstorm.*
import groovy.json.*
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
@GrabExclude(group='xerces', module='xercesImpl')
import static groovyx.net.http.ContentType.TEXT
@Grab(group='commons-io', module='commons-io', version='2.3')
@clojj
clojj / core.clj
Created Jan 23, 2015
github v3 api with tentacles
View core.clj
(ns github-api.core
(:gen-class)
(:require [tentacles.repos :as repos])
(:use [clojure.pprint])
(:import (java.time Period LocalDateTime)))
(let [starred (repos/starring "clojj"
{:per-page 4
:accept "application/vnd.github.v3.star+json"})
@clojj
clojj / core.clj
Created Jan 25, 2015
sort starred repos by commit-count in last n days
View core.clj
(ns github-api.core
(:gen-class)
(:require [tentacles.repos :as repos]
[clojure.core.reducers :as r])
(:use [clojure.pprint])
(:import (java.time Period LocalDateTime)))
(defn latest [days token]
(let [starred (repos/starring "clojj"
{:accept "application/vnd.github.v3.star+json" :auth token
@clojj
clojj / gist:2366602e794633a059db
Created Feb 13, 2015
Review changes in **/pom.xml before merging release- or feature-branch
View gist:2366602e794633a059db
find . -name "pom.xml" | xargs git diff master..some-release-or-feature/foo-bar --
@clojj
clojj / core.clj
Created Feb 23, 2015
peek into transducing
View core.clj
(ns transpeek.core
(:gen-class)
(:import (java.util ArrayList)))
(defn map-
([f]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
@clojj
clojj / solution_53.clj
Created Mar 1, 2015
using transducers.. needs clojure 1.7.0
View solution_53.clj
(ns solutions-4clojure.solution-53
(:import (java.util ArrayDeque)))
(defn subseq-by [f]
(fn [rf]
(let [deq (ArrayDeque.)]
(fn
([] (rf))
([result]
@clojj
clojj / progressor.clj
Last active Aug 29, 2015
in response to http://oobaloo.co.uk/multiplexing-work-reliably-and-efficiently-with-state-machines-and-core-dot-async .... so (start) will give you a channel to put initial states on.. or you can comment-in line 70
View progressor.clj
(ns async-state-progressor.progressor
(:require [clojure.core.async :refer [go-loop >! <! thread chan timeout onto-chan]]))
(defn log-state [msg current-state]
(locking *out* (println msg (:state current-state))))
(defn download-report [{:keys [client date] :as state}]
;; snip
(assoc state :file "dummy-file"))