Idiomatic Clojure way of mimicking Python's yield
You can build this yourself using lazy-seq as you mention or you could use partition
and reduce
to split the problem into phases then thread them together. I'll use the thread-last macro to show each step on it's own:
user> (->> [1, 5, 2, 5, :SENTINEL, 4, 6, 7] ;; start with data
(partition-by #(= :SENTINEL %)) ;; ((1 5 2 5) (:SENTINEL) (4 6 7))
(take-nth 2) ;; ((1 5 2 5) (4 6 7))
(map #(* 2 (reduce + %)))) ;; the map here keeps it lazy
(26 34)
and here it is usin lazy-seq directly:
user> (defn x [items]
(when (seq items)
(lazy-seq (cons (* 2 (reduce + (take-while #(not= :SENTINEL %) items)))
(x (rest (drop-while #(not= :SENTINEL %) items)))))))
#'user/x
user> (x [1, 5, 2, 5, :SENTINEL, 4, 6, 7])
(26 34)
The Tupelo library has a way to do this using lazy-gen
/yield
which mimics a Python generator function:
(ns xyz
(:require [tupelo.core :as t] ))
(def data-1 [1 5 2 5 :SENTINEL 4 6 7] )
(def data-2 [1 5 2 5 :SENTINEL 4 6 7 :SENTINEL] )
(defn yielder [vals]
(t/lazy-gen
(let [state (atom 0)]
(doseq [item vals]
(if (= :SENTINEL item)
(do
(t/yield (* 2 @state))
(reset! state 0))
(swap! state + item))))))
(yielder data-1) => (26)
(yielder data-2) => (26 34)
Note that the original problem description had a bug, since the cumulative state is only output when the :SENTENEL
tag is encountered. The different outputs for data-1
and data-2
illustrate the problem.