Skip to content

Instantly share code, notes, and snippets.

@linasm
Last active March 15, 2021 06:17

Revisions

  1. linasm revised this gist Jan 7, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions unapply.scala
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ object FullName {
    def apply(first: String, last: String): FullName =
    new FullName(first, last)

    def unapply(full: FullName): Option[(String, String)] =
    def unapply(full: FullName): Some[(String, String)] =
    Some((full.first, full.last))
    }

    @@ -15,24 +15,24 @@ val me = FullName("Linas", "Medžiūnas")
    val FullName(meFirst, meLast) = me

    object DateTime {
    def unapply(dt: LocalDateTime): Option[(LocalDate, LocalTime)] =
    def unapply(dt: LocalDateTime): Some[(LocalDate, LocalTime)] =
    Some((dt.toLocalDate, dt.toLocalTime))
    }

    object DateTimeSeq {
    def unapplySeq(dt: LocalDateTime): Option[Seq[Int]] =
    def unapplySeq(dt: LocalDateTime): Some[Seq[Int]] =
    Some(Seq(
    dt.getYear, dt.getMonthValue, dt.getDayOfMonth,
    dt.getHour, dt.getMinute, dt.getSecond))
    }

    object Date {
    def unapply(d: LocalDate): Option[(Int, Int, Int)] =
    def unapply(d: LocalDate): Some[(Int, Int, Int)] =
    Some((d.getYear, d.getMonthValue, d.getDayOfMonth))
    }

    object Time {
    def unapply(t: LocalTime): Option[(Int, Int, Int)] =
    def unapply(t: LocalTime): Some[(Int, Int, Int)] =
    Some((t.getHour, t.getMinute, t.getSecond))
    }

  2. linasm created this gist May 4, 2018.
    65 changes: 65 additions & 0 deletions unapply.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    import java.time.{LocalDate, LocalDateTime, LocalTime}

    /*case */class FullName(val first: String, val last: String)

    object FullName {
    def apply(first: String, last: String): FullName =
    new FullName(first, last)

    def unapply(full: FullName): Option[(String, String)] =
    Some((full.first, full.last))
    }

    val me = FullName("Linas", "Medžiūnas")

    val FullName(meFirst, meLast) = me

    object DateTime {
    def unapply(dt: LocalDateTime): Option[(LocalDate, LocalTime)] =
    Some((dt.toLocalDate, dt.toLocalTime))
    }

    object DateTimeSeq {
    def unapplySeq(dt: LocalDateTime): Option[Seq[Int]] =
    Some(Seq(
    dt.getYear, dt.getMonthValue, dt.getDayOfMonth,
    dt.getHour, dt.getMinute, dt.getSecond))
    }

    object Date {
    def unapply(d: LocalDate): Option[(Int, Int, Int)] =
    Some((d.getYear, d.getMonthValue, d.getDayOfMonth))
    }

    object Time {
    def unapply(t: LocalTime): Option[(Int, Int, Int)] =
    Some((t.getHour, t.getMinute, t.getSecond))
    }

    object AM {
    def unapply(t: LocalTime): Option[(Int, Int, Int)] =
    t match {
    case Time(h, m, s) if h < 12 => Some((h, m, s))
    case _ => None
    }
    }

    object PM {
    def unapply(t: LocalTime): Option[(Int, Int, Int)] =
    t match {
    case Time(12, m, s) => Some(12, m, s)
    case Time(h, m, s) if h > 12 => Some(h - 12, m, s)
    case _ => None
    }
    }

    val now = LocalDateTime.now

    val dt @ DateTime(date @ Date(y, m, d), time @ Time(h, mm, s)) = now

    val DateTimeSeq(y2, m2, d2, h2, _*) = now

    Seq(LocalTime.now) map {
    case t @ AM(h, m, _) => f"$h%2d:$m%02d AM ($t precisely)"
    case t @ PM(h, m, _) => f"$h%2d:$m%02d PM ($t precisely)"
    }