# Checking membership of object in set without pattern matching

I'm curious in how to best structure code that checks for membership of an instance of an ADT in a set of elements without using pattern matching.

Given the following ADT, methods and functions, which would be seen as the idiomatic way to check for membership in Scala?

sealed trait Code {
def isEven: Boolean
def isOdd: Boolean
def isOther: Boolean
}
case class Even(num: Int) extends Code {
def isEven = true
def isOdd = false
def isOther = false
}
case class Odd(num: Int) extends Code {
def isEven = false
def isOdd = true
def isOther = false
}

case class Other(num: Double) extends Code {
def isEven: Boolean = false
def isOdd: Boolean = false
def isOther = true
}

object Code {
val EvensBelow5: Seq[Code] = Seq(
Even(0),
Even(2),
Even(4)
)
val EvensAbove5: Seq[Code] = Seq(
Even(6),
Even(8)
)

val OddCodes: Seq[Code] = Seq(
Odd(1),
Odd(3),
Odd(5)
)

val OtherCode: Code = Other(1.1)

def isEvenBelow5(code: Code) = EvensBelow5.contains(code)
def isEvenAbove5(code: Code) = EvensAbove5.contains(code)
def isOdd(code: Code) = OddCodes.contains(code)
def isOther(code: Code) = OtherCode == code
def isOf(code: Code, codes: Seq[Code]) =
codes.contains(code)
}


Setup

import Code._

val even: Code = Even(2)
val odd: Code = Odd(1)
val other: Code = Other(1.1)


1) Would it be using methods?

even.isOdd
odd.isEven
other.isOther


2) Would it be using membership check or equality

EvensBelow5.contains(even)
EvensAbove5.contains(even)
OddCodes.contains(odd)
other == OtherCode


3) Would it be using functions that rely on values on the companion object?

isEvenBelow5(even)
isOdd(odd)
isOther(other)


4) Would it be using functions that receive all values?

isOf(even, EvensBelow5)
isOf(even, EvensAbove5)
isOf(odd, OddCodes)
isOf(other, Seq(OtherCode))


5) Would it be another way?

Maybe something that I have not listed which is not pattern matching.

Bonus question:

Would the functions that rely on values / state on the companion object (isEvenBelow5, isEvenAbove5, isOdd, isOther) be considered purely functional?