实现Clojure条件/分支传感器

dsg*_*dsg 8 conditional clojure transducer

我正在尝试在Clojure中创建一个条件传感器,如下所示:

(defn if-xf
  "Takes a predicate and two transducers.
   Returns a new transducer that routes the input to one of the transducers
   depending on the result of the predicate."
  [pred a b]
  (fn [rf]
    (let [arf (a rf)
          brf (b rf)]
      (fn
        ([] (rf))
        ([result]
           (rf result))
        ([result input]
           (if (pred input)
             (arf result input)
             (brf result input)))))))
Run Code Online (Sandbox Code Playgroud)

它非常有用,因为它可以让你做这样的事情:

;; multiply odd numbers by 100, square the evens.  
(= [0 100 4 300 16 500 36 700 64 900]
    (sequence
          (if-xf odd? (map #(* % 100)) (map (fn [x] (* x x))))
          (range 10)))
Run Code Online (Sandbox Code Playgroud)

但是,这种条件传感器对于在其1-arity分支中执行清理的传感器不能很好地工作:

;; negs are multiplied by 100, non-negs are partitioned by 2
;; BUT! where did 6 go?
;; expected: [-600 -500 -400 -300 -200 -100 [0 1] [2 3] [4 5] [6]]
;;
(= [-600 -500 -400 -300 -200 -100 [0 1] [2 3] [4 5]]
 (sequence
  (if-xf neg? (map #(* % 100)) (partition-all 2))
  (range -6 7)))
Run Code Online (Sandbox Code Playgroud)

是否有可能if-xf通过清理调整处理传感器情况的定义?

我正在尝试这个,但有奇怪的行为:

(defn if-xf
  "Takes a predicate and two transducers.
   Returns a new transducer that routes the input to one of the transducers
   depending on the result of the predicate."
  [pred a b]
  (fn [rf]
    (let [arf (a rf)
          brf (b rf)]
      (fn
        ([] (rf))
        ([result]
           (arf result) ;; new!
           (brf result) ;; new!
           (rf result))
        ([result input]
           (if (pred input)
             (arf result input)
             (brf result input)))))))
Run Code Online (Sandbox Code Playgroud)

具体来说,冲洗发生在最后:

;; the [0] at the end should appear just before the 100.
(= [[-6 -5] [-4 -3] [-2 -1] 100 200 300 400 500 600 [0]]
      (sequence
       (if-xf pos? (map #(* % 100)) (partition-all 2))
       (range -6 7)))
Run Code Online (Sandbox Code Playgroud)

有没有办法制作这种分支/条件传感器而不将整个输入序列存储在该传感器内的本地状态(即在清理时在1-arity分支中进行所有处理)?

Clo*_*tly 1

这个想法是在每次传感器切换时完成。IMO 这是在不缓冲的情况下执行此操作的唯一方法:

(defn if-xf
  "Takes a predicate and two transducers.
   Returns a new transducer that routes the input to one of the transducers
   depending on the result of the predicate."
  [pred a b]
  (fn [rf]
    (let [arf (volatile! (a rf))
          brf (volatile! (b rf))
          a? (volatile! nil)]
      (fn
        ([] (rf))
        ([result]
         (let [crf (if @a? @arf @brf)]
           (-> result crf rf)))
        ([result input]
         (let [p? (pred input)
               [xrf crf] (if p? [@arf @brf] [@brf @arf])
               switched? (some-> @a? (not= p?))]
           (if switched?
             (-> result crf (xrf input))
             (xrf result input))
           (vreset! a? p?)))))))
(sequence (if-xf pos? (map #(* % 100)) (partition-all 2)) [0 1 0 1 0 0 0 1])
; => ([0] 100 [0] 100 [0 0] [0] 100)
Run Code Online (Sandbox Code Playgroud)