{"id":15159356,"url":"https://github.com/cmhteixeira/typed-list","last_synced_at":"2025-08-09T16:16:00.198Z","repository":{"id":49112872,"uuid":"173616235","full_name":"cmhteixeira/typed-list","owner":"cmhteixeira","description":"A linked list with compile time size.","archived":false,"fork":false,"pushed_at":"2021-08-18T19:53:23.000Z","size":143,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-31T03:01:31.870Z","etag":null,"topics":["compile-time","linked-list","scala","type-level-programming"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cmhteixeira.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-03T18:52:26.000Z","updated_at":"2024-08-02T07:40:54.000Z","dependencies_parsed_at":"2022-09-10T14:40:20.048Z","dependency_job_id":null,"html_url":"https://github.com/cmhteixeira/typed-list","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmhteixeira%2Ftyped-list","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmhteixeira%2Ftyped-list/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmhteixeira%2Ftyped-list/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cmhteixeira%2Ftyped-list/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cmhteixeira","download_url":"https://codeload.github.com/cmhteixeira/typed-list/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237999515,"owners_count":19399897,"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":["compile-time","linked-list","scala","type-level-programming"],"created_at":"2024-09-26T21:05:15.351Z","updated_at":"2025-02-09T18:31:02.424Z","avatar_url":"https://github.com/cmhteixeira.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Typed List \u0026emsp; [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.cmhteixeira/typed-list_2.13/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.cmhteixeira/typed-list_2.13) [![Build Status](https://www.travis-ci.com/cmhteixeira/typed-list.svg?branch=master)](https://www.travis-ci.com/cmhteixeira/typed-list)\n\nA linked list with compile time size.\n\n**Found the project interesting?** \u0026rightarrow;  Then drop a :star:. I am needing the encouragement. ;)  \n**Found a problem?** \u0026rightarrow; Open an issue, and I will look into it ASAP.  \n**Want to contribute?** \u0026rightarrow; If you found a bug and want to fix it yourself open an MR.  I welcome contributions.\n\n---\n**📝 NOTE - No large lists.**\n\nIn practise, this library will only work for relatively small lists. (I couldn't go past 400 elements).  \nThe problem is that it relies on the peano encoding of the natural numbers to express the size of the lists. This is very heavy on the compiler and it simply gives up.  \nI think there is a presentation somewhere of Miles Sabin reporting the same issue in his Shapeless library.\n\n---\n\n## Usage\n\nCreate a typed list the same way you would a standard list:\n```scala\nimport com.cmhteixeira.typedlist.TypedNil\nval aListOfSizeEight = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: TypedNil\n\nval standardList = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: Nil\n```\nYou can also use a standard list by specifying the size you expect. The result is wrapped in an `Option`.\n```scala\nimport com.cmhteixeira.typedlist.TypedList\nimport com.cmhteixeira.typedlist.naturalnumbers.Natural.{Nat10, Nat5}\n\nval standardList= (1 to 50).toList\nval typedList = TypedList.fromList[Nat10#Mult[Nat5], Int](standardList)\n```\n\nBecause the size is known at compile time, you cannot access the head or tail of an empty list:\n```scala\nval oneElement = \"Foo\" :: TypedNil\noneElement.tail.head // does not compile\noneElement.tail.tail // does not compile\n```\n\nYou can obtain an element at a given index:\n```scala\nimport com.cmhteixeira.typedlist.TypedNil\nimport com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat2\nval typedList = \"Foo\" :: \"Bar\" :: \"Baz\" :: TypedNil\ntypedList.get[Nat2]\nres\u003e \"Bar\"  \n```\nBut you cannot obtain an element at an index greater that the size of the list:\n```scala\nimport com.cmhteixeira.typedlist.TypedNil\nimport com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat4\nval typedList = \"Foo\" :: \"Bar\" :: \"Baz\" :: TypedNil\ntypedList.get[Nat4] // does not compile\n```\n\nYou can split two lists at a given index, and the resulting lists will maintain type information. That is, their size will still be known:\n```scala\nimport com.cmhteixeira.typedlist.TypedNil\nimport com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat3\nval typedList = \"Foo\" :: \"Bar\" :: \"Baz\" :: \"Qux\" :: \"Quux\" :: TypedNil\nval (l, r) = typedList.split[Nat3]\nres\u003e \nval l: TypedList[Suc[Suc[Suc[Zero.type]]], String] = TypedList(\"Foo\", \"Bar\", \"Baz\")\nval r: TypedList[Suc[Suc[Zero.type]], String] = TypedList(\"Qux\", \"Quux\")\n```\n\nYou can also map over the list, and the resulting list will maintain its size:\n```scala\nimport com.cmhteixeira.typedlist.TypedNil\nval typedList = \"Bar\" :: \"Baz\" :: TypedNil\nval stringList = aListOfSizeEight.map(_.length)\nres\u003e \nval stringList = TypedList(3, 3)\n```\n\nYou can concatenate two lists and the resulting list will still have the correct typed size; which in fact will be a summation of the sizes of the two lists being concatenated:\n```scala\nimport com.cmhteixeira.typedlist.TypedNil\nval firstList = \"Foo\" :: \"Bar\" :: TypedNil\nval secondList = \"Baz\" :: TypedNil\nval concatenatedList = firstList concat secondList\nconcatenatedList.tail.tail.tail.head // will not compile\nconcatenatedList.tail.tail.tail.tail // will not compile\nres\u003e\nval concatenatedList = TypedList(Foo, Bar, Baz)\n```\n\nLastly, you can flatmap a list, and the resulting list will have the correct size which will still be known at compile time. The drawback is that the function that is applied to each element of the original list must return a new list with a constant size. That is, the returning size cannot vary across the elements of the original list:\n\n```scala\nimport com.cmhteixeira.typedlist.TypedNil\nimport com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat6\n\nval someList = \"Foo\" :: \"Bar\" :: \"Baz\" :: TypedNil\nval result = someList.flatMap(i =\u003e s\"$i-1\" :: s\"$i-2\" :: TypedNil)\nres\u003e \nval result: TypedList[Nat6, String] = TypedList(Foo-1, Foo-2, Bar-1, Bar-2, Baz-1, Baz-2)\n```\n\n### Cats support\n\nA Typed list has a `Traverse` instance. Which also makes it a `Functor` and a `Foldable`.  \nYou **don't need** to import any implicits to have it working:  \n```scala\nimport cats.syntax.traverse._\nimport cats.implicits.catsStdInstancesForOption\nimport com.cmhteixeira.typedlist.TypedNil\n\nval list =  1 :: 2 :: 3 :: 4 :: TypedNil\n\nlist.traverse[Option, Int](i =\u003e Some(i))\nres\u003e Some(TypedList(1, 2, 3, 4))\n\nlist.traverse[Option, Int](i =\u003e if (i == 2) None else Some(i))\nres\u003e None\n```\n\n## Support\n\nThe artefacts have been uploaded to Maven Central.\n\n| Library Version | Scala 2.11 | Scala 2.12 | Scala 2.13 |\n|---------|------------|------------|------------|\n| 2.0.0   | [![Maven Central](https://img.shields.io/maven-central/v/com.cmhteixeira/typed-list_2.11/2.0.0)](https://search.maven.org/artifact/com.cmhteixeira/typed-list_2.11/2.0.0/jar) | [![Maven Central](https://img.shields.io/maven-central/v/com.cmhteixeira/typed-list_2.12/2.0.0)](https://search.maven.org/artifact/com.cmhteixeira/typed-list_2.12/2.0.0/jar) | [![Maven Central](https://img.shields.io/maven-central/v/com.cmhteixeira/typed-list_2.13/2.0.0)](https://search.maven.org/artifact/com.cmhteixeira/typed-list_2.13/2.0.0/jar) |\n| 1.0.0   | | [![Maven Central](https://img.shields.io/maven-central/v/com.cmhteixeira/typed-list_2.12/1.0.0)](https://search.maven.org/artifact/com.cmhteixeira/typed-list_2.12/1.0.0/jar)        | |\n| 0.1   | | [![Maven Central](https://img.shields.io/maven-central/v/com.cmhteixeira/typed-list_2.12/0.1)](https://search.maven.org/artifact/com.cmhteixeira/typed-list_2.12/0.1/jar)        | |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmhteixeira%2Ftyped-list","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcmhteixeira%2Ftyped-list","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcmhteixeira%2Ftyped-list/lists"}