Kajihiro Kazunori
kichijojirb #3
(let [text "hello, world"]
(println text))
;;; before 1.7
(map inc (range 5))
; => (1 2 3 4 5)
(filter odd? (range 5))
; => (1 3)
(take 3 (range 5))
; => (0 1 2)
;;; from 1.7
(def mapping (map inc))
(sequence mapping (range 5))
; => (1 2 3 4 5)
(def filtering (filter odd?))
(sequence filtering (range 5))
; => (1 3)
(def taking (take 3))
(sequence taking (range 5))
; => (0 1 2)
(def mapping (map inc))
(sequence mapping (range 5))
; => (1 2 3 4 5)
(transduce mapping + 0 (range 5)
; => 15
;; Transducers compose with ordinary function composition
(def xform (comp (filter odd?) (map inc)))
(sequence xform (range 5))
; => (2 4)
(transduce xform + 0 (range 5))
; => 6
More examples
(map inc (range 5))
; => (1 2 3 4 5)
(map inc (range 5))
; => (1 2 3 4 5)
output form
mapping function
input
(def mapping (map inc)) ; mapping transducer
; `sequence` is a transducible function
(sequence mapping (range 5)
; => (1 2 3 4 5)
; `transduce` is also a transducible function
(transduce mapping + 0 (range 5))
; => 15
コレクションの操作をTransducer と Transducible function に分割する
Transducers are independent from the context of their input and output sources [...]. Because transducers are decoupled from input or output sources, they can be used in many different processes - collections, streams, channels, observables, etc.
gem install transducers
require 'transducers'
T = Transducers
T.transduce(T.compose(T.map(:succ), T.filter(:even?)), :<<, [], 0..9)
# => [2, 4, 6, 8, 10]
require 'transducers'
T = Transducers
T.transduce(T.compose(T.map(:succ), T.filter(:even?)), :<<, [], 0..9)
# => [2, 4, 6, 8, 10]