CX · Reference guideset
PlaygroundDownloadsAboutv0.18.0-pre.1

set

Bundled cx-stdlib module.

set:of

[$set:of] (members::any) -> map — The set of a sequence's members — a map from each member to true, in first-occurrence order; a pair or an array is one member, compared by its canonical bytes (CKEY-1); a member that is an element or a document refuses CXER0100 as map:entry refuses a non-key.

      [?lib 'cx-stdlib/set']
[$set:of (b, a, b, 1, '1')]
    
      {b: true, a: true, 1: true, '1': true}
    

set:contains

[$set:contains] (s::map, member::any) -> bool — Whether the member is in the set, under (kind, image) identity — an int 1 is not the string '1'.

      [?lib 'cx-stdlib/set']
[$set:contains [$set:of (1, 2)] 2]
    
      true
    

set:members

[$set:members] (s::map) -> any — The sequence of members in insertion order, each carrying its own kind.

      [?lib 'cx-stdlib/set']
[$set:members [$set:of (c, a, b)]]
    
      (c, a, b)
    

set:add

[$set:add] (s::map, member::any) -> map — The set with one more member (Python's set.add): a new member goes last, a present one stays where it is; usable as a [using] step of a fold. A member that is not an admissible key scalar refuses CXER0100 as set:of does.

      [?lib 'cx-stdlib/set' as=set]
[$set:contains [$set:add [$set:of (1)] 2] 2]
    
      true
    

set:remove

[$set:remove] (s::map, member::any) -> map — The set without the member (Python's set.discard): an absent member answers the set unchanged, as map:remove of an absent key does.

      [?lib 'cx-stdlib/set']
([$set:add [$set:of (a, b)] c], [$set:add [$set:of (a, b)] a], [$set:add [$set:of (1)] '1'], [$set:remove [$set:of (a, b, c)] b], [$set:remove [$set:of (a)] z], [?reduce (3, 1, 3, 2) [using [?fn ($s $x) [$set:add $s $x]]] [init [$set:of ()]]])
    
      ({a: true, b: true, c: true}, {a: true, b: true}, {1: true, '1': true}, {a: true, c: true}, {a: true}, {3: true, 1: true, 2: true})
    

set:union

[$set:union] (a::map, b::map) -> map — Every member of either set — the first set's members in its order, then the second's new members in the second's order.

      [?lib 'cx-stdlib/set']
[$set:union [$set:of (a, b)] [$set:of (b, c)]]
    
      {a: true, b: true, c: true}
    

set:intersection

[$set:intersection] (a::map, b::map) -> map — The members of the first set that are also in the second, in the first set's order.

      [?lib 'cx-stdlib/set']
[$set:intersection [$set:of (a, b, c)] [$set:of (c, a)]]
    
      {a: true, c: true}
    

set:difference

[$set:difference] (a::map, b::map) -> map — The members of the first set that are not in the second, in the first set's order.

      [?lib 'cx-stdlib/set']
[$set:difference [$set:of (a, b, c)] [$set:of (b)]]
    
      {a: true, c: true}
    

set:subset

[$set:subset] (a::map, b::map) -> bool — Whether every member of the first set is a member of the second; the empty set is a subset of every set.

      [?lib 'cx-stdlib/set']
[$set:subset [$set:of (a)] [$set:of (a, b)]]
    
      true
    

set:closure

[$set:closure] (seed::map, step::function, opts::map) -> map — The fixed point of a step from a seed: each round calls step with the members the previous round added and the whole set so far, and adds the members of its answer not yet seen, until a round adds none. max-rounds defaults to 10,000 and max-size to no bound; reaching either before the fixed point refuses CXER7100 naming the bound and its value.

      [?lib 'cx-stdlib/set']
[?lib 'cx-stdlib/map']
[?let [= $edges {a: (b), b: (c, a), c: ()}]
  [$set:closure [$set:of (a)]
    [?fn ($frontier $seen)
      [$set:of [?for [in $m [$set:members $frontier]] [in $n [$map:get $edges $m]] [yield $n]]]]]]
    
      {a: true, b: true, c: true}