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
(def column-n (fn [n sep] (fn [record] (let [s (str record) arr (clojure.string/split s sep)] (arr n))))) | |
(defn file-freq [file column sep] | |
(with-open [ rdr (clojure.java.io/reader file) ] | |
(-> (column-n column sep) (map (line-seq rdr)) frequencies) | |
) | |
) |
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 buffered-select [f-select init-pos] | |
"Creates a lazy sequence of messages for this datasource" | |
(letfn [ | |
(m-seq [buff pos] | |
(if-let [buff2 (if (empty? buff) (f-select pos) buff)] | |
(cons (first buff2) (lazy-seq (m-seq (rest buff2) (inc pos) ))) | |
) | |
) | |
] | |
(m-seq nil init-pos) |
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
;Print "Hi" every 2 seconds: | |
(defn t [] | |
(go (loop [] (<! (timeout 2000)) (prn "hi") (recur)))) | |
;or as a macro |
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
;for testing purposes lets simulate a connection that will return nil or 1 | |
(defn connect-and-get [] (rand-nth [nil 1 1 1 nil 1 nil 1 nil])) | |
(def select-data (repeatedly connect-and-get)) | |
(defn sleep-while-nil [ts s] | |
(filter (complement nil?) (map (fn [x] (if-not x (Thread/sleep ts)) x) s))) | |
(take 10 (sleep-while-nil 100 select-data)) |
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
(use 'clojure.core.async) | |
(defn offer [ch msg timeout-ms] | |
"Blocks till the message can be placed on the queue and returns true otherwise returns false" | |
(not | |
(nil? | |
(first (alts!! [(go (>! ch msg) msg) (timeout timeout-ms)]))))) | |
;;lets test |
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
(require '[clojure.core.async :refer [go alts! >! <! >!! <!! chan timeout]]) | |
(defn buffered-chan | |
"Reads from ch-source and if either timeout or the buffer-count has been | |
read the result it sent to the channel thats returned from this function" | |
([ch-source buffer-count timeout-ms] | |
(buffered-chan ch-source buffer-count timeout-ms 1)) | |
([ch-source buffer-count timeout-ms buffer-or-n] | |
(let [ch-target (chan buffer-or-n)] | |
(go |
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
(require '[clojure.core.async :refer [chan go >! <! >!! <!!]]) | |
(require '[clojure.core.async :as async]) | |
(defn chan-bridge | |
([ch-source map-f ch-target] | |
"map map-f onto ch-source and copy the result to ch-target" | |
(chan-bridge (async/map map-f [ch-source]) ch-target)) | |
([ch-source ch-target] | |
"in a loop read from ch-source and write to ch-target | |
this function returns inmediately and returns the ch-target" |
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
for f in ./*.mkv; do | |
echo "convert $f" | |
ffmpeg -i "$f" -f avi -c:v mpeg4 -b:v 4000k -c:a libmp3lame -b:a 320k "${f%.*}.avi" | |
done |
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
from mymem import cache_set, cache_get | |
######################## | |
#### private functions | |
def chunk_seq(file_name, chunk_size): | |
''' | |
Create a lazy sequence for bytes each up to chunk_size (default 1048576) | |
''' |
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 | |
^{:doc "Concurrent keyed pool implementation using ConcurrentHashMap and ConcurrentLinkedQueue"} | |
kafka-clj.pool-impl | |
(:use criterium.core) | |
(:import (java.util.concurrent ConcurrentLinkedQueue ConcurrentHashMap Semaphore ExecutorService Executors TimeUnit))) | |
;; add [criterium "0.4.4"] to you're project.clj file | |
;; then use run-test-cc and run-test-a | |
;; | |
;; Results for both ConcurrentLinkedQueue + Semaphore and for atom + vector access is the same |