Coder Social home page Coder Social logo

quickbirdeng / nonemptycollections Goto Github PK

View Code? Open in Web Editor NEW
51.0 4.0 4.0 114 KB

A type-safe implementation for collections that cannot be empty. Life is too short for emptiness-checks!

License: MIT License

Kotlin 100.00%
kotlin non empty non-empty collections collection list lists set sets

nonemptycollections's Introduction

NonEmptyCollections

Making the world a little more type-safe ๐ŸŒ๐Ÿ†

Cover Image

Reduce the need for emptiness checks and reduce unsafe APIs with NonEmptyCollections.

You can use NonEmptyList, NonEmptySet and NonEmptyMap to restrict the input of functions to make your code safer and avoid unnecessary runtime exceptions.

For a detailed explanation see our related article Non-Empty Lists in Kotlin.

This is an early version and work in progress. Do not hesitate to give feedback, ideas or improvements via an issue.

Examples

Average without exceptions

With the NonEmptyList type, we can make sure that at least one element is always a list. If we want to calculate the average of that list, it is impossible to compile a program where an invalid input is passed to our function.

fun NonEmptyList<Int>.average() = sum() / size
nonEmptyListOf<Int>().average()   // This does not compile! โŒ

nonEmptyListOf(1, 2, 3).average() // This does! โœ…

Non-empty Shopping-Cart

Let's imagine an online shop, where you can put articles into a shopping cart. If you have some articles in the shopping cart, you should be able to share the articles with your friends, save them for later on a wish list or directly buy them. But these three features just make sense if the shopping cart is not empty. Wouldn't it be cool to already prevent at compile-time that somebody tries these features with an empty set of articles?

sealed class ShoppingCart {
    object Empty : ShoppingCart()

    data class Filled(
        val articles: NonEmptySet<Article>
    ) : ShoppingCart() {

        fun buy(paymentType: PaymentType) = articles.buy(paymentType)
        fun share() = articles.share()
        fun saveTo(wishList: WishList) = articles.saveTo(wishList)
    }
}

fun NonEmptyCollection<Article>.buy(paymentType: PaymentType) { ๐Ÿ’ธ }

fun NonEmptyCollection<Article>.share() { ๐Ÿ’ฌ }

fun NonEmptyCollection<Article>.saveTo(wishList: WishList) { ๐Ÿ’พ }

The devs, who implement buy, share and saveTo don't have to handle the empty case. The consumers of these APIs don't have to think of exception handling, because they are forced by the compiler to provide a valid input. We would say, that's a win-win situation ๐Ÿ†.

๐Ÿƒ Library Setup

1. Add the repository

build.gradle.kts

allprojects {
    repositories {
        ...
        maven { url = uri("https://jitpack.io") }
    }
}

2. Add the dependency

build.gradle.kts

dependencies {
    ...
    implementation("com.github.quickbirdstudios.NonEmptyCollections:NonEmptyCollections:1.1.0")
}

๐Ÿ‘ค Author

This Kotlin library is created with โค๏ธ by QuickBird Studios.

โค๏ธ Contributing

Open an issue if you need help, if you found a bug, or if you want to discuss a feature request.

Open a PR if you want to make changes to NonEmptyCollections.

๐Ÿ“ƒ License

NonEmptyCollections is released under an MIT license. See License for more information.

nonemptycollections's People

Contributors

balazstothofficial avatar quick-mischa-bird avatar ragnese avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nonemptycollections's Issues

Petition Kotlin to include?

Greetings - awesome work - is there a YouTrack to include this capability in Kotlin itself? Seems like a high-value-add!

API Suggestion: Replace/remove all "unsafe" toNonEmptyFoo extensions and rename the nullable ones

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.