Next: , Previous: , Up: srfi sets-and-bags   [Index]


2.37.7 Mapping and folding

Function: set-map comparator proc set

Apply proc to each element of set in arbitrary order and return a newly allocated set, created as if by (set comparator), which contains the results of the applications. For example:

(set-map string-ci-comparator
         symbol->string
         (set eq-comparator 'foo 'bar 'baz))
≡ (set string-ci-comparator "foo" "bar" "baz")

Note that, when proc defines a mapping that is not 1:1, some of the mapped objects may be equivalent in the sense of comparator’s equality predicate, and in this case duplicate elements are omitted as in the set constructor. For example:

(set-map integer-comparator
         (lambda (x)
           (quotient x 2))
         (set integer-comparator 1 2 3 4 5))
≡ (set integer-comparator 0 1 2)

If the elements are the same in the sense of eqv?, it is unpredictable which one will be preserved in the result.

Function: bag-map comparator proc bag

Apply proc to each element of bag in arbitrary order and return a newly allocated bag, created as if by (bag comparator), which contains the results of the applications. For example:

(bag-map string-ci-comparator
         symbol->string
         (bag eq-comparator 'foo 'bar 'baz))
≡ (bag string-ci-comparator "foo" "bar" "baz")

(list-sort <
  (bag->list
    (bag-map integer-comparator
             (lambda (x)
               (quotient x 2))
             (bag integer-comparator 1 2 3 4 5))))
⇒ (0 1 1 2 2)
Function: set-for-each proc set
Function: bag-for-each proc bag

Apply proc to set or bag in arbitrary order, discarding the returned values. Return unspecified results.

Function: set-fold proc nil set
Function: bag-fold proc nil bag

Invoke proc on each member of set or bag in arbitrary order, passing the result of the previous invocation as a second argument. For the first invocation, nil is used as the second argument. Return the result of the last invocation, or nil if there was no invocation.

(import (vicare) (srfi :113) (srfi :114))

(list-sort <
  (set-fold (lambda (elm nil)
              (cons elm nil))
              '()
              (set fixnum-comparator 1 2 3)))
⇒ (1 2 3)

(list-sort <
  (bag-fold (lambda (elm nil)
              (cons elm nil))
            '()
            (bag fixnum-comparator 1 2 2 3)))
⇒ (1 2 2 3)
Function: set-filter predicate set
Function: bag-filter predicate bag

Return a newly allocated set or bag with the same comparator as set or bag, containing just the elements of set or bag that satisfy predicate.

Function: set-filter! predicate set
Function: bag-filter! predicate bag

Like set-filter and bag-filter but they are permitted to mutate and return set or bag rather than allocating a new set or bag.

Function: set-remove predicate set
Function: bag-remove predicate bag

Return a newly allocated set or bag with the same comparator as set or bag, containing just the elements that do not satisfy predicate.

Function: set-remove! predicate set
Function: bag-remove! predicate bag

Like set-remove and bag-remove but they are permitted to mutate and return set or bag rather than allocating a new set or bag.

Function: set-partition predicate set
Function: bag-partition predicate bag

Return two values:

  1. A newly allocated set or bag, with the same comparator as set or bag, that contains just the elements that do satisfy predicate.
  2. A newly allocated set or bag, with the same comparator as set or bag, that contains just the elements that do not satisfy predicate.

Usage example:

(import (vicare) (srfi :113) (srfi :114))

(receive (in out)
    (set-partition (lambda (elm)
                     (<= 2 elm))
                   (set fixnum-comparator 1 2 2 3))
  (values (set->list in  #t)
          (set->list out #t)))
⇒ (2 3) (1)

(receive (in out)
    (bag-partition (lambda (elm)
                     (<= 2 elm))
                   (bag fixnum-comparator 1 2 2 3))
  (values (bag->list in  #t)
          (bag->list out #t)))
⇒ (2 2 3) (1)
Function: set-partition! predicate set
Function: bag-partition! predicate bag

Like set-partition and bag-partition but they are permitted to mutate and return set or bag rather than allocating a new set or bag.


Next: , Previous: , Up: srfi sets-and-bags   [Index]