This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Experimental Sorter/Dict/Set API.
version 1.0.0
license BSD-3-Clause
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.1.0
Committed At 2018-06-29 14:42:32 UTC
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1

README

elm-sorter-experiment

It's right there in the name, but just to be totally clear about it:

⚠️ THIS IS AN EXPERIMENT! ⚠️

I have a hypothesis that these may be good ideas, but they may not be! The purpose of this package is to facilitate trying them out and learning more.

This package is a group of related experiments I've been pondering for awhile. They are:

  1. A composable Sorter API that replaces List.sort, List.sortBy, and List.sortWith with a single function (Sort.list). In particular, Sort.by and Sort.reverse (when used together) nicely scratch an itch I've encountered on a few different occasions.
  2. An implementation of Dict and Set that use Sorter to permit keys that are not comparable.
  3. Some minor naming changes to Dict and Set (explained below).

Since we're already in experimental territory, I based this experiment on another one; this is using Skinney/elm-dict-exploration under the hood for some performance benefits.

Prior art

Set and Dict API Changes

In this package, the following operations on both Set and Dict now take a Sorter as their first argument:

  • empty
  • singleton
  • fromList
  • merge

Additionally, diff has been given named arguments in order to prevent ordering mistakes. It is now:

{-| Keep a value when it appears in the `original` set
but not in the `other` set. The `original` set's `Sorter` will be used.

-}
diff : { original : Set a, other : Set a } -> Set a

Set API Changes

insert

-insert : a -> Set a -> Set a
+add : a -> Set a -> Set a

There is strong consensus among languages for what this operation should be called. Python, Ruby, Java, JavaScript, and OCaml, all call this add. Erlang calls it addElement and Scala overloads (+) for this operation.

Only Haskell calls it insert.

map

-map : (a -> b) -> Set a -> Set b
+map : Sorter b -> (a -> b) -> Set a -> Set b

An extra argument is needed to specify the Sorter for the resulting Set.

intersect and union

-intersect : Set a -> Set a -> Set a
+intersect : Sorter a -> Set a -> Set a

-union : Set a -> Set a -> Set a
+union : Sorter a -> Set a -> Set a

These now take an explicit Sorter in order to make it clear which sorting operation will be used.

Dict API Changes

insert

-insert : k -> v -> Dict k v -> Dict k v
+store : k -> v -> Dict k v -> Dict k v

There is no consensus among languages for what this operation should be called. Ruby's store seems clearer than Haskell's insert because "inserting" is often an operation that strictly increases the size of the collection (e.g. INSERT in SQL) whereas "storing" is idempotent.

(Other alternatives considered: put from Java, add from OCaml, replace from ReasonML, set from JavaScript.)

intersect

-intersect : Dict k v -> Dict k v -> Dict k v
+intersect : { preferred : Dict k v, other : Dict k v } -> Dict k v

intersect has been given named arguments in order to clarify which values will be kept.

union

-union : Dict k v -> Dict -> k v -> Dict k v
+storeAll : { from : Dict k v, into : Dict k v } -> Dict k v

union has been renamed and given named arguments to make it clearer what happens in the event of collisions.

"It takes all the key/value pairs from one dictionary and stores them into the other one" builds on the caller's understanding of how store resolves collisions.

This API also makes it clear which dictionary's Sorter will be used.