Giter Site home page Giter Site logo

lue-bird / elm-fast-dict Goto Github PK

View Code? Open in Web Editor NEW

This project forked from minibill/elm-fast-dict

0.0 0.0 0.0 129 KB

A drop-in, faster implementation of `Dict` from `elm/core`

Home Page: https://package.elm-lang.org/packages/miniBill/elm-fast-dict/latest/

License: BSD 3-Clause "New" or "Revised" License

JavaScript 1.14% Elm 97.92% Makefile 0.62% HTML 0.32%

elm-fast-dict's Introduction

miniBill/elm-fast-dict Build Status

This is a replacement package for Dict from elm/core. The API is identical, except needing to use equals instead of == for comparing two dictionaries.

The suggested way to use this package is by replacing import Dict with import FastDict as Dict in your project.

The main differences between Dict and FastDict are:

  1. This is not a built-in type, so you can't use == with it. If you do you may get false negatives (dictionaries which are equal but that == considers different) - use FastDict.equals to check for equality;
  2. size is O(1) instead of O(n) (i.e.: it runs in constant time instead of scanning the whole dictionary);
  3. union automatically merges the smaller dictionary into the bigger (without changing the result), making it faster;
  4. intersect is O(m + n) instead of O(m log n) and in practice is MUCH faster for small intersections (usually ranging from 2x faster to 100x faster);
  5. equals is sometimes faster: it's O(1) if the size is different, and O(index of first different value) otherwise instead of O(m + n);
  6. getMinKey/getMin/popMin/getMaxKey/getMax/popMax functions are available, with cost O(log n),
  7. stoppableFoldl/stoppableFoldr/restructure functions are available.

When to use this package

  • You use intersect, union or size a lot;
  • you have big dictionaries;
  • you need a fast getMin/getMax function;
  • you need stoppableFolds or restructure.

When not to use this package

  • You need to interact with code that expects elm/core dictionaries a lot;
  • you have tiny dictionaries;
  • you have a lot of existing code that would need to be checked for uses of ==;
  • you need to use Html.Lazy.

Examples

import FastDict as Dict exposing (Dict)

type alias Priority =
    Int

type alias Job =
    String

queue : Dict Priority Job
queue =
    Dict.fromList
        [ (3, "Shave the yak")
        , (5, "Reticulate splines")
        , (1, "Feed the gremlins")
        ]

{-| Returns the most important item

    mostImportant queue
    --> Just (1, "Feed the gremlins")

-}

mostImportant : Dict Priority Job -> Maybe (Priority, Job)
mostImportant =
    Dict.getMin

elm-fast-dict's People

Contributors

lue-bird avatar minibill 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.