I've been playing with some of the interfaces in data/collection and I'm loving it so far. Having generic interfaces to different Racket collections like lists, streams, and sequences, is really handy -- especially given the diversity of interfaces to such types otherwise (list-*, vector-*, string-*, stream-*, sequence-*, ... !).
But do these interfaces play well with the built-in sequences in Racket? Specifically, I'm running into this error:
(require data/collection)
(take 10 (in-cycle '(1 2 3)))
=>
; take: contract violation
; expected: sequence?
; given: #<sequence>
; in: the 2nd argument of
; (-> natural? sequence? sequence?)
; contract from:
; <pkgs>/collections-lib/data/collection/sequence.rkt
; blaming: top-level
; (assuming the contract is correct)
; at: <pkgs>/collections-lib/data/collection/sequence.rkt:53.3
The function in-cycle returns a built-in "sequence," while the polymorphic take provided by data/collections expects its own special sequence interface.
In this particular case I could manually define a stream to replace the built-in in-cycle, something like:
(define (in-cycle coll [i 0])
(stream-cons (nth coll (modulo i (length coll)))
(in-cycle coll (add1 i))))
... which works, but there are an awful lot of built-in sequences defined so I'm wondering if there's a better, perhaps standard/recommended way to handle this. That is, can we take advantage of all the built-in sequences in terms of the sequences defined in data/collection, the same way the latter wraps other existing sequences like lists and streams?