-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd34986
commit f5eb7ef
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) |