Skip to content

zorba128/scala-collection-strict

Repository files navigation

scala-collection-strict

This module provides custom alternative to Map/Set collections from scala library. It is still far from being production-ready, its main goal is to propose and evaluate alternative approach.

Explanation

Proposal is to change Set and Map interpretation - rather than focusing on set algebra, make it behave like constrained collection of distinct elements.

This leads to builders throwing exception on duplicates rather than dropping them silently. We all are used to current behavior (it's been there since java 1.0) but maybe its high time to at least discuss if this was the best choice.

Of course old behavior has to stay available - it’s still needed to be able to easily include element in the set, regardless if it was already there or not. But I’d like to propose to separate those apis. Let Set.add() add elements, and Set.incl() ensure element is included.

Story document provides more detailed discussion of the idea.

Usage

Collections are in the io.mk.collections package. For now only immutable variants are provided.

  • StrictSet
  • StrictMap

Basic example illustrating the idea

  val a = StrictSet("a", "A")
  a.incl("a") // StrictSet("a","A")
  a.add("a") // duplicate element exception
  a union StrictSet("a", "b") // StrictSet("a","A","b")
  a concat StrictSet("a", "b") // duplicate element exception
  
  a.map(_.toUpperCase) // duplicate element exception
  StrictSet("a", "a") // duplicate element exception

See Api for short summary of what is in there.

Take a look at TODO for list of improvements pending.

About

Scala Set/Map improvements

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages