- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
processing Nil versus non-Nil lists
Sun, 2012-02-05, 15:39
I am curious to see how people would recommend coding the following case where if the list is empty do one thing and if it isn't do another.
//val conditions = Nilval conditions = List("XYZ = '123'", "ABC = DEF")
val whereClause = conditions match { case Nil => None case _ => Some("where " + conditions.mkString("and"))}
Note later this is done
val sql = (selectClause ++ whereClause ++ groupByClause).mkString(" ")
Any recommendations? Is there a simple way to turn an empty list into a None and the contents of a non-empty list into a Some(elements)
thanks,Glen
//val conditions = Nilval conditions = List("XYZ = '123'", "ABC = DEF")
val whereClause = conditions match { case Nil => None case _ => Some("where " + conditions.mkString("and"))}
Note later this is done
val sql = (selectClause ++ whereClause ++ groupByClause).mkString(" ")
Any recommendations? Is there a simple way to turn an empty list into a None and the contents of a non-empty list into a Some(elements)
thanks,Glen
Sun, 2012-02-05, 16:41
#2
Re: processing Nil versus non-Nil lists
Thanks Rex. I agree the when isn't dramatically better but if I pimp a List and add toOption similar to the other toXXXX methods that is what I was looking for. I am not trying to make the exact code better so much as trying to make a DSL, that has code "like" that littered throughout, better.
//the following could be genericized to anything that has an isEmpty: Boolean methodimplicit def ToOptionPimping[T](list: List[T]) = new { def toOption: Option[List[T]] = if ( list.isEmpty ) None else Some(list) def optMap[U](code: List[T] =>U): Option[U] = this.toOption.map(code) // to add some brevity to the common use case}
val conditions = List("XYZ = '123'", "ABC = DEF") val whereClause = conditions optMap ("where " + _.mkString(" and "))
gives ==> whereClause: Option[java.lang.String] = Some(where XYZ = '123' and ABC = DEF)
via a REPL session
scala> implicit def ToOptionPimping[T](list: List[T]) = new { | def toOption: Option[List[T]] = if ( list.isEmpty ) None else Some(list) | def optMap[U](code: List[T] =>U): Option[U] = this.toOption.map(code) // to add some brevity to the common use case | }ToOptionPimping: [T](list: List[T])java.lang.Object{def toOption: Option[List[T]]; def optMap[U](code: List[T] => U): Option[U]}
scala> val conditions = List("XYZ = '123'", "ABC = DEF")conditions: List[java.lang.String] = List(XYZ = '123', ABC = DEF)
scala> val whereClause = conditions optMap ("where " + _.mkString(" and ")) whereClause: Option[java.lang.String] = Some(where XYZ = '123' and ABC = DEF)
Not sure the name optMap fits but the concept is what I am after. I can ask the dsl users what name they want.
On Sun, Feb 5, 2012 at 10:03 AM, Rex Kerr <ichoran@gmail.com> wrote:
Well, aside from the obvious answer of "use an if-statement", I do have a method
def when(f: A=>Boolean) = if (f(a)) Some(a) else None
that I have "enriched" onto every object via implicits. So I might
conditions.when(!_.isEmpty)
to get an Option[List[String]] in a case where I wanted to convert an empty list into a None.
But for the use case you show below, the way it's done is already sensible.
val whereClause = conditions.when(!_.isEmpty).map("where" + _.mkString("and"))
does not strike me as dramatically better than the match statement you've got.
--Rex
On Sun, Feb 5, 2012 at 9:39 AM, Glen Marchesani <glen@model3.net> wrote:I am curious to see how people would recommend coding the following case where if the list is empty do one thing and if it isn't do another.
//val conditions = Nilval conditions = List("XYZ = '123'", "ABC = DEF")
val whereClause = conditions match { case Nil => None case _ => Some("where " + conditions.mkString("and"))}
Note later this is done
val sql = (selectClause ++ whereClause ++ groupByClause).mkString(" ")
Any recommendations? Is there a simple way to turn an empty list into a None and the contents of a non-empty list into a Some(elements)
thanks,Glen
Sun, 2012-02-05, 18:31
#3
Re: processing Nil versus non-Nil lists
On Sun, Feb 5, 2012 at 12:39, Glen Marchesani wrote:
> I am curious to see how people would recommend coding the following case
> where if the list is empty do one thing and if it isn't do another.
>
> //val conditions = Nil
> val conditions = List("XYZ = '123'", "ABC = DEF")
>
> val whereClause = conditions match {
> case Nil => None
> case _ => Some("where " + conditions.mkString("and"))
> }
>
> Note later this is done
>
> val sql = (selectClause ++ whereClause ++ groupByClause).mkString(" ")
>
> Any recommendations? Is there a simple way to turn an empty list into a
> None and the contents of a non-empty list into a Some(elements)
You could start with this as a basis:
scala> def f[T](l: List[T]) = PartialFunction.condOpt(l) { case _ :: _
=> l.mkString }
f: [T](l: List[T])Option[String]
scala> f(List(1, 2, 3))
res0: Option[String] = Some(123)
scala> f(Nil)
res1: Option[String] = None
def when(f: A=>Boolean) = if (f(a)) Some(a) else None
that I have "enriched" onto every object via implicits. So I might
conditions.when(!_.isEmpty)
to get an Option[List[String]] in a case where I wanted to convert an empty list into a None.
But for the use case you show below, the way it's done is already sensible.
val whereClause = conditions.when(!_.isEmpty).map("where" + _.mkString("and"))
does not strike me as dramatically better than the match statement you've got.
--Rex
On Sun, Feb 5, 2012 at 9:39 AM, Glen Marchesani <glen@model3.net> wrote: