Skip to content

Instantly share code, notes, and snippets.

Last active Dec 28, 2018
What would you like to do?
Sample collection implementation with Scala 2.13
package com.tiqwab.example.step4
import scala.collection.mutable.ListBuffer
import scala.collection.{immutable, mutable, SeqFactory, StrictOptimizedLinearSeqOps}
class MyList[+A] private (elems: List[A])
extends immutable.LinearSeq[A] with immutable.LinearSeqOps[A, MyList, MyList[A]]
with StrictOptimizedLinearSeqOps[A, MyList, MyList[A]] {
// To be overridden in implementations, as said in scala.collection.LinearSeq
override def isEmpty: Boolean = elems.isEmpty
override def head: A = elems.head
override def tail: MyList[A] = new MyList(elems.tail)
override def iterator: Iterator[A] = elems.iterator
// To be overridden for LinearSeqOps
override def iterableFactory: SeqFactory[MyList] = MyList
override def toString: String = s"MyList(${elems.mkString(",")})"
object MyList extends SeqFactory[MyList] {
override def from[A](source: IterableOnce[A]): MyList[A] =
override def empty[A]: MyList[A] =
new MyList(List.empty)
override def newBuilder[A]: mutable.Builder[A, MyList[A]] =
new ListBuffer[A].mapResult(elems => new MyList(elems))
package com.tiqwab.example.step4
import scala.collection.mutable.ListBuffer
import scala.collection.{immutable, mutable, MapFactory, StrictOptimizedMapOps}
class MyMap[K, +V] private (elems: Map[K, V])
extends immutable.Map[K, V] with immutable.MapOps[K, V, MyMap, MyMap[K, V]]
with StrictOptimizedMapOps[K, V, MyMap, MyMap[K, V]] {
// Should be implemented for immutable.Map
override def remove(key: K): MyMap[K, V] =
new MyMap(elems.remove(key))
override def updated[V1 >: V](key: K, value: V1): MyMap[K, V1] =
new MyMap(elems.updated(key, value))
override def get(key: K): Option[V] =
override def iterator: Iterator[(K, V)] =
override def mapFactory: MapFactory[MapCC] = MyMap
override def toString: String = s"MyMap(${elems.mkString(",")})"
object MyMap extends MapFactory[MyMap] {
override def from[K, V](it: IterableOnce[(K, V)]): MyMap[K, V] = newBuilder[K, V].addAll(it).result()
override def empty[K, V]: MyMap[K, V] = new MyMap(Map.empty)
override def newBuilder[K, V]: mutable.Builder[(K, V), MyMap[K, V]] =
new ListBuffer[(K, V)]().mapResult(elems => new MyMap(elems.toMap))
package com.tiqwab.example.step4
import scala.collection.mutable.ListBuffer
import scala.collection.{immutable, mutable, IterableFactory, StrictOptimizedSetOps}
class MySet[A] private (elems: Set[A])
extends immutable.Set[A] with immutable.SetOps[A, MySet, MySet[A]] with StrictOptimizedSetOps[A, MySet, MySet[A]] {
// Should be implemented for immutable.Set
override def incl(elem: A): MySet[A] = new MySet(elems.incl(elem))
override def excl(elem: A): MySet[A] = new MySet(elems.excl(elem))
override def contains(elem: A): Boolean = elems.contains(elem)
override def iterator: Iterator[A] = elems.iterator
override def iterableFactory: IterableFactory[MySet] = MySet
override def toString: String = s"MySet(${elems.mkString(",")})"
object MySet extends IterableFactory[MySet] {
override def from[A](source: IterableOnce[A]): MySet[A] = newBuilder[A].addAll(source).result()
override def empty[A]: MySet[A] = new MySet(Set.empty)
override def newBuilder[A]: mutable.Builder[A, MySet[A]] =
new ListBuffer[A]().mapResult(elems => new MySet(elems.toSet))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment