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


2.37.11 Bag–only procedures

Function: bag-sum bag0 bag

Return a newly allocated bag containing all the unique elements in all the bag arguments, such that the count of each unique element in the result is equal to the sum of the counts of that element in the arguments. It differs from bag-union by treating identical elements as potentially distinct rather than attempting to match them up.

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

(let* ((B1 (bag fixnum-comparator 1 2))
       (B2 (bag fixnum-comparator 2 3))
       (B3 (bag fixnum-comparator 3 4))
       (B  (bag-sum B1 B2 B3)))
  (bag->list B #t))
⇒ (1 2 2 3 3 4)
Function: bag-sum! bag0 bag

Like bag-sum but it is permitted to mutate and return bag0 rather than allocating a new set.

Function: bag-product N bag

Return a newly allocated bag containing all the unique elements in bag, where the count of each unique element in the bag is equal to the count of that element in bag multiplied by N.

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

(let* ((B1 (bag fixnum-comparator 1 2))
       (B  (bag-product 0 B1)))
  (bag->list B #t))
⇒ ()

(let* ((B1 (bag fixnum-comparator))
       (B  (bag-product 2 B1)))
  (bag->list B #t))
⇒ ()

(let* ((B1 (bag fixnum-comparator 1 2))
       (B  (bag-product 3 B1)))
  (bag->list B #t))
⇒ (1 1 1 2 2 2)
Function: bag-product! N bag

Like bag-product but it is permitted to mutate and return bag rather than allocating a new set.

Function: bag-unique-size bag

Return the number of unique elements of bag.

Function: bag-element-count bag element

Return an exact integer representing the number of times that element appears in bag.

Function: bag-for-each-unique proc bag

Apply proc to each unique element of bag in arbitrary order, passing the element and the number of times it occurs in bag, and discarding the returned values. Return an unspecified result.

Function: bag-fold-unique proc nil bag

Invoke proc on each unique element of bag in arbitrary order, passing the number of occurrences as a second argument and the result of the previous invocation as a third argument. For the first invocation: nil is used as the third argument. Return the result of the last invocation.

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

(let ((B (bag fixnum-comparator)))
  (bag-fold-unique (lambda (elm count knil)
                     (cons (list elm count) knil))
    '()
    B))
⇒ '()

(let ((B (bag fixnum-comparator 1 2 3)))
  (bag-fold-unique (lambda (elm count knil)
                     (cons (list elm count) knil))
    '()
    B))
⇒ ((1 1) (2 1) (3 1))
Function: bag-increment! bag element count
Function: bag-decrement! bag element count

Linear update procedures that return a bag with the same elements as bag, but with the element count of element in bag increased or decreased by the exact integer count (but not less than zero).

Function: bag->set bag

Return a newly allocated set containing the unique elements (in the sense of the equality predicate) of bag.

Function: set->bag set

Return a newly allocated bag containing the elements of set.

Function: set->bag! bag set

return a bag containing the elements of both bag and set. In all cases, the comparator of the result is the same as the comparator of the argument or arguments.

Function: bag->alist bag
Function: bag->alist bag compar

Return a newly allocated alist whose keys are the unique elements of bag and whose values are the number of occurrences of each element.

As Vicare extension, if the optional argument compar is present:

Usage examples:

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

(let ((B (bag fixnum-comparator 1 2 3)))
  (bag->alist B #t))
⇒ ((1 . 1) (2 . 1) (3 . 1))

(let ((B (bag fixnum-comparator 1 1 2 2 3 3)))
  (bag->alist B fx<?))
⇒ ((1 . 2) (2 . 2) (3 . 2))
Function: alist->bag comparator alist

Return a newly allocated bag based on comparator, where the keys of alist specify the elements and the corresponding values of alist specify how many times they occur.

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

(let ((B (alist->bag fixnum-comparator '())))
  (bag->alist B))
⇒ ()

(let ((B (alist->bag fixnum-comparator
           '((1 . 1) (2 . 1) (3 . 1)))))
  (bag->alist B #t))
⇒ ((1 . 1) (2 . 1) (3 . 1))

(let ((B (alist->bag fixnum-comparator
           '((1 . 2) (2 . 2) (3 . 2)))))
  (bag->alist B fx<?))
⇒ ((1 . 2) (2 . 2) (3 . 2))

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