Giter Site home page Giter Site logo

trie_again's Introduction

trie_again

Package Version Hex Docs CI

๐ŸŒณ Tries in Gleam

โš™๏ธ This package supports the Erlang and Javascript targets!

Why tries?

A trie is a data structure that uses lists as keys. By taking advantage of this property it is possible to efficiently perform some queries: for example imagine you want to find all the elements that are associated with a key with the same prefix; with a trie the complexity of the lookup is linear in the size of the prefix you're looking for.

That is why tries can be used to implement autocompleting text dictionaries, spell checking or prefix matching algorithms.

In this example a trie is used to store words (as lists of graphemes) as keys associated with a definition. With the subtrie function one can look for all the elements sharing a commong prefix in their key:

import trie
import string

let dictionary =
  trie.new()
  |> trie.insert(at: string.to_graphemes("gleam"), value: "To produce a small, bright light")
  |> trie.insert(at: string.to_graphemes("gleaming"), value: "Bright and shiny")
  |> trie.insert(at: string.to_graphemes("beam"), value: "A line of light that shines from a bright object")

dictionary
|> trie.subtrie(at: ["g", "l"])
|> trie.to_list
// -> [
//      #(["g", "l", "e", "a", "m"], "To produce a small, bright light"),
//      #(["g", "l", "e", "a", "m", "i", "n", "g"], "Bright and shiny"),
//    ]

Installation

To add this package to your Gleam project:

gleam add trie_again

Usage

Import the trie module and write some code! You can find many examples of how the different functions work in the project documentation.

import trie

trie.new()
|> trie.insert(at: ["c", "a", "r"], value: 1)
|> trie.insert(at: ["c", "a", "t"], value: 10)
|> trie.get(at: ["c", "a", "t"])
// -> Ok(10)

Contributing

If you think there's any way to improve this package, or if you spot a bug don't be afraid to open PRs, issues or requests of any kind! Any contribution is welcome ๐Ÿ’œ

trie_again's People

Contributors

giacomocavalieri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.