Rebellion <https://docs.racket-lang.org/rebellion/index.html> now provides
the rebellion/streaming/transducer
<https://docs.racket-lang.org/rebellion/Transducers.html> library for
processing streams of data using transducers.

A *transducer* is a kind of sequence transformation that comes with
guarantees about memory usage and about how much of the sequence is
consumed in between each produced output. Transducers are first class
objects that chain together fluently, without any macros involved. For
example, this code defines a function that finds and reports all of the
lines in a string that are longer than 80 characters and it uses *no macros
at all*:


(require rebellion/streaming/reducer
         rebellion/streaming/transducer)

(define (report-all-long-lines str)
  (transduce str
             (batching into-line)
             enumerating
             (filtering long-line?)
             #:into (into-for-each report-long-line)))

(define (long-line? e)
  (define line (enumerated-element e))
  (> (string-length line) 80))

(define (report-long-line e)
  (define line (enumerated-element e))
  (define number (enumerated-position e))
  (printf "Line ~a is longer than 80 characters: ~a\n" number line))


You may find transducers more useful than for-style loops in the following
cases:

   - The for/xxx form you need doesn't exist
   - You want to sort
   
<https://docs.racket-lang.org/rebellion/Transducers.html#%28def._%28%28lib._rebellion%2Fstreaming%2Ftransducer..rkt%29._sorting%29%29>
   elements or search for the N largest or N smallest elements
   - You're reading data from external sources and want control over when
   resources are acquired and released
   - You're processing a stream that's too large to fit into memory
   - You care a lot about processing your streams in only one pass
   - You want to combine elements into batches
   
<https://docs.racket-lang.org/rebellion/Transducers.html#%28def._%28%28lib._rebellion%2Fstreaming%2Ftransducer..rkt%29._batching%29%29>
   or group
   
<https://docs.racket-lang.org/rebellion/Entries.html#%28def._%28%28lib._rebellion%2Fcollection%2Fentry..rkt%29._grouping%29%29>
   them by key
   - You want to remove duplicate
   
<https://docs.racket-lang.org/rebellion/Transducers.html#%28def._%28%28lib._rebellion%2Fstreaming%2Ftransducer..rkt%29._deduplicating%29%29>
   elements
   - You prefer writing sequence transformations in a point-free style
   - You already like reducers
   <https://docs.racket-lang.org/rebellion/Reducers.html>
   - You've got a rebellious streak

For some real-world examples of transducers in action, I cannot thank Sam
Phillips enough for putting together this gist
<https://gist.github.com/samdphillips/abfd8b477d5963e929ec4449815b0a38>
containing scripts he wrote for his day job using transducers.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAAXAoJUbF3Gog2az44kvDYq7rxrqdefMUJYnSceY_utinTSjxQ%40mail.gmail.com.

Reply via email to