Comments (7)
I'm happy to hear that you've enjoyed using List 😄
Adding a NonEmptyList
is something that I've been considering and something that I think would be very useful. However, I can see two different ways to implement it, and I'd like to hear what you think about them:
1/ Make NonEmptyList
s a seperate from List
and create a different version of relevant functions that only takes NonEmptyList
s. This is what I've seen done in languages like Haskell and PureScript.
2/ Make NonEmptyList
a subtype of List
. Then every function that accepts a List
also accepts a List
. But the type of relevant functions would change. For instance append
would return a NonEmptyList
and head
would be overloaded with the type
function head<A>(l: NonEmptyList<A>): A;
function head<A>(l: List<A>): A | undefined;
My current thoughts are that the latter approach the best fit for TypeScript. The major downside seems to be that the types of various function will be more complicated to handle NonEmptyList
s as well.
What do you think?
from list.
I was thinking way too long about this, there's always a con that crops up when you have so many pros for one or the other way 😅 In the end, I think your suggestion 2 makes the most sense for this library: it's a self-contained library that has the luxury to know all the types it needs to work with and can therefore provide functions that know how to deal with either List
or NonEmptyList
.
from list.
@grossbart Thank you for the feedback. It's the option I'm leaning towards as well. This shouldn't be too tricky to add. I'll work on it when I get the time and do a PR.
from list.
FWIW, fp-ts
follows approach 1, mimicking what languages like Haskell & PureScript do:
https://github.com/gcanti/fp-ts/blob/master/src/NonEmptyArray.ts
I like this, because it keeps the two types totally isolated and distinct from each other.
EDIT: Whoops, didn't notice NonEmptyArray from fp-ts was linked at the top already. 😁
from list.
@joshburgess I know they separate the two completely, but I don't have the intuitions yet to decide whether combining them for this library would mean trouble. My reasoning was that because this library is self-contained it couldn't hurt to have NonEmptyList
be a subtype of List
with an additional constraint. But maybe this is a fallacy and we trade comfort at the expense of lawfulness? I can certainly see that keeping them separate makes it more clear that they are different things (even though they seem so similar at first glance).
from list.
What about, instead of subtyping, just importing and using the utility functions from List
also with NonEmptyList
and casting? (internally only, I mean)
I'm no fan of casting, in general, but it would be only be an internal implementation detail. With this approach, it would allow reusing the existing code + making maintainability for the two separate list types easier, but would still be type-safe from the outside, requiring the user to use the proper types in their app code.
Just an idea. I would probably resort to something like that, I think... to cut down on maintenance costs, but also try to control how people use the API.
Then again, some utility functions for List might not really apply to NonEmptyList since the possibility of the list being empty is no longer there (this would change the implementation for some functions).
from list.
What about, instead of subtyping, just importing and using the utility functions from
List
also withNonEmptyList
and casting? (internally only, I mean)
That sounds interesting. But, I don't fully understand what you're proposing here? Could you maybe explain it in more detail?
from list.
Related Issues (20)
- vindum.io link in README is broken HOT 1
- npm.js package is not linked to github repo HOT 4
- support sanctuary-show HOT 6
- groupWith seems broken HOT 6
- question: flatMap/chain and iterables HOT 3
- bugs: list returns wrong results (likely bug in drop function) HOT 12
- at least one other bug in the list HOT 19
- list throws HOT 5
- list will not release items for GC HOT 4
- What about `reduced`? HOT 7
- Reverse iteration HOT 10
- Stop mutating HOT 2
- multi-field sort HOT 8
- Can't find fromArray HOT 2
- .random possible? HOT 1
- head() is missing? HOT 3
- nth() throws when it's passed NaN HOT 2
- Access the index on map() operations HOT 4
- findLastIndex HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from list.