(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))
It requires the forward pipe operator, such as the one provided by Scalaz or the one defined here.
(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))
import scala.xml.NodeSeq
abstract class FormatterXML[T] {
def format(input: T): NodeSeq
}
def output[T](what: T)(implicit formatter: FormatterXML[T]) = println(formatter.format(what))
case class Person(name: String, age: Int); object Person {
implicit object Formatter extends FormatterXML[Person] {
def format(input: Person): NodeSeq = <Person><Name>{input.name}</Name><Age>{input.age}</Age></Person>
}
}
output(Person("John", 32))
case class Song(title: String, author: Person); object Song {
implicit object Formatter extends FormatterXML[Song] {
def format(input: Song): NodeSeq = <Song>
<Title>{input.title}</Title>
<Author>{implicitly[FormatterXML[Person]].format(input.author)}</Author>
</Song>
}
}
output(Song("War Pigs", Person("Ozzy Osbourne", 61)))
def ListFormatterXML[T](implicit formatter: FormatterXML[T]) = new FormatterXML[Traversable[T]] {
def format(input: Traversable[T]): NodeSeq = <List>{input map formatter.format}</List>
}
case class Song(title: String, authors: Person*); object Song {
implicit object Formatter extends FormatterXML[Song] {
def format(input: Song): NodeSeq = <Song>
<Title>{input.title}</Title>
<Authors>{ListFormatterXML[Person].format(input.authors)}</Authors>
</Song>
}
}
output(Song("War Pigs",
Person("Tony Iommi", 62),
Person("Ozzy Osbourne", 61),
Person("Geezer Butler", 60),
Person("Bill Ward", 62)))
def ListFormatterXML[T](kind: String)(implicit formatter: FormatterXML[T]) = new FormatterXML[Traversable[T]] {
def format(input: Traversable[T]): NodeSeq = <List>{input map formatter.format}</List>.copy(label = kind)
}
case class Song(title: String, authors: Person*); object Song {
implicit object Formatter extends FormatterXML[Song] {
def format(input: Song): NodeSeq = <Song>
<Title>{input.title}</Title>
{ListFormatterXML[Person]("Authors").format(input.authors)}
</Song>
}
}
output(Song("War Pigs",
Person("Tony Iommi", 62),
Person("Ozzy Osbourne", 61),
Person("Geezer Butler", 60),
Person("Bill Ward", 62)))
abstract class Formatter[T, Format] {
def format(input: T): Format
}
abstract class FormatterXML[T] extends Formatter[T, NodeSeq] {
def format(input: T): NodeSeq
}
def as[F] = new {
def output[T](what: T)(implicit formatter: Formatter[T, F]) = println(formatter.format(what))
}
as[NodeSeq].output(Song("War Pigs",
Person("Tony Iommi", 62),
Person("Ozzy Osbourne", 61),
Person("Geezer Butler", 60),
Person("Bill Ward", 62)))
implicit object PersonFormatterString extends Formatter[Person, String] {
def format(input: Person): String = "%s, age %d" format (input.name, input.age)
}
def ListFormatterString[T](kind: String)(implicit formatter: Formatter[T, String]) = new Formatter[Traversable[T], String] {
def format(input: Traversable[T]): String = input map formatter.format mkString (kind+" ", ", ", "; ")
}
implicit object SongFormatterString extends Formatter[Song, String] {
def format(input: Song): String = input.title+" "+ListFormatterString[Person]("by").format(input.authors)
}
as[String].output(Song("War Pigs",
Person("Tony Iommi", 62),
Person("Ozzy Osbourne", 61),
Person("Geezer Butler", 60),
Person("Bill Ward", 62)))
abstract class Acceptable[T]
object Acceptable {
implicit object IntOk extends Acceptable[Int]
implicit object LongOk extends Acceptable[Long]
}def f[T: Acceptable](t: T) = t
def f[T](t: T)(implicit ev: Acceptable[T]) = t
scala> f(1)
res0: Int = 1
scala> f(1L)
res1: Long = 1
scala> f(1.0)
<console>:8: error: could not find implicit value for parameter ev: Acceptable[Double]
f(1.0)
^
scala> f(1: Byte)
<console>:8: error: could not find implicit value for parameter ev: Acceptable[Byte]
f(1: Byte)
^
def interpolate(text: String, vars: Map[String, String]) =
"""\$\{([^}]+)\}""".r.replaceAllIn(text, (_: scala.util.matching.Regex.Match) match {
case Regex.Groups(v) => vars.getOrElse(v, "")
})
scala> "The ${name} of the ${game}"
res0: java.lang.String = The ${name} of the ${game}
scala> Map("name" -> "X", "game" -> "Y")
res1: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(name -> X, game -> Y)
scala> interpolate(res0, res1)
res2: String = The X of the Y
There's joy to be found in writing code which is solely about what you want, and not about how to get there. Of expressing the fulness of your intent as a single expression. It's like the ultimate perfect katana cut.
Of course, in a real fight I'd rather have a gun.