{"id":15679299,"url":"https://github.com/izeigerman/scalanum","last_synced_at":"2025-05-07T09:28:11.690Z","repository":{"id":210934007,"uuid":"98478933","full_name":"izeigerman/scalanum","owner":"izeigerman","description":null,"archived":false,"fork":false,"pushed_at":"2017-07-28T01:50:08.000Z","size":7,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T08:39:25.008Z","etag":null,"topics":["enum","functional-programming","haskell","scala","typeclasses"],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/izeigerman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-07-27T01:07:12.000Z","updated_at":"2019-07-23T20:44:31.000Z","dependencies_parsed_at":"2023-12-05T17:04:08.799Z","dependency_job_id":null,"html_url":"https://github.com/izeigerman/scalanum","commit_stats":null,"previous_names":["izeigerman/scalanum"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izeigerman%2Fscalanum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izeigerman%2Fscalanum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izeigerman%2Fscalanum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izeigerman%2Fscalanum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izeigerman","download_url":"https://codeload.github.com/izeigerman/scalanum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252850320,"owners_count":21813943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","functional-programming","haskell","scala","typeclasses"],"created_at":"2024-10-03T16:28:28.774Z","updated_at":"2025-05-07T09:28:11.661Z","avatar_url":"https://github.com/izeigerman.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Scalanum\n\nScalanum - 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.\n\nFirst we need to prepare the environment:\n```\nsbt clean package\nscala -cp ./scalanum/target/scala-2.11/scalanum_2.11-0.1.jar\n```\n\nFor our example we use the following sum type:\n```scala\nsealed trait Animal\ncase object Cat extends Animal\ncase object Dog extends Animal\ncase object Rabbit extends Animal\ncase object Cow extends Animal\n```\nThe Enum instance for the sum type above might look like following:\n```scala\nimport scalanum._\n\nimplicit val animalEnum = new Enum[Animal] {\n  val animals = Array(Cat, Dog, Rabbit, Cow)\n  override def fromEnum(a: Animal): Int = animals.indexOf(a)\n  override def toEnum(i: Int): Animal = animals(i)\n}\n```\nOr the same but shorter:\n```scala\nimport scalanum._\n\nimplicit val animalEnum = new EnumIndexed[Animal] {\n  override val list: IndexedSeq[Animal] = Array(Cat, Dog, Rabbit, Cow)\n}\n```\nNow let's play with it:\n```scala\nscala\u003e Enum[Animal].succ(Cat)\nres0: Animal = Dog\n\nscala\u003e Enum[Animal].pred(Cow)\nres1: Animal = Rabbit\n```\nScalanum is able to generate collections of enumerated values:\n```scala\nscala\u003e Enum[Animal].fromTo(Cat, Rabbit).foreach(println)\nCat\nDog\nRabbit\n```\nNote: if you use a `EnumIndexed` approach you get upper and lower bounds for your enumeration automatically:\n```scala\nscala\u003e Enum[Animal].from(Dog).foreach(println)\nDog\nRabbit\nCow\n```\nThe 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:\n```scala\nscala\u003e Enum[Char].from('a').take(5).foreach(println)\na\nb\nc\nd\ne\n\nscala\u003e Enum[Int].from(1).filter(_ % 2 == 0).takeWhile(_ \u003c 20).foreach(println)\n2\n4\n6\n8\n10\n12\n14\n16\n18\n```\nWe can also specify a custom progression pattern:\n```scala\nscala\u003e Enum[Char].fromThenTo('a', 'c', 'h').foreach(println)\na\nc\ne\ng\n\nscala\u003e Enum[Int].fromThenTo(1, 5, 20).foreach(println)\n1\n5\n9\n13\n17\n```\nAt last a small real world example:\n```scala\nimport scalanum._\n\nsealed trait TrafficLight\ncase object Green extends TrafficLight\ncase object Yellow extends TrafficLight\ncase object Red extends TrafficLight\n\nimplicit val trafficLightEnum = new EnumIndexed[TrafficLight] {\n  override val list: IndexedSeq[TrafficLight] = Array(Green, Yellow, Red)\n}\n\nscala\u003e Enum[TrafficLight].succ(Green)\nres0: TrafficLight = Yellow\n\nscala\u003e Enum[TrafficLight].succ(Enum[TrafficLight].succ(Green))\nres2: TrafficLight = Red\n\nscala\u003e Enum[TrafficLight].pred(Red)\nres3: TrafficLight = Yellow\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizeigerman%2Fscalanum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizeigerman%2Fscalanum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizeigerman%2Fscalanum/lists"}