elm-community / elm-list-extra Goto Github PK
View Code? Open in Web Editor NEWDEPRECATED; use elm-community/list-extra
License: MIT License
DEPRECATED; use elm-community/list-extra
License: MIT License
Essentially the same as here: https://github.com/elm-lang/core/issues/485.
Currently it will return the head of the list.
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
Aren't these two functions in this package doing the exact same thing?
@rtfeldman (who added unique
)
As per elm-community/guidelines#27, and in particular this discussion: elm-community/guidelines#27 (comment).
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.
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
The discussion for #27 revealed that getAt
should probably swap around its parameters, i.e. the index should be first and the list second. This is primarily motivated by this section in the design guidelines.
Would you be open to a PR implementing these two?
cf. https://github.com/srid/chronicle/blob/master/src/Elm/Util.elm#L9
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
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.