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


2.37.5 Updaters

Function: set-adjoin set element

Return a newly allocated set that uses the same comparator as set and contains all the values of set, and in addition each element unless it is already equal (in the sense of the comparator) to one of the existing or newly added members.

It is an error to add an element for which the type test procedure of the comparator does not return #t.

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

(internal-body
  (define S
    (set fixnum-comparator 1 2 3))
  (define S^
    (set-adjoin S 4))
  (list-sort fx<? (set->list S^)))
⇒ (1 2 3 4)

(internal-body
  (define S
    (set fixnum-comparator 1 2 3))
  (define S^
    (set-adjoin S 2))
  (list-sort fx<? (set->list S^)))
⇒ (1 2 3)
Function: bag-adjoin bag element

Return a newly allocated bag that uses the same comparator as bag and contains all the values of bag, and in addition each element.

It is an error to add an element for which the type test procedure of the comparator does not return #t.

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

(internal-body
  (define B
    (bag fixnum-comparator 1 2 3))
  (define B^
    (bag-adjoin B 4))
  (list-sort fx<? (bag->list B^)))
⇒ (1 2 3 4)

(internal-body
  (define B
    (bag fixnum-comparator 1 2 3))
  (define B^
    (bag-adjoin B 2))
  (list-sort fx<? (bag->list B^)))
⇒ (1 2 2 3)
Function: set-adjoin! set element
Function: bag-adjoin! bag element

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

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

(internal-body
  (define S
    (set fixnum-comparator 1 2 3))
  (set-adjoin! S 4)
  (list-sort fx<? (set->list S)))
⇒ (1 2 3 4)

(internal-body
  (define S
    (set fixnum-comparator 1 2 3))
  (set-adjoin! S 2)
  (list-sort fx<? (set->list S)))
⇒ (1 2 3)

(internal-body
  (define B
    (bag fixnum-comparator 1 2 3))
  (bag-adjoin! B 4)
  (list-sort fx<? (bag->list B)))
⇒ (1 2 3 4)

(internal-body
  (define B
    (bag fixnum-comparator 1 2 3))
  (bag-adjoin! B 2)
  (list-sort fx<? (bag->list B)))
⇒ (1 2 2 3)
Function: set-replace set element

Return a newly allocated set that uses the same comparator as set and contains all the values of set except that: if element is equal (in the sense of set’s comparator) to an existing member of set, then that member is omitted and replaced by element. If there is no such member in set, then set is returned unchanged.

Function: bag-replace bag element

Return a newly allocated bag that uses the same comparator as bag and contains all the values of bag except that: if element is equal (in the sense of bag’s comparator) to an existing member of bag, then that member is omitted and replaced by element. If there is no such member in bag, then bag is returned unchanged.

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

(define B  (bag real-comparator 1 2 2 3))
(define B^ (bag-replace B 2.0))
(list-sort < (bag->list B^)))
⇒ (1 2.0 2.0 3)
Function: set-replace! set element
Function: bag-replace! bag element

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

Function: set-delete set element

Return a newly allocated set containing all the values of set except for any that are equal (in the sense of set’s comparator) to one or more of the elements. Any element that is not equal to some member of set is ignored.

Function: bag-delete bag element

Return a newly allocated bag containing all the values of bag except for any that are equal (in the sense of bag’s comparator) to one or more of the elements. Any element that is not equal to some member of bag is ignored.

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

(internal-body
  (define B  (bag fixnum-comparator 1 2 2 3))
  (define B^ (bag-delete B 2))
  (list-sort < (bag->list B^)))
⇒ (1 2 3)

(internal-body
  (define B  (bag real-comparator 1 2 2 3))
  (define B^ (bag-delete B 2.0))
  (list-sort < (bag->list B^)))
⇒ (1 2 3)
Function: set-delete! set element
Function: bag-delete! bag element

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

Function: set-delete-all set element-list
Function: bag-delete-all bag element-list
Function: set-delete-all! set element-list
Function: bag-delete-all! bag element-list

Like set-delete, bag-delete, set-delete! and bag-delete! but accept a single argument which is a list of elements to be deleted.

Function: set-search! set element failure success
Function: bag-search! bag element failure success

The set or bag is searched for element.

The effects of the continuations are as follows (where obj is any Scheme object):

In all cases, two values are returned: the possibly updated set or bag and obj.

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

;;success and update
(internal-body
  (define S  (set real-comparator 1 2 3))
  (receive (S^ obj)
      (set-search! S 2.0
                   ;;failure proc
                   (lambda (insert ignore)
                     (error #f "wrong"))
                   ;;success proc
                   (lambda (true-element update remove)
                     (update 9 'flag)))
    (list-sort < (set->list S^))        ⇒ (1 3 9)
    obj))                               ⇒ flag

;;success and remove
(internal-body
  (define S  (set real-comparator 1 2 3))
  (receive (S^ obj)
      (set-search! S 2.0
                   ;;failure proc
                   (lambda (insert ignore)
                     (error #f "wrong"))
                   ;;success proc
                   (lambda (true-element update remove)
                     (remove 'flag)))
    (list-sort < (set->list S^))        ⇒ (1 3)
    obj))                               ⇒ flag

;;failure and ignore
(internal-body
  (define S  (set real-comparator 1 2 3))
  (receive (S^ obj)
      (set-search! S 99
                   ;;failure proc
                   (lambda (insert ignore)
                     (ignore 'flag))
                   ;;success proc
                   (lambda (true-element update remove)
                     (error #f "wrong")))
    (list-sort < (set->list S^))        ⇒ (1 2 3)
    obj))                               ⇒ flag

;;failure and insert
(internal-body
  (define S  (set real-comparator 1 2 3))
  (receive (S^ obj)
      (set-search! S 99
                   ;;failure proc
                   (lambda (insert ignore)
                     (insert 'flag))
                   ;;success proc
                   (lambda (true-element update remove)
                     (error #f "wrong")))
    (list-sort < (set->list S^))        ⇒ (1 2 3 99)
    obj))                               ⇒ flag

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