This file contains hidden or 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) | |
;this is the function you want to use | |
(defn lazy-channels [chs] | |
(lazy-seq (cons (let [ [v _] (alts!! chs)] v) (lazy-channels chs)))) | |
;now for the tesging | |
(def chs [ (chan) (chan) (chan) ]) ; the channels can come from anywhere, here we are using three channels for testing |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Caution: Partitioning lazy sequence code freeze | |
(def l [1 2 3 4 5]) | |
;create a simple lazy sequence function testing only | |
;(rdr l) returns a lazy sequence from l | |
(def rdr (fn reader[x] (cons (first x) (lazy-seq (reader (rest x)))))) | |
;the line below will freeze | |
(doall (partition-all 2 (rdr l)) ) |
This file contains hidden or 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
int ret = new ProcessBuilder("gzip", "-t", "t.txt.gz").inheritIO().start().waitFor() | |
This file contains hidden or 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
(comment | |
This is the easiest and most concise way of calling an external process in Java. The inheritIO methods causes the command to output stdout and errout to the same place as your current process (which most of the times is the console), no need to create mechanisms for reading asynchronously from input streams to get at the information. | |
http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html | |
) | |
(def ret (.waitFor (-> (ProcessBuilder. ["gzip" "-t" "g.txt.gz"]) .inheritIO .start))) | |
This file contains hidden or 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 pseidon.core.ds.ftp | |
(:require [pseidon.core.conf :refer [get-conf get-conf2] ] | |
) | |
(:use clojure.tools.logging | |
)) | |
(import | |
'(org.apache.commons.vfs2 FileContent FileObject FileSystemManager FileSystemOptions VFS Selectors) |
This file contains hidden or 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
package lazyseq; | |
import org.junit.Test; | |
import clojure.lang.AFn; | |
import clojure.lang.ASeq; | |
import clojure.lang.ArraySeq; | |
import clojure.lang.Cons; | |
import clojure.lang.IPersistentMap; | |
import clojure.lang.ISeq; |
NewerOlder