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
Still more NonEmpty variants of inits & tails #252
Comments
It seems these identities hold (could someone double check?) List.inits == Foldable.toList . NonEmpty.inits
nonEmptyInits == Foldable.toList . NonEmpty.inits1 If that's so, then perhaps this new combinator should be called |
Putting the new functions in |
-1. Just use |
@mixphix, if my input is a possibly-empty list, |
Excuse me? ghci> :m +Data.Foldable Data.List Data.List.NonEmpty
-- 1
ghci> Data.List.tails [1, 2, 3, 4]
[[1,2,3,4],[2,3,4],[3,4],[4],[]]
-- 2
ghci> Data.List.NonEmpty.tails (1 :| [2, 3, 4])
[1,2,3,4] :| [[2,3,4],[3,4],[4],[]]
-- 3
ghci> Data.List.NonEmpty.tails1 (1 :| [2, 3, 4])
(1 :| [2,3,4]) :| [2 :| [3,4],3 :| [4],4 :| []]
-- 4
ghci> Data.List.NonEmpty.toList $ Data.List.NonEmpty.tails1 (1 :| [2, 3, 4])
[1 :| [2,3,4],2 :| [3,4],3 :| [4],4 :| []]
-- 5
ghci> Data.List.NonEmpty.toList <$> Data.List.NonEmpty.tails1 (1 :| [2, 3, 4])
[1,2,3,4] :| [[2,3,4],[3,4],[4]]
ghci> let nonEmptyTails = fmap Data.List.NonEmpty.fromList . Data.List.init . Data.List.tails . Data.Foldable.toList
ghci> nonEmptyTails (1 :| [2, 3, 4])
[1 :| [2,3,4],2 :| [3,4],3 :| [4],4 :| []] -- same as 4
ghci> nonEmptyTails [1, 2, 3, 4]
[1 :| [2,3,4],2 :| [3,4],3 :| [4],4 :| []] -- same as 4 |
But |
This is not primitive, not useful, and not hard to get by without. |
Again, these functions appear as subexpressions of the existing I agree we can get by without it. It's not hard to implement, but neither is it hard to implement Regardless, Sorry if this is too trivial a thing to suggest. |
Prior art is I personally think that this is a useful addition. It's fairly common to encounter things like |
Sadly for me, those functions are analogous to the existing |
Ah, my bad, sorry. I'm not really sure about it then. If we can come up with good, self-explaining names then improving consistency and symmetry would be nice. But otherwise - as one can guess from my mistake above - there is a cost of offering users too many confusingly similar alternatives. |
Previously: #67
We currently have:
These three result types (
[[a]]
,NonEmpty [a]
,NonEmpty (NonEmpty a)
) form an incomplete square; the missing type is[NonEmpty a]
, and there is a perfectly reasonable variant of these functions that would return that type (it resemblesinits1
/tails1
but doesn't require its input to be non-empty).The names are up for debate since I'm unable to find an existing convention for functions matching these types. But I propose adding to
Data.List.NonEmpty
the following (with these names or better ones):These are subexpressions of the current implementations of
inits1
andtails1
, which should be refactored to reference them. If these implementations are later optimized to, e.g., make fewer unnecessary run-time checks for emptiness, exposing them as functions makes those improvements available to users who would otherwise be defining these variants themselves.The text was updated successfully, but these errors were encountered: