-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-Empty Discussion #105
Comments
I'm not a huge fan myself, as I usually pass the result of It's not a big deal, but personally I don't often have use of non-empty lists. |
In my own code, I often will use
|
I think that would be a good idea. I also think it would be good to use a type alias in those functions which return a nonempty list, to make it more clear :) type alias Nonempty = (a, List a) |
I also like The alias, I am not so sure. I see I could be wrong. Maybe this is a worthy trade off. This is the kind of thing I wish we could obtain empirical data on, but I suppose we never will. |
I once wrote this piece of code where the non-empty actually came in quite useful! -- GROUPING
type alias Groups a =
List (Group a)
type alias Group a =
{ name : String, items : List a }
type alias Config a =
{ outerOrder : Comparison a
, innerOrder : Comparison a
, makeName : a -> String
}
type alias Comparison a =
a -> a -> Order
{-| preprocess elements before passing them to a comparison function
-}
pipeComparison : (a -> b) -> Comparison b -> Comparison a
pipeComparison fn comparison firstOperand secondOperand =
comparison (fn firstOperand) (fn secondOperand)
{-| Turn a list into a list of groups with a title
type alias Pizza = {cat : String, price : Int}
items : List Pizza
items = [ {cat = "Veggie", price = 7}
, {cat = "Veggie", price = 10}
, {cat = "Beef", price = 8}
, {cat = "Beef", price = 9}
]
cfg : Config Pizza
cfg =
{ outerOrder = pipeComparison .cat compare
, innerOrder = pipeComparison .price compare
, makeName = .cat
}
make cfg items
--> [ { items = [{ cat = "Beef", price = 8} , { cat = "Beef", price = 9 }], name = "Beef"}
--> , { items = [{ cat = "Veggie", price = 7} , { cat = "Veggie", price = 10 }], name = "Veggie" }
--> ]
-}
sortedGroups : Config a -> List a -> Groups a
sortedGroups { outerOrder, innerOrder, makeName } =
-- sort and make groups for outer
List.sortWith outerOrder
>> List.Extra.groupWhile (\a b -> outerOrder a b == EQ)
>> List.map (\( first, rest ) -> { name = makeName first, items = first :: rest })
-- sort inner
>> List.map (\group -> { group | items = List.sortWith innerOrder group.items }) |
I have had a few use cases recently where I tried out a non-empty list. They didnt pan out too well I think, but that could have been just due to my use cases. ButtonRow.view : (Button msg, List (Button msg)) -> Html msg I dont think a non empty list was really worthwhile for this use case, and I have since refactored back to a regular An admin panel type Model
= NoGroups
| SomeGroups (Group, List Group) GroupsLoadedModel A non-empty list was handy here, not because the state of no groups to administer isnt possible, but because when there are some groups there necessarily should be a bunch of other values as well. But then, for my project, it turned out to be quite a lot more possible to have no groups later on in the UX than I expected, so I reverted to a regular list. So the actual non-empty list type wasnt used, but, it has been useful to fork on list emptiness at the "top" of the applicatio. |
My vote is to use the |
We recently merged a change to
groupWhile
to make it deal with non-empty lists(a, List a)
. Until now, we have kind of steered clear of non-empty lists.So now we have a little bit of non-empty lists in List.Extra. Is that a good move? A bad move? Where do we draw the line? Do we need to draw any lines? Bottom line question is, should we have non-empty list support in List Extra?
My opinion is that theres no problem with a some non-empty list functions. We dont have to support every possible operation, but a few helpers that can make it easy to transform non-empty lists to lists and vice versa would be nice.
The text was updated successfully, but these errors were encountered: