All Projects → quickbirdstudios → NonEmptyCollections

quickbirdstudios / NonEmptyCollections

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

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to NonEmptyCollections

Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
Stars: ✭ 125 (+177.78%)
Mutual labels:  map, set, list, collection, collections
PowerCollections
Powerfull Collections, Sets, Lists and Maps.
Stars: ✭ 15 (-66.67%)
Mutual labels:  lists, maps, collections, sets
Buckets Js
A complete, fully tested and documented data structure library written in pure JavaScript.
Stars: ✭ 1,128 (+2406.67%)
Mutual labels:  map, set, collection, collections
Mlib
Library of generic and type safe containers in pure C language (C99 or C11) for a wide collection of container (comparable to the C++ STL).
Stars: ✭ 321 (+613.33%)
Mutual labels:  set, list, collections
Collection
A PHP library for representing and manipulating collections.
Stars: ✭ 488 (+984.44%)
Mutual labels:  map, set, collection
Redisson
Redisson - Redis Java client with features of In-Memory Data Grid. Over 50 Redis based Java objects and services: Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Publish / Subscribe, Bloom filter, Spring Cache, Tomcat, Scheduler, JCache API, Hibernate, MyBatis, RPC, local cache ...
Stars: ✭ 17,972 (+39837.78%)
Mutual labels:  map, set, list
Java
"Always choose a lazy person to get the job done. A lazy person will always find the easy way out."
Stars: ✭ 16 (-64.44%)
Mutual labels:  lists, maps, sets
php-sorted-collections
Sorted Collections for PHP
Stars: ✭ 22 (-51.11%)
Mutual labels:  map, set, collection
Gods
GoDS (Go Data Structures). Containers (Sets, Lists, Stacks, Maps, Trees), Sets (HashSet, TreeSet, LinkedHashSet), Lists (ArrayList, SinglyLinkedList, DoublyLinkedList), Stacks (LinkedListStack, ArrayStack), Maps (HashMap, TreeMap, HashBidiMap, TreeBidiMap, LinkedHashMap), Trees (RedBlackTree, AVLTree, BTree, BinaryHeap), Comparators, Iterators, …
Stars: ✭ 10,883 (+24084.44%)
Mutual labels:  map, set, list
Awesome Maps
There is more than google: A collection of great online maps 🌍🗺🌎
Stars: ✭ 124 (+175.56%)
Mutual labels:  map, collection, maps
observable ish
Observable state and events for browser and Flutter.
Stars: ✭ 26 (-42.22%)
Mutual labels:  map, set, list
Collectable
High-performance immutable data structures for modern JavaScript and TypeScript applications. Functional interfaces, deep/composite operations API, mixed mutability API, TypeScript definitions, ES2015 module exports.
Stars: ✭ 233 (+417.78%)
Mutual labels:  map, set, list
HelloWorlds
Hello-World program in most programming languages
Stars: ✭ 102 (+126.67%)
Mutual labels:  lists, collection, collections
php-collections
A collection library for php
Stars: ✭ 34 (-24.44%)
Mutual labels:  map, collections
leaflet.minichart
Leaflet.minichart is a leaflet plugin for adding to a leaflet map small animated charts
Stars: ✭ 27 (-40%)
Mutual labels:  map, maps
vaguely-rude-places
The map of Vaguely Rude Place Names
Stars: ✭ 19 (-57.78%)
Mutual labels:  map, maps
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-53.33%)
Mutual labels:  set, sets
s60-maps
Yet another maps for Symbian OS
Stars: ✭ 27 (-40%)
Mutual labels:  map, maps
go-streams
Stream Collections for Go. Inspired in Java 8 Streams and .NET Linq
Stars: ✭ 127 (+182.22%)
Mutual labels:  collection, collections
react-map-gl-cluster
Urbica React Cluster Component for Mapbox GL JS
Stars: ✭ 27 (-40%)
Mutual labels:  map, maps

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].