Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion functional-lib/data/functor.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
([c:sequence? (define map c:map)]))

(define/renamed map (variadic-map f . args)
(if (c:sequence? (first args))
(if (and (c:sequence? (first args))
(not (empty? (rest args))))
(apply c:map f args)
(apply map f args)))
32 changes: 30 additions & 2 deletions functional-test/tests/data/functor.rkt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#lang racket/base

(require (except-in data/collection map)
(prefix-in b: racket/base)
data/functor
racket/generic
rackunit
rackunit/spec)

Expand All @@ -15,7 +17,33 @@
(describe "map"
(it "applies a function to the values inside a context"
(check-equal? (map add1 (identity 25))
(identity 26)))
(identity 26))
(check-equal? (sequence->list (map add1 (list 1 2 3)))
(list 2 3 4)))

(it "works like zip when applied to sequences"
(check-equal? (sequence->list (map + '(1 2 3) '(10 20 30))) '(11 22 33)))))
(check-equal? (sequence->list (map + '(1 2 3) '(10 20 30))) '(11 22 33)))

(it "uses a functor map specification, if available, when applied to a sequence"
(struct bag (items)
#:transparent

#:methods gen:functor
[(define (map f x)
;; exclude numbers too big to fit in the bag
(bag (b:filter (lambda (v)
(< v 10))
(b:map f (bag-items x)))))]

#:methods gen:sequence
[(define/generic -empty? empty?)
(define/generic -first first)
(define/generic -rest rest)
(define (empty? x)
(-empty? (bag-items x)))
(define (first x)
(-first (bag-items x)))
(define (rest x)
(bag (-rest (bag-items x))))])

(check-equal? (map add1 (bag (list 7 8 9))) (bag (list 8 9))))))