Nicolas Petton

Nicolas Petton

Web developer, Lisper, Smalltalker & Emacs maniac.

stream.el

Lazy sequences in Emacs with stream.el

October 14, 2015

I just pushed to GNU ELPA the first version of stream, a new streaming library for Emacs. Streams are implemented as delayed evaluation of cons cells, and are immutable. In that sense they are similar to streams in the SICP book[1] or to Clojure's lazy sequences.

stream requires Emacs >= 25, as it leverages the extensibility of seq.el: all functions defined in seq will work on streams, meaning that it's possible to consume a stream using seq-take, map and filter it using seq-map and seq-filter, and so forth.

Streams could be created from any sequential input data:

  • sequences, making operation on them lazy
  • a set of 2 forms (first and rest), making it easy to represent infinite sequences
  • buffers (by character)
  • buffers (by line)
  • buffers (by page)
  • IO streams
  • orgmode table cells

The generic stream function currently accepts lists, strings, arrays and buffers as input, but it can be cleanly extended to support pretty much any kind of data.

All operations on streams are lazy (including the functions from seq.el), unless data is actually needed.

Here is an example of an infinite stream of ones:

(defun ones ()
  (stream-cons 1 (ones)))

(ones) ;; => stream

The next example shows an implementation of the Fibonacci numbers implemented as in infinite stream:

(defun fib (a b)
  (stream-cons a (fib b (+ a b))))

(fib 0 1)

The following example returns a stream of the first 50 characters of the current buffer:

(seq-take (stream (current-buffer)) 50) ;; => stream

Because stream implements the seq protocol, it is also possible to use mapping, filtering, etc. functions, you just have to remember that all operations on streams are lazy:

(seq-map #'1+ (seq-take (fib 0 1) 50)) ;; => stream

There are many more things to show about stream, one of them being how to extend seq with new sequence types.

If you use a recent version of Emacs built from the git repository, you can install stream through M-x package-install RET stream.

[1] https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5

comments powered by Disqus