quickbirdeng / nonemptycollections Goto Github PK
View Code? Open in Web Editor NEWA type-safe implementation for collections that cannot be empty. Life is too short for emptiness-checks!
License: MIT License
A type-safe implementation for collections that cannot be empty. Life is too short for emptiness-checks!
License: MIT License
i.e., interface NonEmptyCollection<out T>: Collection<T>
because Collection
is defined as interface Collection<out T>
.
Then NonEmptyList, NonEmptySet, NonEmptyMap should also use out params.
It doesn't hurt anything for them not to.
But, I don't actually know how to implement the firstOr
or ifEmpty
ones. It might be better to just leave them alone (as in: don't override them at all).
firstOrNull()
is easy, though:
fun firstOrNull(): T = first()
For example, you have:
fun <T> List<T>.nonEmptyOrNull() = if (isEmpty()) null else toNonEmptyList()
and
@UnsafeNonEmptyCollectionApi
fun <T> Iterable<T>.toNonEmptyList(): NonEmptyList<T> = isAlreadyNonEmptyOr {
nonEmptyListOf(first(), drop(1))
}
If it were me, I'd probably be lazy and just drop the whole unsafe set. The user can always just do:
val nonEmpty = someList.nonEmptyOrNull() ?: error("Bug! I expected this to be non-empty!")
But if you keep both APIs, I suggest at least naming both sets with the "toNonEmptyFoo" style. A lot of methods in the Kotlin standard library have names like toList
, so going with toNonEmptyList
seems like a good match.
Greetings - awesome work - is there a YouTrack to include this capability in Kotlin itself? Seems like a high-value-add!
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.