Skip to content

Instantly share code, notes, and snippets.

@scottashipp
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottashipp/61ed8ce312a025e47b31 to your computer and use it in GitHub Desktop.
Save scottashipp/61ed8ce312a025e47b31 to your computer and use it in GitHub Desktop.
Alternate Scala collect example
object MemberType extends Enumeration {
type MemberType = Value
val Free, Trial, Pay, Premium = Value
}
object MemberStatus extends Enumeration {
type MemberStatus = Value
val Active, Inactive, Disabled = Value
}
class DataRecord(val id: Long, val status: MemberStatus.Value, val name: String, val memType: MemberType.Value, val accessKey: String)
//.. envision DataRecord actually having 30+ attributes
trait Member {
val name: String
}
object Member {
class Free(val name: String) extends Member
class Pay(val name: String) extends Member
class Premium(val name: String, val accessKey: Option[String]) extends Member
def apply(d: DataRecord): Member = {
if(d.status == MemberStatus.Active && d.memType == MemberType.Premium)
new Premium(d.name, Option(d.accessKey))
else if(d.status == MemberStatus.Active && d.memType == MemberType.Trial || d.memType == MemberType.Pay)
new Pay(d.name)
else
new Free(d.name)
}
}
val pierre = new DataRecord(1, MemberStatus.Inactive, "Pierre", MemberType.Pay, "1111")
val joanne = new DataRecord(2, MemberStatus.Inactive, "Joannne", MemberType.Free, "")
val du = new DataRecord(3, MemberStatus.Active, "Du", MemberType.Premium, "3333")
val petra = new DataRecord(4, MemberStatus.Disabled, "Petra", MemberType.Premium, "4444")
val chin = new DataRecord(5, MemberStatus.Active, "Chin", MemberType.Trial, "5555")
val carlos = new DataRecord(6, MemberStatus.Active, "Carlos", MemberType.Pay, "6666")
val tran = new DataRecord(7, MemberStatus.Active, "Tran", MemberType.Pay, "7777")
val alva = new DataRecord(8, MemberStatus.Active, "Alva", MemberType.Premium, "8888")
val nan = new DataRecord(9, MemberStatus.Active, "Nan", MemberType.Free, "")
val steve = new DataRecord(10, MemberStatus.Active, "Steve", MemberType.Free, "101010")
val allData = List(pierre, joanne, du, petra, chin, carlos, tran, alva, nan, steve)
def getMembers(datas: List[DataRecord], c: Class[_]) = datas map { Member(_) } filter { _.getClass == c }
val payMembers = getMembers(allData, classOf[Member.Pay])
val premiumMembers = getMembers(allData, classOf[Member.Premium])
def greetMembers(memList: List[Member], greeting: String) = for(m <- memList) { println(String.format(greeting, m.name)) }
def premiumGreeting = "Hello, %s! Premium members get the best experience! "
def payGreeting = "Hello, %s! Become a premium member and upgrade your experience!"
greetMembers(premiumMembers, premiumGreeting)
greetMembers(payMembers, payGreeting)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment