Skip to main content

Diigo Home
Home/ Groups/ clojure-dev
Stanton Teters

ongoing · Concur.next - Parallel I/O - 0 views

  • Stanton Teters
     
    Conclusion first: It turns out that Clojure's concurrency primitives allow you, with a very moderate amount of uncomplicated code, to take advantage of parallel hardware and outperform really fast software when it doesn't take such advantage.
Stanton Teters

ongoing · Concur.next - References - 1 views

  • Stanton Teters
     
    These, "refs" for short, are one of the three tools offered by Clojure to make concurrency practical and manageable. Herewith a walk-through of code that uses them to accomplish a simple task in a highly concurrent fashion.
Javier Neira

Paste number 90605: example of -> destructuring - 1 views

  • Javier Neira
     
    ;; Here's an example distilled from real-world code:

    (defn p [string]
    (for [mime-exts (.split string ",")
    :let [[mime & exts] (.split mime-exts ";")]]
    [(keyword mime) exts]))


    ;; With -> destructuring, could be written like this:

    (defn p [string]
    (for [(-> (.split ";") [mime & exts]) (.split string ",")]
    [(keyword mime) exts]))


    ;; Either way, it does this:

    user=> (p "text/plain;foo;bar,text/xml;extra")
    ([:text/plain ("foo" "bar")] [:text/xml ("extra")])
Javier Neira

Clojure and me » Are pipe dreams made of promises? - 0 views

  • Javier Neira
     


    (defn pipe
    "Returns a pair: a seq (the read end) and a function (the write end).
    The function can takes either no arguments to close the pipe
    or one argument which is appended to the seq. Read is blocking."
    []
    (let [promises (atom (repeatedly promise))
    p (second @promises)]
    [(lazy-seq @p)
    (fn
    ([] ;close the pipe
    (let [[a] (swap! promises #(vector (second %)))]
    (if a
    (deliver a nil)
    (throw (Exception. "Pipe already closed")))))
    ([x] ;enqueue x
    (let [[a b] (swap! promises next)]
    (if (and a b)
    (do
    (deliver a (cons x (lazy-seq @b)))
    x)
    (throw (Exception. "Pipe already closed"))))))]))

    Beware of not printing the seq while the pipe is still open!

    (let [[q f] (pipe)]
    (future (doseq [x q] (println x)) (println "that's all folks!"))
    (doseq [x (range 10)]
    (f x))
    (f)) ;close the pipe

Javier Neira

Clojars - 0 views

  • Javier Neira
     
    Clojars.org will be a dead easy jar repository for open source Clojure libraries.
Javier Neira

Paste number 90517: doc extend-protocol - 0 views

  • Javier Neira
     
    clojure.core/extend-protocol
    ([p & specs])
    Macro
    Useful when you want to provide several implementations of the same
    protocol all at once. Takes a single protocol and the implementation
    of that protocol for one or more types. Expands into calls to
    extend-type and extend-class:

    (extend-protocol Protocol
    ::AType
    (foo [x] ...)
    (bar [x y] ...)
    ::BType
    (foo [x] ...)
    (bar [x y] ...)
    AClass
    (foo [x] ...)
    (bar [x y] ...)
    nil
    (foo [x] ...)
    (bar [x y] ...))

    expands into:

    (do
    (clojure.core/extend-type ::AType Protocol
    (foo [x] ...)
    (bar [x y] ...))
    (clojure.core/extend-type ::BType Protocol
    (foo [x] ...)
    (bar [x y] ...))
    (clojure.core/extend-class AClass Protocol
    (foo [x] ...)
    (bar [x y] ...))
    (clojure.core/extend-type nil Protocol
    (foo [x] ...)
    (bar [x y] ...)))
Javier Neira

Paste number 87611: hydra (producer/consumers) - 0 views

  • Javier Neira
     
    (defn hydra
    "returns a BlockingQueue, will return a new infinite lazy-seq wrapped in a delay
    evertime it is deref'ed. all items put in the LinkedBlockingQueue will be added to
    all the lazy-seqs producded by deref'ing"
    []
    (let [consumers (atom nil)
    producer (proxy [LinkedBlockingQueue IDeref] []
    (deref []
    (let [x (LinkedBlockingQueue.)]
    (swap! consumers conj x)
    (delay (repeatedly #(.take x))))))]
    (future
    (while true
    (let [x (.take producer)]
    (doseq [y @consumers]
    (.put y x)))))
    producer))
Javier Neira

in which things are mapped, but also reduced - Technomancy - 0 views

  • I think a map/reduce approach is a little more
    idiomatic since it can be done with no explicit state change.
  • The find-widely
    function below takes a filename and a number of agents to work
    with. Agents can be thought of as independent asynchronous workers
    that share a thread pool and keep a single state value.
  • Our line-seq sets up a lazy sequence of lines
    from the file. We map over this sequence together with an infinite
    loop of the agents we construct
    using cycle.
  • ...2 more annotations...
  • merge-with is a special case of reduce
    that assumes it works on a sequence of maps and merges key
    collisions using the provided function, in this
    case +. This gives us a map of page names to hit
    counts.
  • In any case, this is a simple example of how to break up a
    commonplace problem using a classic map/reduce strategy and
    immutable data structures.
1 - 20 of 518 Next › Last »
Showing 20 items per page
Join this group