Giter Club home page Giter Club logo

elm-list-extra's People

Contributors

abadi199 avatar agrafix avatar apanatshka avatar ayamorisawa avatar ccapndave avatar codedmart avatar dasch avatar eeue56 avatar janiczek avatar jvoigtlaender avatar mdgriffith avatar paralax avatar r41d avatar rehno-lindeque avatar robinheghan avatar seanhess avatar sindikat avatar srid avatar twopoint718 avatar z5h avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elm-list-extra's Issues

groupBy is a bad name and we should feel bad

groupBy is not groupBy. It is groupWhile.

What I mean by this is that the name is misleading. It relies on order! This is not a groupBy.

  • groupWhile looks like: groupWhile (<) [1, 2, 3, 4, 1, 2, 3, 4] == [ [1, 2, 3, 4], [1, 2, 3, 4] ]
  • groupBy looks like groupBy .id [ { id = 1 }, { id = 2 }, { id = 1 } ] == [ [ { id = 1 }, { id = 1 } ], [ { id = 2 } ] ]

Alternatively, groupBy could even go one step further:

  • groupBy : (v -> comparable) -> List v -> Dict comparable (List v)

We have an implementation of that we use internally:

{-| Takes a list of elements and a way to extract a key from each element,
then returns a Dict of those keys, with values being the list of all elements
on which that key was found.

See http://elm-lang.org/edit/examples/Functional/Dict.elm for a more flexible
implementation of groupBy
-}
groupBy : (v -> comparable) -> List v -> Dict comparable (List v)
groupBy keyFromElem list =
  let
    reducer elem dict =
      let
        key =
          keyFromElem elem

        values =
          Maybe.withDefault [] (Dict.get key dict)
      in
        Dict.insert key (List.append values [ elem ]) dict
  in
    List.foldl reducer Dict.empty list

Add setAt

Maybe it's for good reason, but it seems lopsided to have getAt without the equally useful setAt. Am I just missing it?

I've cobbled together a version:

setAt : List a -> Int -> a -> Maybe (List a)
setAt l index value =
  let
    head = List.take index l
    tail = List.drop index l |> List.tail
  in
    case tail of
      Nothing ->
        Nothing

      Just t ->
        Just (value :: t |> List.append head)

though, being a relative elm newb, I'm sure there's room for improvement.

get element by id

Hey i made this small function to get an element by id, perhaps it can be added?

(!!) : List a -> Int -> Maybe a
(!!) list index =
  Array.get index (Array.fromList list)

infixr 9 !!

I liked the !! syntax as used by haskell. Only thing i'm not sure of if i set the right priority with the infixr

Add clojure's partition and partition-all

input = [1..5]

# Basically consecutive pairs/triplets/whatever the first argument is.
# If can't get the needed number, stop.
# I'm not sure about the names
partition' 2 input == [[1,2],[3,4]]
partition' 3 input == [[1,2,3]]
partition' 5 input == [[1,2,3,4,5]]
partition' 6 input == []

# the second arg is how much (head sndGroup) advances from (head fstGroup).
# partition' x == partition'' x x
partition'' 2 1 input == [[1,2],[2,3],[3,4],[4,5]]
partition'' 3 3 input == [[1,2,3]] # almost was [[1,2,3],[4,5,6]] but no dice

# also useful
partition-all' 5 input = [[1,2,3,4,5]]
partition-all' 6 input = [[1,2,3,4,5]]

partition-all'' 4 2 input = [[1,2,3,4],[3,4,5]]

# basically get what you can even if you can't get the specified number of them.

Definitions in Clojure:

I'll add a PR when I get more ti-- some sleep :)

How should it be named? partition is already taken.
Something along the lines of sliding-window?

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.