From f5eb7ef008aceccbf4c7052154ff5b82d729eddb Mon Sep 17 00:00:00 2001 From: Taras Mychaskiw Date: Tue, 17 Dec 2024 15:33:02 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20some=20parsing=20utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scala/org/lemon/advent/lib/parse.scala | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/scala/org/lemon/advent/lib/parse.scala diff --git a/src/main/scala/org/lemon/advent/lib/parse.scala b/src/main/scala/org/lemon/advent/lib/parse.scala new file mode 100644 index 0000000..7343db7 --- /dev/null +++ b/src/main/scala/org/lemon/advent/lib/parse.scala @@ -0,0 +1,31 @@ +package org.lemon.advent.lib + +object Parse: + object Num: + def unapply[T: Numeric](s: String): Option[T] = summon[Numeric[T]].parseString(s) + + private def splitParse[T: Numeric](s: String, delimeter: String): Option[Seq[T]] = + Some(s.split(delimeter).map(_.trim).toSeq.flatMap(Num.unapply[T])) + + object NumCsv: + def unapply[T: Numeric](s: String): Option[Seq[T]] = splitParse(s, ",") + + object NumWsv: + def unapply[T: Numeric](s: String): Option[Seq[T]] = splitParse(s, "\\s+") + + object Chunk: + def unapply(s: String): Option[Seq[String]] = Some(s.split("\n\n").toSeq) + + object Int: + def unapply(s: String): Option[Int] = Num.unapply(s) + object IntCsv: + def unapply(s: String): Option[Seq[Int]] = NumCsv.unapply[Int](s) + object IntWsv: + def unapply(s: String): Option[Seq[Int]] = NumWsv.unapply[Int](s) + + object Long: + def unapply(s: String): Option[Long] = Num.unapply(s) + object LongCsv: + def unapply(s: String): Option[Seq[Long]] = NumCsv.unapply[Long](s) + object LongWsv: + def unapply(s: String): Option[Seq[Long]] = NumWsv.unapply[Long](s)