Last active
February 23, 2022 18:55
-
-
Save jseteny/da05ea9c55a192909475613049ab90a9 to your computer and use it in GitHub Desktop.
Handmade Shapeless. Naive, but functional and still succinct.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait SList[T] { | |
def ::[H](head: H): ::[H, T] = new ::(head, this) | |
} | |
type SNil = Nothing | |
case object SNil extends SList[SNil] | |
case class ::[H, T](head: H, tail: SList[T]) extends SList[H :: T] { | |
override def toString = s"$head :: $tail" | |
} | |
println( | |
"A" :: 1 :: SNil | |
) | |
List( | |
"A" :: 1 :: SNil, | |
"B" :: 2 :: 3 :: SNil, | |
"C" :: "D" :: SNil | |
).foreach(slist => slist match { | |
case (s: String) :: (i: Int) :: _ => | |
println(s"string: $s, than int: $i") | |
case a :: (b: String) :: _ => | |
println(s"something: $a than string: $b") | |
}) | |
case class Example(ds: Double :: String :: SNil) | |
val e = Example(2.72 :: "e" :: SNil) | |
println(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the future I may implement a CSV converter using the CSV converter example from the original Shapeless:
https://github.com/milessabin/shapeless/blob/main/examples/src/main/scala/shapeless/examples/csv.scala
(This example shows how to createa a serializer/deserializer from CSV to products.)