Socket
Socket
Sign inDemoInstall

com.cmhteixeira:typed-list_2.13

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

com.cmhteixeira:typed-list_2.13

Linked list with compile time size.


Version published
Maintainers
1
Source

Typed List   Maven Central Build Status

Intro

A linked list with compile time size.

Usage

Create a typed list the same way you would a standard list:

import com.cmhteixeira.typedlist.TypedNil
val aListOfSizeEight = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: TypedNil

val standardList = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: Nil

Because the size is known at compile time, you cannot access the head or tail of an empty list:

val oneElement = "Foo" :: TypedNil
oneElement.tail.head // does not compile
oneElement.tail.tail // does not compile

You can obtain an element at a given index:

import com.cmhteixeira.typedlist.TypedNil
import com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat2
val typedList = "Foo" :: "Bar" :: "Baz" :: TypedNil
typedList.get[Nat2]
res> "Bar"  

But you cannot obtain an element at an index greater that the size of the list:

import com.cmhteixeira.typedlist.TypedNil
import com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat4
val typedList = "Foo" :: "Bar" :: "Baz" :: TypedNil
typedList.get[Nat4] // does not compile

You can split two lists at a given index, and the resulting lists will maintain type information. That is, their size will still be known:

import com.cmhteixeira.typedlist.TypedNil
import com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat3
val typedList = "Foo" :: "Bar" :: "Baz" :: "Qux" :: "Quux" :: TypedNil
val (l, r) = typedList.split[Nat3]
res> 
val l: TypedList[Suc[Suc[Suc[Zero.type]]], String] = TypedList("Foo", "Bar", "Baz")
val r: TypedList[Suc[Suc[Zero.type]], String] = TypedList("Qux", "Quux")

You can also map over the list, and the resulting list will maintain its size:

import com.cmhteixeira.typedlist.TypedNil
val typedList = "Bar" :: "Baz" :: TypedNil
val stringList = aListOfSizeEight.map(_.length)
res> 
val stringList = TypedList(3, 3)

You 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:

import com.cmhteixeira.typedlist.TypedNil
val firstList = "Foo" :: "Bar" :: TypedNil
val secondList = "Baz" :: TypedNil
val concatenatedList = firstList concat secondList
concatenatedList.tail.tail.tail.head // will not compile
concatenatedList.tail.tail.tail.tail // will not compile
res>
val concatenatedList = TypedList(Foo, Bar, Baz)

Lastly, 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:

import com.cmhteixeira.typedlist.TypedNil
import com.cmhteixeira.typedlist.naturalnumbers.Natural.Nat6

val someList = "Foo" :: "Bar" :: "Baz" :: TypedNil
val result = someList.flatMap(i => s"$i-1" :: s"$i-2" :: TypedNil)
res> 
val result: TypedList[Nat6, String] = TypedList(Foo-1, Foo-2, Bar-1, Bar-2, Baz-1, Baz-2)

Support

The artefacts have been uploaded to Maven Central.

Library VersionScala 2.11Scala 2.12Scala 2.13
1.0.0Maven Central
0.1Maven Central

FAQs

Package last updated on 05 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc