FlyCloudC/symbol

★ 0Forks 0MoonBitGitHub ↗Compare

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}">,
      #|]
    ),
  )
}

Contributors

FlyCloudC

Issues