Git Product home page Git Product logo

dict-extra's People

Contributors

acerempel avatar cobalamin avatar danieljenkins avatar ingara avatar janiczek avatar jvoigtlaender avatar opsb avatar pilatch avatar robinheghan avatar skyqrose avatar zwilias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dict-extra's Issues

document mapKeys duplicate precedence

When the function given to mapKeys returns the same value multiple times, the last returned values overwrites all values that came before. I think it would be good to add a sentence to the docs of mapKeys so users keep this in mind.

frequencies fn

This was useful to me more than once. Would it warrant inclusion in this library?

frequencies ["a","b","c","b","c","b"]
--> {"a": 1, "b": 3, "c": 2}
frequencies : List comparable -> Dict comparable Int
frequencies list =
    list
        |> List.foldl
            (\el counter ->
                Dict.get el counter
                    |> Maybe.withDefault 0
                    |> (\count -> count + 1)
                    |> (\count -> Dict.insert el count counter)
            )
            Dict.empty

`groupBy` groups elements in reverse order

In my opinion, it's more intuitive to group elements in the order they appear in the original list. Here are some examples of groupBy implemented in other languages. Notice that the sort order is preserved within each group:

Ruby (standard library)

[2,3,4,5,6,7].group_by { |x| x % 2 }
# => {0=>[2, 4, 6], 1=>[3, 5, 7]}

Javascript (lodash)

_.groupBy([2,3,4,5,6,7], x => x % 2)
// {"0":[2,4,6],"1":[3,5,7]}

This issue can be fixed by simply using a foldr instead of foldl:

groupBy : (a -> comparable) -> List a -> Dict comparable (List a)
groupBy keyfn list =
-    List.foldl
+    List.foldr
        (\x acc ->
            Dict.update (keyfn x) (Maybe.map ((::) x) >> Maybe.withDefault [ x ] >> Just) acc
        )
        Dict.empty
        list

Performance issue with groupBy

In a call groupBy keyfn list, with the current implementation, the function keyfn will be applied potentially many times to each element of list, though one time should be enough. Depending on how expensive keyfn is to compute, this can be very bad.

The situation is like https://github.com/elm-lang/core/issues/485, and the solution would also be similar to there, i.e., first making a pass through the input list and pairing each element with its keyfn-image.

As a separate note: There was some discussion elsewhere, elm-community/elm-list-extra#38 (comment), whether this function should actually live in what is now https://github.com/elm-community/list-extra.

groupBy with transform

It would be nice to have a version of groupBy that also maps the values. This seems like a common scenario when taking apart and reassembling dictionaries, as below.

invert : Dict comparable (List comparable1) -> Dict comparable1 (List comparable)
invert dict =
    let
        f ( x, ys ) =
            List.map (\y -> ( x, y )) ys

        g _ xys =
            List.map Tuple.first xys
    in
        dict
            |> Dict.toList
            |> List.concatMap f
            |> Dict.Extra.groupBy Tuple.second
            |> Dict.map g

Come to think of it, inverting dictionaries is also pretty common.

elm-package not redirecting to this repo from Skinney/elm-dict-extra

Per discussion at https://elmlang.slack.com/archives/elm-community/p1460684154000129 by @eeue56 it turns out that trying to install the package Skinney/elm-dict-extra fails with a message saying "Error: Could not download source code successfully.". Although github redirects https://github.com/Skinney/elm-dict-extra to this repo it seems that elm-package does not follow that redirect.

We have reached out to @Skinney via email to ask that he fork this repo back again to re-establish the Skinney/elm-dict-extra repo and thereby satisfy elm-package.

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.