Set.has
func Set.has[T](s: Set[T], x: T): boolSet.has reports whether x is a member of s.
Details
- Members compare by
==, so a record or an array matches by contents. - To test and insert in one step, use Set.add: it returns
truewhen the value was not a member.
Basic examples
let colors = Set.of(["red", "green", "blue"])
Set.has(colors, "green") // true
Set.has(colors, "purple") // falseCall it through . on any set:
let primes = Set.of([2, 3, 5, 7])
primes.has(5) // true
primes.has(9) // falseScope
On an empty set, has is false for every value:
let none: Set[string] = Set.of([])
none.has("") // falseRecords and arrays compare by value:
type Point = { x: int, y: int }
let visited = Set.of([Point{ x: 0, y: 0 }, Point{ x: 1, y: 0 }])
let pairs = Set.of([[1, 2], [3, 4]])
visited.has(Point{ x: 1, y: 0 }) // true
visited.has(Point{ x: 0, y: 1 }) // false
pairs.has([3, 4]) // true
pairs.has([4, 3]) // falseA tagged union member matches by tag and fields, so equal fields under another tag do not match:
type Circle = { size: float64 }
type Square = { size: float64 }
type Shape = Circle | Square
let shapes: Set[Shape] = Set.of([Circle{ size: 1.0 }])
shapes.has(Circle{ size: 1.0 }) // true
shapes.has(Square{ size: 1.0 }) // falseA set may hold null when its element type includes it:
let answers: Set[bool | null] = Set.of([true, null])
answers.has(null) // true
answers.has(false) // falseStrings compare by code points, without normalizing:
let words = Set.of(["café"])
words.has("café") // true
words.has("cafe\u{301}") // falseOn a set that may itself be null, ?. gives null; ?? turns that into a bool:
let tags: Set[string] | null = null
tags?.has("a") // null
tags?.has("a") ?? false // falseIn a pipeline, the set is the first argument:
Set.of(["a", "b"]) | Set.has("b") // trueApplications
Drop the stop words from a sentence:
let stop = Set.of(["a", "an", "the", "of"])
"the end of a story".split(" ").filter({ !stop.has(x) }) // ["end", "story"]Walk a graph breadth first, visiting each node once even when edges form a cycle:
let edges = Map{ a: ["b", "c"], b: ["c"], c: ["a", "d"], d: [] }
let seen = Set.of(["a"])
let queue = ["a"]
var i = 0
while i < queue.length() {
let node = queue[i]
i += 1
println(node)
for next in edges[node] ?? [] {
if !seen.has(next) {
seen.add(next)
queue.push(next)
}
}
}a
b
c
dCheck a value against a list of allowed ones:
let methods = Set.of(["GET", "HEAD", "POST"])
func allowed(method: string): bool {
methods.has(method.toUpper())
}
println(allowed("get"))
println(allowed("DELETE"))true
falseProperties and relations
Set.has agrees with Array.contains on the members, and with the array the set was built from:
let xs = [3, 1, 3, 2]
let s = Set.of(xs)
for x in [0, 1, 2, 3, 4] {
assert(s.has(x) == s.values().contains(x))
assert(s.has(x) == xs.contains(x))
}Set.add puts a member in and Set.remove takes it out:
let s: Set[string] = Set.of([])
s.add("x")
assert(s.has("x"))
s.remove("x")
assert(!s.has("x"))
assert(s.isEmpty())Membership in a union, an intersection, or a difference follows from membership in the two sets:
let a = Set.of([1, 2, 3])
let b = Set.of([2, 3, 4])
for x in [1, 2, 3, 4, 5] {
assert(a.union(b).has(x) == (a.has(x) || b.has(x)))
assert(a.intersect(b).has(x) == (a.has(x) && b.has(x)))
assert(a.difference(b).has(x) == (a.has(x) && !b.has(x)))
}Possible issues
The value must have the set's element type:
let ids = Set.of([1, 2, 3])
println(ids.has("1")) // error: cannot use string where int is expectedNaN is not == to itself, so a NaN member is never found. 0.0 and -0.0 are ==, so they find each other:
Set.of([Math.NaN]).has(Math.NaN) // false
Set.of([0.0]).has(-0.0) // trueA member is hashed when it is added. Mutating an array or record after you add it is a program error, and Set.has may no longer find it. Add a copy, or do not change the member.
See also
Set.of · Set.add · Set.remove · Set.values · Set.isSubset · Map.has · Array.contains
