Skip to content

Commit

Permalink
✨ add some parsing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
twentylemon committed Dec 17, 2024
1 parent bd34986 commit f5eb7ef
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/scala/org/lemon/advent/lib/parse.scala
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f5eb7ef

Please sign in to comment.