All Projects → izeigerman → scalanum

izeigerman / scalanum

Licence: other
No description or website provided.

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to scalanum

Arrow
Λrrow - Functional companion to Kotlin's Standard Library
Stars: ✭ 4,771 (+39658.33%)
Mutual labels:  typeclasses
classes
Smart, pythonic, ad-hoc, typed polymorphism for Python
Stars: ✭ 461 (+3741.67%)
Mutual labels:  typeclasses
play2scalaz
Scalaz typeclasses <~> Playframework2 typeclasses
Stars: ✭ 14 (+16.67%)
Mutual labels:  typeclasses
concise-cheat-sheets
Cheat Sheets for programming languages and tools
Stars: ✭ 98 (+716.67%)
Mutual labels:  typeclasses
no-more-orphans
Companion repository for 'No More Orphans'
Stars: ✭ 12 (+0%)
Mutual labels:  typeclasses
scalaz-deriving
Principled, fast, typeclass derivation.
Stars: ✭ 52 (+333.33%)
Mutual labels:  typeclasses
ninny-json
JSON typeclasses that know the difference between null and absent fields
Stars: ✭ 19 (+58.33%)
Mutual labels:  typeclasses
lectures
Learn Functional Programming in Scala
Stars: ✭ 114 (+850%)
Mutual labels:  typeclasses
AUXify
Introduces macro/meta annotations @ aux, @ self, @ instance, @ apply, @ delegated, @ syntax and String-based type class LabelledGeneric
Stars: ✭ 25 (+108.33%)
Mutual labels:  typeclasses
typeclass-interface-pattern
Ideas, thoughts, and notes on a typeclass/interface based polymorphism pattern for standard C
Stars: ✭ 26 (+116.67%)
Mutual labels:  typeclasses

Scalanum

Scalanum - is the Enum and Bounded type classes implemented in Scala. The implementation is inspired by the corresponding type class in Haskell. These particular type classes are missing in Cats and I don't really want to bring Scalaz to my projects because of this small inconvenience. That's why I came up with my own minimalistic version of Enum and Bounded with zero external dependencies. Below are some useful examples.

First we need to prepare the environment:

sbt clean package
scala -cp ./scalanum/target/scala-2.11/scalanum_2.11-0.1.jar

For our example we use the following sum type:

sealed trait Animal
case object Cat extends Animal
case object Dog extends Animal
case object Rabbit extends Animal
case object Cow extends Animal

The Enum instance for the sum type above might look like following:

import scalanum._

implicit val animalEnum = new Enum[Animal] {
  val animals = Array(Cat, Dog, Rabbit, Cow)
  override def fromEnum(a: Animal): Int = animals.indexOf(a)
  override def toEnum(i: Int): Animal = animals(i)
}

Or the same but shorter:

import scalanum._

implicit val animalEnum = new EnumIndexed[Animal] {
  override val list: IndexedSeq[Animal] = Array(Cat, Dog, Rabbit, Cow)
}

Now let's play with it:

scala> Enum[Animal].succ(Cat)
res0: Animal = Dog

scala> Enum[Animal].pred(Cow)
res1: Animal = Rabbit

Scalanum is able to generate collections of enumerated values:

scala> Enum[Animal].fromTo(Cat, Rabbit).foreach(println)
Cat
Dog
Rabbit

Note: if you use a EnumIndexed approach you get upper and lower bounds for your enumeration automatically:

scala> Enum[Animal].from(Dog).foreach(println)
Dog
Rabbit
Cow

The Enum instance for integers and chars are already included into the library. Due to the lazy nature of generated collections we can produce "infinite" sequences:

scala> Enum[Char].from('a').take(5).foreach(println)
a
b
c
d
e

scala> Enum[Int].from(1).filter(_ % 2 == 0).takeWhile(_ < 20).foreach(println)
2
4
6
8
10
12
14
16
18

We can also specify a custom progression pattern:

scala> Enum[Char].fromThenTo('a', 'c', 'h').foreach(println)
a
c
e
g

scala> Enum[Int].fromThenTo(1, 5, 20).foreach(println)
1
5
9
13
17

At last a small real world example:

import scalanum._

sealed trait TrafficLight
case object Green extends TrafficLight
case object Yellow extends TrafficLight
case object Red extends TrafficLight

implicit val trafficLightEnum = new EnumIndexed[TrafficLight] {
  override val list: IndexedSeq[TrafficLight] = Array(Green, Yellow, Red)
}

scala> Enum[TrafficLight].succ(Green)
res0: TrafficLight = Yellow

scala> Enum[TrafficLight].succ(Enum[TrafficLight].succ(Green))
res2: TrafficLight = Red

scala> Enum[TrafficLight].pred(Red)
res3: TrafficLight = Yellow
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].