liveBook · Manning

Pcalls

As explained in Clojure: The Essential Reference

pcalls is a Clojure function that invokes zero-argument functions in parallel and returns their results as a lazy sequence.

pcalls invokes any number of zero-argument functions in parallel and returns their results as a lazy sequence. Results retain input order, while evaluation uses futures, making pcalls useful for side-effecting parallel transformations. section 9.1.5

Listing 9.5. → Parallel computation, Lazy sequences
(pmap
  ([f coll])
  ([f coll & colls]))

(pcalls [& fns])

(pvalues [& exprs])

Use the compact signature to remember that pcalls accepts zero or more functions, each requiring no arguments. section 9.1.5

Calling independent functions

(pcalls (constantly "Function") #(System/currentTimeMillis) #(println "side-effect")) ;; side-effect ;; ("Function" 1553770187108 nil) The printing function returns nil, and the resulting sequence follows the functions' order. section 9.1.5

Keep these behavioral details in mind:

  • Pass zero or more functions; every function must have arity zero. section 9.1.5
  • Realize the lazy sequence to obtain results; parallel evaluation does not change their sequence order. section 9.1.5

How Clojure: The Essential Reference covers it

How deep it goes
Defines inputs, outputs, laziness, ordering, and futures, but stops short of detailed scheduling or exception behavior.
Examples
Demonstrates constant, timestamp, and printing functions producing an ordered lazy result sequence.
What you can do after
Apply pcalls to run independent zero-argument computations concurrently and consume their ordered results lazily.
How it connects
Connects pcalls to pmap, pvalues, futures, lazy sequences, and parallel evaluation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.