<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Evolutionnext latest blog entries</title>
    <link>http://www.evolutionnext.com/blog/categories/null</link>
    <description>The latest blog entries from evolutionnext.com</description>
    <item>
      <title>The easy and digestable way to understand view in Scala</title>
      <link>http://www.evolutionnext.com/blog/entry/title/The+easy+and+digestable+way+to+understand+view+in+Scala.html</link>
      <description>&lt;p class="seamTextPara"&gt;
Views can be difficult to understand.  It is presented at times in a way that can make it even more difficult to understand.  In creating a &lt;a href="https://bitbucket.org/dickwall/scala-koans" class="seamTextLink"&gt;Scala Koan &lt;/a&gt;, I decided to make a koan that makes it easy to grok what a view is all about.&#xD;
&lt;/p&gt;
&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
In this example, I create list of Ints 1 through 6 and call it &lt;i class="seamTextEmphasis"&gt;lst&lt;/i&gt;. lst has two maps.  A map takes a function and invokes that function on each element of a collection.&#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
 The first map in my example will print out in stdout what will actually be doubled and then doubles that element.  The second map will print that it is adding 1 to each element then adds 1 to each element.   &#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
The results:&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
scala&amp;gt; val lst = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: Nil&#xD;
scala&amp;gt; lst.map{x =&amp;gt; println(&amp;quot;Doubling %s&amp;quot;.format(x)); x*2}&#xD;
          .map{x =&amp;gt; println(&amp;quot;Adding 1 to %s&amp;quot;.format(x)); x + 1}&#xD;
Doubling 1&#xD;
Doubling 2&#xD;
Doubling 3&#xD;
Doubling 4&#xD;
Doubling 5&#xD;
Doubling 6&#xD;
Adding 1 to 2&#xD;
Adding 1 to 4&#xD;
Adding 1 to 6&#xD;
Adding 1 to 8&#xD;
Adding 1 to 10&#xD;
Adding 1 to 12&#xD;
res1: List[Int] = List(3, 5, 7, 9, 11, 13)&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
What should become blatantly clear is that doing two maps is inefficient. The first map goes through each of items, and the second map will go through each of the items again.  &#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
This is where view comes in.  When you use a view you can add compound functions to a collection before invoking those functions in one fell swoop using the method force.  In other words, you can set up all the functions you want and when you are ready, force the view to calculate your maps (or other functions that you are using on your collection).  &#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
In the following example, I do the same exact thing as I did in the previous example but if you notice I have lst.view.map instead of lst.map  I create a view of the list so that I can add some functions before actually invoking them. &#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
What I get as a result is a SeqView Object.  Once I have the SeqView object, I can then call force and get the same final result as before, except this time you will notice that &lt;q&gt;how&lt;/q&gt; we got there is quite different.  This time instead of doubling all the elements, and then adding all the elements, we interweaved our operations for efficiency, and the list was iterated once instead of twice.&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
scala&amp;gt; lst.view.map{x =&amp;gt; println(&amp;quot;Doubling %s&amp;quot;.format(x)); x*2}&#xD;
               .map{x =&amp;gt; println(&amp;quot;Adding 1 to %s&amp;quot;.format(x)); x + 1}&#xD;
res2: scala.collection.SeqView[Int,Seq[_]] = SeqViewMM(...)&#xD;
&#xD;
scala&amp;gt;res2.force                                                                                                  &#xD;
Doubling 1&#xD;
Adding 1 to 2&#xD;
Doubling 2&#xD;
Adding 1 to 4&#xD;
Doubling 3&#xD;
Adding 1 to 6&#xD;
Doubling 4&#xD;
Adding 1 to 8&#xD;
Doubling 5&#xD;
Adding 1 to 10&#xD;
Doubling 6&#xD;
Adding 1 to 12&#xD;
res3: Seq[Int] = List(3, 5, 7, 9, 11, 13)&#xD;
&lt;/pre&gt;</description>
      <pubDate>Mon, 21 Mar 2011 04:40:11 GMT</pubDate>
      <guid>http://www.evolutionnext.com/blog/entry/title/The+easy+and+digestable+way+to+understand+view+in+Scala.html</guid>
      <dc:date>2011-03-21T04:40:11Z</dc:date>
    </item>
    <item>
      <title>Albuquerque Scala Study Group Funtime Homework and Solutions</title>
      <link>http://www.evolutionnext.com/blog/entry/title/Albuquerque+Scala+Study+Group+Funtime+Homework+and+Solutions.html</link>
      <description>&lt;p class="seamTextPara"&gt;
These were two homework challenges from our Albuquerque Scala Study Group last week.  One was an NPR Sunday Puzzle.&#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
Do this in Scala....&#xD;
&lt;/p&gt;
&#xD;
&lt;ol class="seamTextOrderedList"&gt;
&lt;li class="seamTextOrderedListItem"&gt; Write down the digits from 2 to 7, in order. Add two mathematical symbols to get an expression equaling 2010. What symbols are these? (Figure it out in Scala)&lt;/li&gt;&#xD;
&lt;li class="seamTextOrderedListItem"&gt; 01/02/2010 was a palindrome date and it was awesome!  In Scala, figure out the next dates that will be a palindrome, and give me the results in a Stream[Date]. &lt;/li&gt;&#xD;
&lt;/ol&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
So if I type:&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
scala &amp;gt; nextPalindromeDates take 5 print  &#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
It should get the next 5 palindrome dates from the day that you run it.&#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
Here are my solutions, with help from the group of course:&#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
Write down the digits from 2 to 7, in order. Add two mathematical symbols to get an expression equaling 2010. What symbols are these? (Figure it out in Scala)&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
&#xD;
object Runner {&#xD;
  def eval(s: String): Int = {&#xD;
    val pattern = &amp;quot;\\d+&amp;quot;.r&#xD;
    var nums = ((pattern findAllIn s).toList)&#xD;
    val opPattern = &amp;quot;(\\+|-|\\*|/)&amp;quot;.r&#xD;
    var ops =  &amp;quot;+&amp;quot; :: ((opPattern findAllIn s).toList)&#xD;
    nums.zip(ops).foldLeft(0) {&#xD;
      (x, y) =&amp;gt;&#xD;
        val num = y._1&#xD;
        val op = y._2&#xD;
        op match {&#xD;
          case &amp;quot;+&amp;quot; =&amp;gt; x + num.toInt&#xD;
          case &amp;quot;-&amp;quot; =&amp;gt; x - num.toInt&#xD;
          case &amp;quot;*&amp;quot; =&amp;gt; x * num.toInt&#xD;
          case &amp;quot;/&amp;quot; =&amp;gt; x / num.toInt&#xD;
        }&#xD;
    }&#xD;
  }&#xD;
&#xD;
  def main(args: Array[String]) {&#xD;
    val list = (2 to 7).toList&#xD;
    val operators = &amp;quot;+&amp;quot; :: &amp;quot;*&amp;quot; :: &amp;quot;/&amp;quot; :: &amp;quot;-&amp;quot; :: Nil&#xD;
    (1 to 5).foreach{ x =&amp;gt; ((x+1) to 6).foreach{ y =&amp;gt;&#xD;
        operators.foreach { op1 =&amp;gt; operators.foreach { op2 =&amp;gt;&#xD;
             if (x != y) {&#xD;
               var formula = (list.slice(0, x) ::: (op1 :: (list.slice(x, y))) ::: (op2 :: (list.takeRight(6 - y)))).mkString&#xD;
               var total = eval(formula)&#xD;
               if (total == 2010) println(formula)&#xD;
             }&#xD;
        }}}}&#xD;
  }&#xD;
}&#xD;
&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
This was harder than it seemed and required an eval that can parse out a String like  &amp;quot;2000*13+12&amp;quot;  and evaluate it.  My strategy was to peel all the numbers into a nums var and then peel all the operators using regular expression.  I then zipped up the numbers with their corresponding operators and folded them up for the result.  &#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
Next, in my main method I have 4 loops one is the loop to determine the first space to separate the numbers.  The next loop is to determine where to space the next set of numbers.  So for example, given that x = 2 and y = 4:&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
2 3 4 5 6 7&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
It would separate the 2nd space and the 4th space giving me:&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
2 3 [        ] 4 5 [       ] 6 7 &#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
It is in these spaces that I will put in operator variants. from the 3rd and 4th loop.   op1 and op2 will go through each of the operators and fit them in to determine which of the applicable value will work and give me the result of 2010.&#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
After running this application I get the result:&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
2345*6/7&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
The 2nd homework question was:  01/02/2010 was a palindrome date and it was awesome!  In Scala, figure out the next dates that will be a palindrome, and give me the results in a Stream[Date]. &#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
So if I type:&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
scala &amp;gt; nextPalindromeDates take 5 print  &#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
It should get the next 5 palindrome dates from the day that you run it.&#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
Here is my solution to this puzzle:&#xD;
&lt;/p&gt;
&#xD;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
object PalindromeDates  {&#xD;
  val datesOnlyFormat = new SimpleDateFormat(&amp;quot;MMddyyyy&amp;quot;)&#xD;
  val dateFormat = new SimpleDateFormat(&amp;quot;MM/dd/yyyy&amp;quot;)&#xD;
&#xD;
  def isPalindrome(cal:Calendar) = {&#xD;
    val fmt = datesOnlyFormat.format(cal.getTime())&#xD;
    fmt.reverse.toString == fmt&#xD;
  }&#xD;
&#xD;
  def getNextPalindromeDate(cal:Calendar):Stream[Calendar] = {&#xD;
    cal.add(Calendar.DATE, 1)&#xD;
    if (isPalindrome(cal)) Stream.cons(cal, getNextPalindromeDate(cal))&#xD;
    else getNextPalindromeDate(cal)&#xD;
  }&#xD;
&#xD;
  def main(args:Array[String]) {&#xD;
    getNextPalindromeDate(Calendar.getInstance()) take 30 foreach {x=&amp;gt;println(dateFormat.format(x.getTime()))}&#xD;
  }&#xD;
}&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
This solution obviously uses recursion with getNextPalindrome date.  It goes through each Calendar.  If the calendar is a Palindrome date as determined by isPalindrome, then the date is conssed (New Word!)  to the stream and recursed again with the calendar.  One thing that stuck out like a sore thumb was the mutable Calendar, given that Scala is a functional language and has a strong culture of having just about everything as immutable as possible with no side effects.&#xD;
&lt;/p&gt;
&#xD;
&lt;p class="seamTextPara"&gt;
The main method for this application is different that the homework I provided just because I wanted cleaner and sexier output.  I also gave it 30 instead of 5 to see if it can handle it and it did, and it did so surprisingly fast. Here is the result...&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
11/02/2011&#xD;
02/02/2020&#xD;
12/02/2021&#xD;
03/02/2030&#xD;
04/02/2040&#xD;
05/02/2050&#xD;
06/02/2060&#xD;
07/02/2070&#xD;
08/02/2080&#xD;
09/02/2090&#xD;
10/12/2101&#xD;
01/12/2110&#xD;
11/12/2111&#xD;
02/12/2120&#xD;
12/12/2121&#xD;
03/12/2130&#xD;
04/12/2140&#xD;
05/12/2150&#xD;
06/12/2160&#xD;
07/12/2170&#xD;
08/12/2180&#xD;
09/12/2190&#xD;
10/22/2201&#xD;
01/22/2210&#xD;
11/22/2211&#xD;
02/22/2220&#xD;
12/22/2221&#xD;
03/22/2230&#xD;
04/22/2240&#xD;
05/22/2250&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
So try out these puzzles on your own and see how well you can do, and try some other solutions.  Better yet, form either a Scala User Group or a Scala Study Group in your area and work on it together.  &#xD;
&lt;/p&gt;</description>
      <pubDate>Tue, 02 Feb 2010 19:40:46 GMT</pubDate>
      <guid>http://www.evolutionnext.com/blog/entry/title/Albuquerque+Scala+Study+Group+Funtime+Homework+and+Solutions.html</guid>
      <dc:date>2010-02-02T19:40:46Z</dc:date>
    </item>
    <item>
      <title>Decorator Pattern In Scala</title>
      <link>http://www.evolutionnext.com/blog/entry/title/Decorator+Pattern+In+Scala.html</link>
      <description>&lt;p class="seamTextPara"&gt;
As part of the Scala Study Group in Albuquerque, we had an assignment to reproduce some of the popular design patterns in Scala.  Although the assignment is over, it is worth while to log about it.   The following pattern was taken from &lt;a href="http://groovy.codehaus.org/Decorator+Pattern" class="seamTextLink"&gt;Groovy Decorator Example&lt;/a&gt;.&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
trait Logger {&#xD;
  def log(message: String) = {&#xD;
    message&#xD;
  }&#xD;
}&#xD;
&lt;/pre&gt;&#xD;
&lt;p class="seamTextPara"&gt;
Figure 1: Establishing a trait shared by all Loggers.&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
object StdLogger extends Logger&#xD;
&lt;/pre&gt;&#xD;
&lt;p class="seamTextPara"&gt;
Figure 2: Objects are explicit creations of an object.  There are no statics in Scala, and for good reason. Reasons that are too many for me to cover here.  What this does is create an object called StdLogger. This is merely an implementation of the trait with no added behavior.&#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
class TimeStampingLogger(logger: Logger) extends Logger {&#xD;
  override def log(message: String) = {&#xD;
    val now = Calendar.getInstance&#xD;
    now.getTime.toString + ' ' + logger.log(message)&#xD;
  }&#xD;
}&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
Figure 3: The time stamping logger takes a Logger object and decorates with the current time prepended with whatever message is returned from the parent logger. &#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
class UpperLogger(logger: Logger) extends Logger {&#xD;
  override def log(message: String) = {&#xD;
    logger.log(message).toUpperCase&#xD;
  }&#xD;
}&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
Figure 4:  The UpperLogger will capitalize all letters returned from the parent Logger object. &#xD;
&lt;/p&gt;
&#xD;
&lt;pre class="seamTextPreformatted"&gt;
&#xD;
object DecoratorRunner extends Application {&#xD;
  override def main(args: Array[String]) {&#xD;
    val timeStampingLogger = new TimeStampingLogger(StdLogger)&#xD;
    val upperLogger = new UpperLogger(timeStampingLogger)&#xD;
    println(upperLogger.log(StdLogger.log(&amp;quot;Decorator for the Scala Study Group&amp;quot;)))&#xD;
  }&#xD;
}&#xD;
&lt;/pre&gt;&#xD;
&#xD;
&lt;p class="seamTextPara"&gt;
Figure 5: The application runner itself.  I instantiate the TimeStampingLogger and the UpperLogger.  Note that I do not instantiate the StdLogger because that already is instantiated because it is an object.  I chain them together and produce the needed result.  &#xD;
&lt;/p&gt;</description>
      <pubDate>Tue, 24 Nov 2009 17:39:13 GMT</pubDate>
      <guid>http://www.evolutionnext.com/blog/entry/title/Decorator+Pattern+In+Scala.html</guid>
      <dc:date>2009-11-24T17:39:13Z</dc:date>
    </item>
  </channel>
</rss>


