symbol

A unique symbolic identifier providing O(1) equality, comparison, and hashing.

symbol
moon add FlyCloudC/symbol@0.4.0
Download zip
Author
Version
0.4.0
License
MIT
Last updated
12 days ago
Downloads
85
README

#FlyCloudC/symbol

A unique symbolic identifier providing O(1) equality, comparison, and hashing.

#Features

  • Symbol::of creates symbols from strings
  • Symbol::generate creates unique new symbols
  • Symbol::to_string returns the original string

#Example

///|
test {
let names = ["a1", "a2", "", "中", "long name"]
let symbols = names.map(Symbol::of)
@debug.assert_eq(symbols.map(Symbol::to_string), names)

symbols.push(Symbol::generate())
symbols.push(Symbol::generate())

let eq_sym = []
for s1 in symbols {
for s2 in symbols {
if s1 == s2 {
assert_eq(s1.hash(), s1.hash())
eq_sym.push(s1)
}
}
}
debug_inspect(
eq_sym,
content=(
#|[
#| <Symbol: "a1">,
#| <Symbol: "a2">,
#| <Symbol: "">,
#| <Symbol: "中">,
#| <Symbol: "long name">,
#| <Symbol: "#{gensym 0}">,
#| <Symbol: "#{gensym 1}">,
#|]
),
)
}

#
Symbol

type Symbol derive(Compare, Default, Eq, Hash)

A unique symbolic identifier providing O(1) equality, comparison, and hashing.
impl Show for Symbol

#
Symbol::generate

fn Symbol::generate() -> Symbol

Generates a unique symbolic identifier using a global counter.

#
Symbol::is_generated

fn Symbol::is_generated(self : Symbol) -> Bool

if this symbol was created via generate() rather than from a string.

#
Symbol::of

fn Symbol::of(name : String) -> Symbol

Creates a symbol from a string.

Source Files