Skip to content

Instantly share code, notes, and snippets.

@fasterthanlime
Created September 23, 2014 20:46
Show Gist options
  • Save fasterthanlime/e87a900d57e4dcba73f7 to your computer and use it in GitHub Desktop.
Save fasterthanlime/e87a900d57e4dcba73f7 to your computer and use it in GitHub Desktop.
(ns conchtest.core
(:require [me.raynes.conch.low-level :as sh] ;; https://github.com/Raynes/conch
[clojure.java.io :as io])) ;; clojurified Java classes
(defn lines
[]
;; ->> is a 'threading operator' - it allows us to write
;; call pipelines in a more natural order.
;; See doc at: http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E
(->>
;; get an infinite sequence: [1 2 3 4 5] etc.
(iterate inc 1)
;; take the 5000 first elements
(take 5000)
;; multiply each element by 11
(map (fn [x] (* x 11)))
;; put all those element on individual lines, in a single string
(reduce (fn [k v] (str k "\n" v)))))
(defn -main
[]
;; cat is just the identity function
(def cat (sh/proc "cat"))
;; grep filters lines that contains "112"
(def grep (sh/proc "grep" "112"))
;; generate 5K lines and feed them to cat
(io/copy (lines) (cat :in))
(.close (cat :in))
;; pipe cat output to grep
(io/copy (cat :out) (grep :in))
(.close (grep :in))
;; display grep output
(sh/stream-to-out grep :out))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment