package nonempty
- Alphabetic
- By Inheritance
- nonempty
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final class NEList[+T] extends AnyVal
- trait NEListInstances extends AnyRef
- final class NEMap[K, +V] extends AnyVal
A non-empty map: an ordered, immutable, non-empty collection of key-value tuples with
LinearSeqperformance characteristics.A non-empty map: an ordered, immutable, non-empty collection of key-value tuples with
LinearSeqperformance characteristics.The purpose of
NEMapis to allow you to express in a type that aMapis non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeqperformance, seeVector.Constructing
NEMapsYou can construct a
NEMapby passing one or more elements to theNEMap.applyfactory method:scala> NEMap(1 -> "one", 2 -> "two", 3 -> "three") res0: org.scalactic.anyvals.NEMap[Int, String] = NEMap(1 -> "one", 2 -> "two", 3 -> "three")
Working with
NEMapsNEMapdoes not extend Scala'sMaporTraversabletraits because these require that implementations may be empty. For example, if you invoketailon aSeqthat contains just one element, you'll get an emptySeq:scala> Map(1 -> "one").tail res6: Map[Int] = Map()
On the other hand, many useful methods exist on
Mapthat when invoked on a non-emptySeqare guaranteed to not result in an emptyMap. For convenience,NEMapdefines a method corresponding to every suchMapmethod. Here are an example:NEMap(1 -> "one", 2 -> "two", 3 -> "three").map(t => (t._1 + 1, t._2)) // Result: NEMap(2 -> "one", 3 -> "two", 4 -> "three")
NEMapdoes not currently define any methods corresponding toMapmethods that could result in an emptyMap. However, an implicit converison fromNEMaptoMapis defined in theNEMapcompanion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilteron anNEMap, even thoughfiltercould result in an empty map—but the result type will beMapinstead ofNEMap:NEMap(1 -> "one", 2 -> "two", 3 -> "three").filter(_._1 < 10) // Result: Map(1 -> "one", 2 -> "two", 3 -> "three") NEMap(1 -> "one", 2 -> "two", 3 -> "three").filter(_._ 1> 10) // Result: Map()
You can use
NEMaps inforexpressions. The result will be anNEMapunless you use a filter (anifclause). Because filters are desugared to invocations offilter, the result type will switch to aMapat that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for ((i, j) <- NEMap(1 -> "one", 2 -> "two", 3 -> "three")) yield (i + 1, j) res0: org.scalactic.anyvals.NEMap[Int, String] = NEMap(2 -> "one", 3 -> "two", 4 -> "three") scala> for ((i, j) <- NEMap(1, 2, 3) if i < 10) yield (i + 1, j) res1: Map[Int, String] = Map(2 -> "one", 3 -> "two", 4 -> "three")
- K
the type of key contained in this
NEMap- V
the type of value contained in this
NEMap
- final class NESet[T] extends AnyVal
A non-empty Set: an ordered, immutable, non-empty collection of elements with
LinearSeqperformance characteristics.A non-empty Set: an ordered, immutable, non-empty collection of elements with
LinearSeqperformance characteristics.The purpose of
NESetis to allow you to express in a type that aSetis non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeqperformance, seeVector.Constructing
NESetsYou can construct a
NESetby passing one or more elements to theNESet.applyfactory method:scala> NESet(1, 2, 3) res0: org.scalactic.anyvals.NESet[Int] = NESet(1, 2, 3)
Alternatively you can cons elements onto the
Endsingleton object, similar to making aSetstarting withNil:scala> 1 :: 2 :: 3 :: Nil res0: Set[Int] = Set(1, 2, 3) scala> 1 :: 2 :: 3 :: End res1: org.scalactic.NESet[Int] = NESet(1, 2, 3)
Note that although
Nilis aSet[Nothing],Endis not aNESet[Nothing], because no emptyNESetexists. (A non-empty Set is a series of connected links; if you have no links, you have no non-empty Set.)scala> val nil: Set[Nothing] = Nil nil: Set[Nothing] = Set() scala> val nada: NESet[Nothing] = End <console>:16: error: type mismatch; found : org.scalactic.anyvals.End.type required: org.scalactic.anyvals.NESet[Nothing] val nada: NESet[Nothing] = End ^Working with
NESetsNESetdoes not extend Scala'sSeqorTraversabletraits because these require that implementations may be empty. For example, if you invoketailon aSeqthat contains just one element, you'll get an emptySeq:scala> Set(1).tail res6: Set[Int] = Set()
On the other hand, many useful methods exist on
Seqthat when invoked on a non-emptySeqare guaranteed to not result in an emptySeq. For convenience,NESetdefines a method corresponding to every suchSeqmethod. Here are some examples:NESet(1, 2, 3).map(_ + 1) // Result: NESet(2, 3, 4) NESet(1).map(_ + 1) // Result: NESet(2) NESet(1, 2, 3).containsSlice(NESet(2, 3)) // Result: true NESet(1, 2, 3).containsSlice(NESet(3, 4)) // Result: false NESet(-1, -2, 3, 4, 5).minBy(_.abs) // Result: -1
NESetdoes not currently define any methods corresponding toSeqmethods that could result in an emptySeq. However, an implicit converison fromNESettoSetis defined in theNESetcompanion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilteron anNESet, even thoughfiltercould result in an empty sequence—but the result type will beSetinstead ofNESet:NESet(1, 2, 3).filter(_ < 10) // Result: Set(1, 2, 3) NESet(1, 2, 3).filter(_ > 10) // Result: Set()
You can use
NESets inforexpressions. The result will be anNESetunless you use a filter (anifclause). Because filters are desugared to invocations offilter, the result type will switch to aSetat that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for (i <- NESet(1, 2, 3)) yield i + 1 res0: org.scalactic.anyvals.NESet[Int] = NESet(2, 3, 4) scala> for (i <- NESet(1, 2, 3) if i < 10) yield i + 1 res1: Set[Int] = Set(2, 3, 4) scala> for { | i <- NESet(1, 2, 3) | j <- NESet('a', 'b', 'c') | } yield (i, j) res3: org.scalactic.anyvals.NESet[(Int, Char)] = NESet((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)) scala> for { | i <- NESet(1, 2, 3) if i < 10 | j <- NESet('a', 'b', 'c') | } yield (i, j) res6: Set[(Int, Char)] = Set((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))- T
the type of elements contained in this
NESet
- trait NESetInstances extends AnyRef
- final class NEString extends AnyVal
A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeqperformance characteristics.A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeqperformance characteristics.The purpose of
NEStringis to allow you to express in a type that aStringis non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeqperformance, seeVector.Constructing
NEStringsYou can construct a
NEStringby passing one or more elements to theNEString.applyfactory method:scala> NEString(1, 2, 3) res0: org.scalactic.anyvals.NEString[Int] = NEString(1, 2, 3)
Alternatively you can cons elements onto the
Endsingleton object, similar to making aStringstarting withNil:scala> 1 :: 2 :: 3 :: Nil res0: String[Int] = String(1, 2, 3) scala> 1 :: 2 :: 3 :: End res1: org.scalactic.NEString[Int] = NEString(1, 2, 3)
Note that although
Nilis aString[Nothing],Endis not aNEString[Nothing], because no emptyNEStringexists. (A non-empty list is a series of connected links; if you have no links, you have no non-empty list.)scala> val nil: String[Nothing] = Nil nil: String[Nothing] = String() scala> val nada: NEString[Nothing] = End <console>:16: error: type mismatch; found : org.scalactic.anyvals.End.type required: org.scalactic.anyvals.NEString[Nothing] val nada: NEString[Nothing] = End ^Working with
NEStringsNEStringdoes not extend Scala'sSeqorTraversabletraits because these require that implementations may be empty. For example, if you invoketailon aSeqthat contains just one element, you'll get an emptySeq:scala> String(1).tail res6: String[Int] = String()
On the other hand, many useful methods exist on
Seqthat when invoked on a non-emptySeqare guaranteed to not result in an emptySeq. For convenience,NEStringdefines a method corresponding to every suchSeqmethod. Here are some examples:NEString(1, 2, 3).map(_ + 1) // Result: NEString(2, 3, 4) NEString(1).map(_ + 1) // Result: NEString(2) NEString(1, 2, 3).containsSlice(NEString(2, 3)) // Result: true NEString(1, 2, 3).containsSlice(NEString(3, 4)) // Result: false NEString(-1, -2, 3, 4, 5).minBy(_.abs) // Result: -1
NEStringdoes not currently define any methods corresponding toSeqmethods that could result in an emptySeq. However, an implicit converison fromNEStringtoStringis defined in theNEStringcompanion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilteron anNEString, even thoughfiltercould result in an empty sequence—but the result type will beStringinstead ofNEString:NEString(1, 2, 3).filter(_ < 10) // Result: String(1, 2, 3) NEString(1, 2, 3).filter(_ > 10) // Result: String()
You can use
NEStrings inforexpressions. The result will be anNEStringunless you use a filter (anifclause). Because filters are desugared to invocations offilter, the result type will switch to aStringat that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for (i <- NEString(1, 2, 3)) yield i + 1 res0: org.scalactic.anyvals.NEString[Int] = NEString(2, 3, 4) scala> for (i <- NEString(1, 2, 3) if i < 10) yield i + 1 res1: String[Int] = String(2, 3, 4) scala> for { | i <- NEString(1, 2, 3) | j <- NEString('a', 'b', 'c') | } yield (i, j) res3: org.scalactic.anyvals.NEString[(Int, Char)] = NEString((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)) scala> for { | i <- NEString(1, 2, 3) if i < 10 | j <- NEString('a', 'b', 'c') | } yield (i, j) res6: String[(Int, Char)] = String((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
Deprecated Type Members
- type NonEmptyList[+T] = NEList[T]
- Annotations
- @deprecated
- Deprecated
NonEmptyList had been renamed to NEList
- type NonEmptyMap[K, +V] = NEMap[K, V]
- Annotations
- @deprecated
- Deprecated
NonEmptyMap had been renamed to NEMap
- type NonEmptySet[T] = NESet[T]
- Annotations
- @deprecated
- Deprecated
NonEmptySet had been renamed to NESet
- type NonEmptyString = NEString
- Annotations
- @deprecated
- Deprecated
NonEmptyString had been renamed to NEString
Value Members
- object NEList extends NEListInstances
Companion object for class
NEList. - object NEMap
Companion object for class
NEMap. - object NESet extends NESetInstances
Companion object for class
NESet. - object NEString
Companion object for class
NEString.
Deprecated Value Members
- final val NonEmptyList: NEList.type
- Annotations
- @deprecated
- Deprecated
NonEmptyList had been renamed to NEList
- final val NonEmptyMap: NEMap.type
- Annotations
- @deprecated
- Deprecated
NonEmptyMap had been renamed to NEMap
- final val NonEmptySet: NESet.type
- Annotations
- @deprecated
- Deprecated
NonEmptySet had been renamed to NESet
- final val NonEmptyString: NEString.type
- Annotations
- @deprecated
- Deprecated
NonEmptyString had been renamed to NEString