Add base structure and library stuff from last year

This commit is contained in:
Alexander Gehrke 2021-12-02 23:34:40 +01:00
commit 9017c4379c
35 changed files with 452 additions and 0 deletions

View file

@ -0,0 +1,23 @@
package de.qwertyuiop.aoc.lib
/* for splitting input with separator lines */
extension [A](input: List[A])(using CanEqual[A,A])
def split(separator: A, keepSeparator: Boolean = false): LazyList[List[A]] =
input.span(_ != separator) match
case (Nil, Nil) => LazyList()
case (h, Nil) => LazyList(h)
case (h, tail @ (_ :: t)) =>
h #:: (if keepSeparator then tail else t).split(separator)
/* Using -Yexplicit-nulls isn't really ready for use with the java standard
* library. e.g. String doesn't have `@NotNull` annotations for its methods
*/
extension (s: String)
def splitOnce(regex: String): Option[(String, String)] =
s.split(regex, 2) match
case Array(a, b) => Some((a.nn, b.nn))
case _ => None
extension [K,V,W](map: Map[K,V])
def mapValuesS(f: V => W): Map[K, W] = map.view.mapValues(f).toMap