Day of the week: Difference between revisions

Content added Content deleted
(Added Bracmat example)
Line 2,164: Line 2,164:
[[Category:Scala Implementations]]
[[Category:Scala Implementations]]
{{libheader|Scala}}
{{libheader|Scala}}
===JDK===
===JDK (discouraged) ===
<lang scala>import java.util.{ Calendar, GregorianCalendar }
<lang scala>import java.util.{ Calendar, GregorianCalendar }
import Calendar.{ DAY_OF_WEEK, DECEMBER, SUNDAY }
import Calendar.{ DAY_OF_WEEK, DECEMBER, SUNDAY }
Line 2,170: Line 2,170:
object DayOfTheWeek extends App {
object DayOfTheWeek extends App {
val years = 2008 to 2121
val years = 2008 to 2121

val yuletide = for {
val yuletide =
year <- years
if (new GregorianCalendar(year, DECEMBER, 25)).get(DAY_OF_WEEK) == SUNDAY
years.filter(year => (new GregorianCalendar(year, DECEMBER, 25)).get(DAY_OF_WEEK) == SUNDAY)

} yield year
// If you want a test: (optional)
assert(yuletide ==
Seq(2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061,
2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118))


println(yuletide.mkString(
println(yuletide.mkString(
s"${yuletide.count(p => true)} Years between ${years.head} and ${years.last}" +
s"${yuletide.length} Years between ${years.head} and ${years.last}" +
" including where Christmas is observed on Sunday:\n", ", ", "."))
" including where Christmas is observed on Sunday:\n", ", ", "."))
}</lang>
}</lang>
===JDK >= 8 (recommended)===
===JDK >= 8 (recommended)===
====Naive programming====
<lang scala>import java.time.{ DayOfWeek, LocalDate }
<lang scala>import java.time.{ DayOfWeek, LocalDate }


Line 2,193: Line 2,198:
" including where Christmas is observed on Sunday:\n", ", ", "."))
" including where Christmas is observed on Sunday:\n", ", ", "."))
}</lang>
}</lang>
===Idiomatic===
====Idiomatic programming====
<lang scala>import java.time.{ DayOfWeek, LocalDate }
<lang scala>import java.time.{ DayOfWeek, LocalDate }


object DayOfTheWeek2 extends App {
object DayOfTheWeek1 extends App {
val years = 2008 to 2121
val years = 2008 to 2121
val yuletide =
val yuletide =
years.filter(yr => LocalDate.of(yr, 12, 25).getDayOfWeek() == DayOfWeek.SUNDAY)
years.filter(year => (LocalDate.of(year, 12, 25).getDayOfWeek() == DayOfWeek.SUNDAY))

// If you want a test: (optional)
assert(yuletide ==
Seq(2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061,
2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118))


println(yuletide.mkString(
println(yuletide.mkString(
s"${yuletide.count(p => true)} Years between ${years.head} and ${years.last}" +
s"${yuletide.length} Years between ${years.head} and ${years.last}" +
" including where Christmas is observed on Sunday:\n", ", ", "."))
" including where Christmas is observed on Sunday:\n", ", ", "."))
}</lang>
}</lang>
===Tail recursion version===
====Tail recursion====
<lang scala>import java.time.{ DayOfWeek, LocalDate }
<lang scala>import java.time.{ DayOfWeek, LocalDate }
import scala.annotation.tailrec
import scala.annotation.tailrec
Line 2,216: Line 2,226:
if (anni == Nil) accu
if (anni == Nil) accu
else inner(anni.tail, accu ++
else inner(anni.tail, accu ++
(if (LocalDate.of(anni.head, 12, 25).getDayOfWeek() == DayOfWeek.SUNDAY)
(if (LocalDate.of(anni.head, 12, 25).getDayOfWeek() == DayOfWeek.SUNDAY) List(anni.head)
List(anni.head) else Nil))
else Nil))
}
}
inner(years.toList, Nil)
inner(years.toList, Nil)
}
}

// If you want a test: (optional)
assert(yuletide ==
Seq(2011, 2016, 2022, 2033, 2039, 2044, 2050, 2061,
2067, 2072, 2078, 2089, 2095, 2101, 2107, 2112, 2118))


println(yuletide.mkString(
println(yuletide.mkString(
s"${yuletide.count(p => true)} Years between ${years.head} and ${years.last}" +
s"${yuletide.length} Years between ${years.head} and ${years.last}" +
" including where Christmas is observed on Sunday:\n", ", ", "."))
" including where Christmas is observed on Sunday:\n", ", ", "."))
}</lang>
}</lang>