{"size": 1725, "ext": "scrbl", "max_stars_repo_path": "collections-doc/scribblings/data/collection/collection/examples.scrbl", "max_stars_repo_name": "wilbowma/racket-collections", "max_stars_repo_stars_event_min_datetime": "2015-12-03T05:07:33.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-21T00:30:28.000Z", "max_issues_repo_path": "collections-doc/scribblings/data/collection/collection/examples.scrbl", "max_issues_repo_name": "wilbowma/racket-collections", "max_issues_repo_issues_event_min_datetime": "2015-12-06T04:13:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T01:39:05.000Z", "max_forks_repo_path": "collections-doc/scribblings/data/collection/collection/examples.scrbl", "max_forks_repo_name": "wilbowma/racket-collections", "max_forks_repo_forks_event_min_datetime": "2015-12-05T20:22:31.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-27T20:27:39.000Z"}
{"max_stars_count": 57, "max_issues_count": 31, "max_forks_count": 11, "avg_line_length": 31.3636363636, "max_line_length": 97, "alphanum_fraction": 0.6260869565}
#lang scribble/manual

@(require
   racket/require
   (for-label (subtract-in
               (combine-in
                racket/base
                racket/list
                racket/set
                racket/dict)
               data/collection)
              (prefix-in base: racket/base)
              data/collection
              racket/generic
              racket/contract
              racket/stream
              racket/string
              racket/match)
   scribble/eval
   "../private/utils.rkt")

@title[#:tag "collections-examples"]{Examples}

Obviously, the primary purpose of generic collections is the ability to operate on values in a
collection-agnostic way. For example, it is possible to map over all kinds of sequences, even
sequences of heterogenous types.

@(coll-interaction
  (sequence->list (map + #(1 2 3) (stream 10 20 30))))

However, more interesting applications of generic collections involve the ability to use lazy
sequences to create infinite streams of values. For example, it is possible to create an infinite
stream of all the Fibonacci numbers:

@(coll-interaction
  (define fibs (stream-cons 1 (stream-cons 1 (map + fibs (rest fibs)))))
  (sequence->list (take 15 fibs)))

Similarly, here is an implementation of the classic
@hyperlink["http://en.wikipedia.org/wiki/Fizz_buzz#Other_uses"]{“fizz buzz” problem} that uses
infinite sequences:

@(coll-interaction
  (define ((divisible-by? x) n)
    (zero? (remainder n x)))
  (define fizzbuzz
    (map
     (match-lambda
       [(? (divisible-by? 15)) 'fizzbuzz]
       [(? (divisible-by?  3)) 'fizz]
       [(? (divisible-by?  5)) 'buzz]
       [n                      n])
     (in-naturals 1)))
  (sequence->list (take 20 fizzbuzz)))
