• Clojure
  • 37 lines
  • 92 views
  • 4 forks
  • Pasted by anonymous on July 30, 2012
(defmacro let-parallel [bindings & forms]
  (let [bindings (partition 2 bindings)
        syms (map first bindings)]
    (when-not (= (count (set syms))
                 (count syms))
      (throw (Exception. "let-parallel requires unique binding-forms.")))
    `(let [mappings# (into {} (map-indexed #(identity [%2 %1]) '~syms))
           returns# (atom {})
           cont# (fn ~(vec syms)
                   (do ~@forms))
           ~'sub-cont (fn [sym# result#]
                        (swap! returns# assoc (get mappings# sym#) result#)
                        (when (= (count @returns#) (count mappings#))
                          (apply cont#
                                 (vals (into (sorted-set) @returns#)))))]
       ~@(map (fn [[sym expr]]
                `(~expr (partial ~'sub-cont '~sym)))
              bindings)
       nil)))

;; java.lang.IllegalArgumentException: No method in multimethod 'emit-constant' for dispatch value: class clojure.lang.LazySeq


;; usage
;;
;; (let-parallel [a (fn [c]
;;                    (js/setTimeout (fn []
;;                                     (.log js/console "A")
;;                                     (c 1))
;;                                   1000))

;;                b (fn [c]
;;                    (js/setTimeout (fn []
;;                                     (.log js/console "B")
;;                                     (c 1))
;;                                   2000))]
;;               (.log js/console (str "END" a b)))

Did you know? CLOSE

  • There are keyboard shortcuts!
    • When Creating A Paste
      • ALT+P Toggle Private
      • CTRL+Enter Create Paste
      • ALT+W Toggle word wrap
    • When Viewing A Paste
      • ALT+G Go to a line
      • ALT+CTRL+E Edit the paste
      • ALT+R Show the raw code
  • There are URL options!
    • When Creating A Paste
      • ?lang=Javascript to default to javascript
    • When Viewing A Paste
      • #L-N Jump to line number N
?