Skip to content

Instantly share code, notes, and snippets.

@clojj
clojj / chan.d
Last active August 29, 2015 14:07 — forked from rjmcguire/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 August 29, 2015 14:07
combinating ppm images
(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")))))
(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 January 14, 2015 21:27
retrieves my starred repos from Github v3 API and stores them in HSQL
@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 January 23, 2015 23:45
github v3 api with tentacles
(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 January 25, 2015 00:03
sort starred repos by commit-count in last n days
(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 February 13, 2015 10:05
Review changes in **/pom.xml before merging release- or feature-branch
find . -name "pom.xml" | xargs git diff master..some-release-or-feature/foo-bar --
@clojj
clojj / core.clj
Created February 23, 2015 12:50
peek into transducing
(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 March 1, 2015 22:41
using transducers.. needs clojure 1.7.0
(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 September 3, 2023 09:25
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
(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"))