Affichage des articles dont le libellé est IT geek. Afficher tous les articles
Affichage des articles dont le libellé est IT geek. Afficher tous les articles

vendredi, novembre 02, 2012

Managing dates with a DSL implemented in Scala

Following Paulo "JCranky" Siqueira's post who himself built it based on Sergio Lopes's post, I also enhanced this class by adding the ability to set dates and to compare them.

Another good link: Baysick: A Scala DSL Implementing BASIC



package kebra

import java.util.Calendar
import java.text.SimpleDateFormat
import java.text.ParsePosition

class DateDSL(val cal: Calendar) {
 cal.clear(Calendar.HOUR)
 import DateDSL.Conjunction
 
 def this(d: DateDSL) = this(d.cal)

 private var last = 0;

 def plus(num: Int) = { last = num; this }
 def minus(num: Int) = { last = -num; this }

 def +(num: Int) = plus(num)
 def -(num: Int) = minus(num)

 def months = { cal.add(Calendar.MONTH, last); this }
 def months(and: Conjunction): DateDSL = months
 def month = months
 def month(and: Conjunction): DateDSL = months

 def years = { cal.add(Calendar.YEAR, last); this }
 def years(and: Conjunction): DateDSL = years
 def year = years
 def year(and: Conjunction): DateDSL = years

 def days = { cal.add(Calendar.DAY_OF_MONTH, last); this }
 def days(and: Conjunction): DateDSL = days
 def day = days
 def day(and: Conjunction): DateDSL = days

 def is(then: Calendar) = {
  cal.setTimeInMillis(then.getTimeInMillis)
  cal.clear(Calendar.HOUR)
  this
 }

 def is(then: String) = {
  val cthen = ParseDate(then,"MM/dd/yyyy")
  cal.setTimeInMillis(cthen.getTimeInMillis)
  cal.clear(Calendar.HOUR)
  this
 }

 def ParseDate(s_date: String, s_format: String): Calendar = {
  var cal = Calendar.getInstance()
  cal.setTime(new SimpleDateFormat(s_format).parse(s_date, new ParsePosition(0)))
  cal
 }

 def before(d: DateDSL): Boolean = cal.before(d.cal)
 def after(d: DateDSL): Boolean = cal.after(d.cal)

 override def toString = new String(new SimpleDateFormat("ddMMMyy").format(cal.getTime()))
}

object DateDSL {
 class Conjunction
 val and = new Conjunction

 def Today = new DateDSL(Calendar.getInstance)
 def Tomorrow = Today + 1 day
 def Yesterday = Today - 1 day

 def today = Today
 def tomorrow = Tomorrow
 def yesterday = Yesterday

 def Now = Today
 def now = Today
}
I also added a testcase below:
package kebra

import java.util.Calendar
import java.text.SimpleDateFormat
import kebra.DateDSL._


object TestKebra extends App {

 println("Hello World!")
 println(Tomorrow minus 1 month and plus 10 years and plus 1 day)
 println(Today + 2 months and plus 9 years and minus 1 day)
 println(Today - 9 years and minus 1 day)
 println(((Today + 2 months and) + 9 years and) - 1 day)
 println(now is "10/1/2011" plus 3 days)
 println("\n"+printZisday((now is "10/1/2011" plus 3 days).cal,"ddMMMyy"))
 assert(printZisday((now is "10/1/2011" plus 3 days).cal,"MM/dd/yyyy").indexOf("10/04/2011")==0)
 assert((now is "10/1/2011" plus 3 days).before(now is "10/1/2011" plus 4 days))
 assert((now is "10/1/2011" plus 4 days).after(now is "10/2/2011" plus 2 days))
 
 val zendDate = new DateDSL(now is "2/2/2011")
 var ago = new DateDSL(now is "2/2/2011" minus (9*7) days)
 println("***** ago: "+ago+", zendDate: "+zendDate)
 while(ago.before(zendDate)) {
  println("      ago: "+ago)
  ago plus 7 days
 }
 println("***** ago: "+ago+", zendDate: "+zendDate)
 assert((ago plus 1 day).after(zendDate))
 println("***** ago: "+ago+", zendDate: "+zendDate)
 assert((ago minus 2 days).before(zendDate))
 println("***** ago: "+ago+", zendDate: "+zendDate)
 
 def printZisday(zisday:  Calendar, fmt: String): String = new String(new SimpleDateFormat(fmt).format(zisday.getTime()))
}

jeudi, décembre 08, 2011

Simple linear regression in Scala

Here is how to compute Simple linear regression in Scala.
Class LinearRegression takes in n measurements from a List[(x: Double, y: Double)] and computes the line that best fits the data according to the least squares metric.
This Scala program is the scala translation of the java program available at http://introcs.cs.princeton.edu/java/97data/LinearRegression.java.html .
class LinearRegression(val pairs: List[(Double,Double)]) { 
 val size = pairs.size
 println("pairs = " + pairs)

 // first pass: read in data, compute xbar and ybar
 val sums = pairs.foldLeft(new X_X2_Y(0D,0D,0D))(_ + new X_X2_Y(_))
 val bars = (sums.x / size, sums.y / size)

 // second pass: compute summary statistics
 val sumstats = pairs.foldLeft(new X2_Y2_XY(0D,0D,0D))(_ + new X2_Y2_XY(_, bars))

 val beta1 = sumstats.xy / sumstats.x2
 val beta0 = bars._2 - (beta1 * bars._1)
 val betas = (beta0, beta1)

 println("y = " + ("%4.3f" format beta1) + " * x + " + ("%4.3f" format beta0))

 // analyze results
 val correlation = pairs.foldLeft(new RSS_SSR(0D,0D))(_ + RSS_SSR.build(_, bars, betas))
 val R2 = correlation.ssr / sumstats.y2
 val svar = correlation.rss / (size - 2)
 val svar1 = svar / sumstats.x2
 val svar0 = ( svar / size ) + ( bars._1 * bars._1 * svar1)
 val svar0bis = svar * sums.x2 / (size * sumstats.x2)
 println("R^2                 = " + R2)
 println("std error of beta_1 = " + Math.sqrt(svar1))
 println("std error of beta_0 = " + Math.sqrt(svar0))
 println("std error of beta_0 = " + Math.sqrt(svar0bis))
 println("SSTO = " + sumstats.y2)
 println("SSE  = " + correlation.rss)
 println("SSR  = " + correlation.ssr)
}

object RSS_SSR {
 def build(p: (Double,Double), bars: (Double,Double), betas: (Double,Double)): RSS_SSR = {
  val fit = (betas._2 * p._1) + betas._1
  val rss = (fit-p._2) * (fit-p._2)
  val ssr = (fit-bars._2) * (fit-bars._2)
  new RSS_SSR(rss, ssr)
 }
}

class RSS_SSR(val rss: Double, val ssr: Double) {
 def +(p: RSS_SSR): RSS_SSR = new RSS_SSR(rss+p.rss, ssr+p.ssr)
}

class X_X2_Y(val x: Double, val x2: Double, val y: Double) {
 def this(p: (Double,Double)) = this(p._1, p._1*p._1, p._2)
 def +(p: X_X2_Y): X_X2_Y = new X_X2_Y(x+p.x,x2+p.x2,y+p.y)
}

class X2_Y2_XY(val x2: Double, val y2: Double, val xy: Double) {
 def this(p: (Double,Double), bars: (Double,Double)) = this((p._1-bars._1)*(p._1-bars._1), (p._2-bars._2)*(p._2-bars._2),(p._1-bars._1)*(p._2-bars._2))
 def +(p: X2_Y2_XY): X2_Y2_XY = new X2_Y2_XY(x2+p.x2,y2+p.y2,xy+p.xy)
}

mardi, novembre 29, 2011

Concrete Scala Map and SortedMap example

This is a concrete Scala Map and SortedMap example.
I wrote this post because I found it very difficult to find the right information when trying to get a concrete Map and SortedMap.
class StringOrder extends Ordering[String] {
 override def compare(s1: String, s2: String) = s1.compare(s2)
}
class MyParameter() {}


class ZeParameters(val pairs:List[(String,MyParameter)] = Nil) extends SortedMap[String,MyParameter] {
 /**** Minimal Map stuff begin ****/
 lazy val keyLookup = Map() ++ pairs
 override def get(key: String): Option[MyParameter] = keyLookup.get(key)
 override def iterator: Iterator[(String, MyParameter)] = pairs.reverseIterator
 override def + [B1 >: MyParameter](kv: (String, B1)) = {
  val (key:String, value:MyParameter) = kv
  new ZeParameters((key,value) :: pairs)
 }
 override def -(key: String): ZeParameters  = new ZeParameters(pairs.filterNot(_._1 == key))
 /**** Minimal map stuff end ****/
 /**** Minimal SortedMap stuff begin ****/
 def rangeImpl (from: Option[String], until: Option[String]): ZeParameters = {
  val out = pairs.filter((p: (String, MyParameter)) => {
   var compareFrom = 0
   from match {
    case Some(s) => compareFrom = p._1.compare(s)
    case _ =>
   }
   var compareUntil = 0
   until match {
    case Some(s) => compareUntil = p._1.compare(s)
    case _ =>
   }
   compareFrom>=0 && compareUntil<=0
  })
  new ZeParameters(out)
 }
 
 def ordering: Ordering[String] = new StringOrder
 /**** Minimal SortedMap stuff end ****/
}
Do not forget that you can also transform your map into a list and then use sortBy:
class ListSort {
  println(List((1.0,"zob"),(1.2,"zab"),(0.9,"zub")).sortBy{_._1})
}

jeudi, septembre 01, 2011

When programming in Java or Scala, I miss those C pre compiler macros __FILE__ , __LINE__ and __FUNC__ . I use them for logging where I am in my programs.

Well, I decided to have those in Scala, using Stack parsing after athrowing an interruption. I personally don't care if it's take time to execute.

There is one advantage compared to the C macros: you can get any upper level in the calling stack, which I sometimes find handy.


object util {
 val MatchFileLine = """.+\((.+)\..+:(\d+)\)""".r
 val MatchFunc = """(.+)\(.+""".r
 def main(args: Array[String]): Unit = { 
  println(util.tag(1))
  println(util.func(1))
 }
 def tag(i_level: Int): String = {
  val s_rien = ""
  try {
   throw new Exception()
  } catch {
   case unknown => unknown.getStackTrace.toList.apply(i_level).toString match {
    case MatchFileLine(file, line) => file+":"+line
    case _ => s_rien
   }
  }
 }

 def func(i_level: Int): String = {
  val s_rien = "functionNotFound"
  try {
   throw new Exception()
  } catch {
   case unknown => unknown.getStackTrace.toList.apply(i_level).toString match {
    case MatchFunc(funcs) => funcs.split('.').toList.last
    case _ => s_rien
   }
  } 
 }
}

class util() { }

jeudi, mars 10, 2011

Scala TreeSet: foreach vs foldLeft

Here is the best article I found to understand what is foldLeft. And here is an example on how to replace foreach with foldLeft:


class KbdKey() {
  def generateQuartets(): ListSet[Quartet] = {...}
}

class KbdMatrix() {
  var ts_keys = new TreeSet[KbdKey]()

  var l_quartets = ListSet.empty[Quartet];
  ts_keys.foreach(l_quartets ++= _.generateQuartets())

  // 2 lines above can be replaced by the line below

  val l_quartets = ts_keys.tail.foldLeft(ts_keys.head.generateQuartets)(_ ++ _.generateQuartets)
}

mercredi, mars 09, 2011

Scala Treeset example: partition

var ts_keys = new TreeSet[KbdKey]()(new CompareRowThenCol())

val (ts_inferiorOrEqualRowKeys,ts_superiorRowKeys) = ts_Keys.partition(_.i_row<=i_row)

class CompareRowThenCol extends Ordering[KbdKey] {
    def compare(k1: KbdKey, k2: KbdKey) = k1.value-k2.value
}

lundi, mars 07, 2011

Scala Treeset example: filter, groupBy, foldLeft, sortBy, etc...

A scala example doing various operations on a TreeSet containing keys organized in rows and columns and who have a certain type. Output is an html table.

def myPrint(i_type: Int, ts_keys: TreeSet[KbdKey]) {

  val lcol = (0 to 16)

  print(lcol.tail.foldLeft("Column [" + lcol.head +"]")(_ + "Column [" + _ +"]"))

  val ts_matrixFilteredByType = ts_keys.filter((k: KbdKey) => k.i_type==i_type)

  val m_matrixGroupByRow = ts_matrixFilteredByType.groupBy((k: KbdKey) => k.i_row)

  val m_matrixGroupBySortedRow = ListMap(m_matrixGroupByRow.toList.sortBy{_._1}:_*)

  m_matrixGroupBySortedRow.foreach((p:(Int, TreeSet[KbdKey])) => 
    print(p._2.tail.foldLeft("Row["+p._1+"]" + p._2.head.myprint)(_ + _.myprint) +"-"))
}

Things to notice are:
  • use of foldLeft to do some printing and not some plain summing "as usual".
  • groupBy returns pairs.

Scala and Java libraries in Eclipse

Don't try to mix scala and java in the same scala project in eclipse. You will 1st have the feeling that it works, but as they are not compiled the same way, it's better to have java and scala files in separate projects. Scala project should have the java project as a dependency.

To get it working, you have to do a lot of project cleaning, to "synchronize" both projects.

A Scala file using a Java class:
   import kbdmatrix_java._
   class KbdMatrix(val L: MyLog) {

The Java file:
   package kbdmatrix_java;
   public class MyLog {

Do not forget to advertize the Java class and its methods as public.

Scala: code to find worksheet name in an excel workbook saved in microsoft xml 2003 format

Scala: code to find worksheet name in an excel workbook saved in microsoft xml 2003 format:
def matchName(n: scala.xml.NodeSeq, s_attributeValue: String): Boolean = {
  n match {
            case xml.Elem(_, "Worksheet", xml.PrefixedAttribute("ss", "Name", v, _), _, _*) => 
                 if(v.text==s_attributeValue) true else false
            case _ => false 
  }
}

mercredi, février 23, 2011

Encryption is useless



What actually happened (Cnet 23Feb2011): Feds seek new ways to bypass encryption
SAN FRANCISCO--When agents at the Drug Enforcement Administration learned a suspect was using PGP to encrypt documents, they persuaded a judge to let them sneak into an office complex and install a keystroke logger that recorded the passphrase as it was typed in.

Read more: http://news.cnet.com/8301-31921_3-20035168-281.html#ixzz1EoUlHQa9

vendredi, janvier 07, 2011

Dimensions Treemap using Protovis

Based on Treemap from protovis design in javascript, I have now a treemap with 3 dimensions.

Click on the image below to see it in action (because my post does not allow it to be dynamically displayed).



3 Dimensions are:
  1. definition of groups that share the same kind of color
  2. each square with it's area
  3. each square is more or less dark and more or less saturated
This is something that you I use to display a status of issues on a bunch of projects. For instance we develop several keyboards and mice at the same time.
  1. The yellow group of color could group all keyboard projects.
  2. square area is proportional to the total number of bugs for this project
  3. square darkness and saturation/alpha is proportional to the number of open bugs for this project

mardi, avril 20, 2010

jeudi, avril 08, 2010

lambdaj errors explained

java.lang.IllegalAccessException: Class ch.lambdaj.proxy.ProxyIterator can not access a member of class XYZ with modifiers "public" at line forEach(products_byname).cropDefects();
assuming List products_byname = new List();
means that class Product should be made public.

java.lang.IllegalAccessException: Class ch.lambdaj.proxy.ProxyIterator can not access a member of class XYZ with modifiers "" at line forEach(products_byname).cropDefects();
means that method cropDefects() should be made public.

java.lang.RuntimeException: It is not possible to create a placeholder for class: java.lang.reflect.Field at line List l_null_fields = select(l_fields,having(on(Field.class).get(this),nullValue())); means that you encountered a Limitation of lambdaj caused by the Java language specification because Field is a final class.


java.lang.IllegalArgumentException: forEach() is unable to introspect on an empty iterator. Use the overloaded method accepting a class instead at line forEach(defects).updateBugActivity(stateArray); means that you encountered a Limitation of lambdaj caused by the Java language specification because defects is an empty list.

mercredi, avril 07, 2010

Java program using lambdaj


Today, I did the same thing (i.e. no for or while statements), but using lambdaj java library.

There is still a for statement but if you make country as an object and not a string then you can use lambdaj features.

1 Have a list of people belonging to various countries:
European people: [Eric from France, Martine from France, John from Great-Britain, Martha from Great-Britain, Carine from France, Gerd from Deutschland, Giuseppe from Italia, Martha from Deutschland]

2 Get the different countries:

European countries: [Great-Britain, France, Italia, Deutschland]

3 List the people that belong to each country
:
People from: [France]: [Carine from France, Eric from France, Martine from France]
People from: [Deutschland]: [Gerd from Deutschland, Martha from Deutschland]
People from: [Great-Britain]: [John from Great-Britain, Martha from Great-Britain]
People from: [Italia]: [Giuseppe from Italia]

Here is the code:
   1 import static ch.lambdaj.Lambda.*;
   2 import ch.lambdaj.group.*;
   3 import java.util.List;
   4 import java.util.Arrays;
   5 import java.util.Set;
   6 
   7 
   8 public class Europeans1 {
   9 
  10         static List<People> l_europeans = Arrays.asList(
  11                         new People("Eric", "France"),
  12                         new People("Martine", "France"),
  13                         new People("John", "Great-Britain"),
  14                         new People("Martha", "Great-Britain"),
  15                         new People("Carine", "France"),
  16                         new People("Gerd", "Deutschland"),
  17                         new People("Giuseppe", "Italia"),
  18                         new People("Martha", "Deutschland"));
  19 
  20         public static void main(String[] args) {
  21                 System.out.println("European people: "+l_europeans);
  22                 Group<People> g_countries = Groups.group(l_europeans, 
  23                         Groups.by(on(People.class).getNationality()));
  24                 Set<String> set_countries = g_countries.keySet();
  25                 System.out.println("European countries: "+set_countries);
  26                 for(String s_country:set_countries) {
  27                         print_inhabitants(s_country);
  28                 }
  29         }
  30 
  31         static void print_inhabitants(String s_country)  {
  32                 System.out.print("People from "+s_country+": ");
  33                 List<People> l_inhabitants = select(l_europeans,
  34                         having(on(People.class).getNationality(),
  35                         org.hamcrest.Matchers.equalTo(s_country)));
  36                 forEach(l_inhabitants).printFirstName();
  37                 System.out.println("");
  38         }
  39 }

dimanche, avril 04, 2010

Java vs Haskell


At that time I did not know it, but I was doing some kind of functional programming. I discovered Haskell a few weeks ago, and I duplicated what I did in Java.

As you can guess the Haskell code is way shorter, and goes even a little further.

1 Have a list of people belonging to various countries:
European people: [Eric from France, Martine from France, John from Great-Britain, Martha from Great-Britain, Carine from France, Gerd from Deutschland, Giuseppe from Italia, Martha from Deutschland]

2 Get the different countries:

European countries: ["Deutschland","France","Great-Britain","Italia"]

3 List the people that belong to each country
:
People from: [France]: [Carine from France, Eric from France, Martine from France]
People from: [Deutschland]: [Gerd from Deutschland, Martha from Deutschland]
People from: [Great-Britain]: [John from Great-Britain, Martha from Great-Britain]
People from: [Italia]: [Giuseppe from Italia]

Here is the code:
import Data.List
import Data.Function

le = [
("Eric", "France"),("Martine", "France"),
("John", "Great-Britain"),("Martha", "Great-Britain"),("Carine", "France"),
("Gerd", "Deutschland"),("Giuseppe", "Italia"),("Martha", "Deutschland")
]

dla xs = "European people: " ++ show [ fst x ++ " from " ++ snd x | x <- xs]
lc xs = [ head y | y <- group(sort([ snd x | x <- xs]))]
dlc = "European countries: " ++ show(lc le)

lpc5 xps = groupBy (\x y -> snd x == snd y) (sortBy (compare `on` snd)([ xp | xp <-xps ]))
lpc9 xps = unlines [ "People from " ++ show (snd (head xp)) ++ ":" ++ (show [fst x | x<-xp ]) | xp <-xps ]
fait9 = do { m <- [lpc5 le]; lpc9 m }
dlad = [ dla le, dlc, fait9 ]

main = putStrLn (unlines(dlad))

mercredi, mars 24, 2010

Getting Haskell XML Toolbox to run on windows

First of all, as of today (march 2010), you have to use the version 8.3.2.
you'll need to install tagsoup-0.6: link
cabal install tagsoup-0.6

then you'll need to install curl:
basically it does not work.

you must install cygwin with curl libraries installed.

running cabal install curl under cygwin will give you some errors even when following these advice: Installing curl from hackage on Cygwin
but do the asked copy anyway because, it maybe useful for the final solution.

you can also try to run these commands instead of running cabal link
download curl from http://hackage.haskell.org/packages/archive/pkg-list.html
cd xxx/curl-1.3.5
runhaskell Setup.hs configure --extra-include-dirs=c:/SandBox/cygwin/usr/include --extra-lib-dirs=c:/SandBox/cygwin/lib
you'll get an error but continue with the next commands.
runhaskell Setup.hs build
runhaskell Setup.hs install
it will tell can't find curl lib.
I can also remember that I had to hardcode some values in curl.c because it could not find the ones defined in curl.h

What I finally did, and I think that is the only thing you need to do:
get the win32 generic version of libcurl from http://curl.haxx.se/download.html : http://www.gknw.net/mirror/curl/win32/curl-7.20.0-devel-mingw32.zip
extract the zip
copy all the lib/*.lib in the lib directories from cygwin and haskell_ghc
copy bin/*.* in the cabal_haskell/bin directory where tagsoup.exe is already present.

install hxt
then either run cabal install hxt-8.3.2 or download the hxt package and run
cd xxx/hxt-8.3.2
runhaskell Setup.lhs configure
runhaskell Setup.lhs build
runhaskell Setup.lhs install



vendredi, mars 19, 2010

Getting Haskell Running on Eclipse - 2010 updated

I wanted to get Haskell running on Eclipse so I tried to do as the video below is saying:

The problem I got when following the video is that it did not work exactly as shown, so, here is my modest contribution to give additional hints on how to have Haskell running on eclipse.

install haskell eclipse plugin
get http://hackage.haskell.org/platform/
install
get http://cvs.haskell.org/Hugs/pages/downloading.htm
install
get http://www.haskell.org/cabal/download.html
execute "cabal" in a dos windows and follow instructions
http://imonad.com/blog/2009/10/installing-haskell-plugin-for-eclipse/
in C:\Program Files\Haskell\bin
execute "cabal update" in a dos windows
execute "cabal install scion" in a dos windows
in C:\Program Files\Haskell\bin you should find scion-server.exe that you'll need it in haskell preferences.

lundi, avril 06, 2009

java list management and filtering without any loop (for, while)

Just a Java exercize for the fun of it: do some list manipulation / filtering without using for/while statements:

1 Have a list of people belonging to various countries:

European people: [Eric from France, Martine from France, John from Great-Britain, Martha from Great-Britain, Carine from France, Gerd from Deutschland, Giuseppe from Italia, Martha from Deutschland]

2 Get the different countries:

European countries: [Gerd from Deutschland, Eric from France, John from Great-Britain, Giuseppe from Italia]

3 List the people that belong to each country
:
People from: [France]: [Carine from France, Eric from France, Martine from France]
People from: [Deutschland]: [Gerd from Deutschland, Martha from Deutschland]
People from: [Great-Britain]: [John from Great-Britain, Martha from Great-Britain]
People from: [Italia]: [Giuseppe from Italia]

Never use for while statements to perform these tasks but make heavy use of the SortedSet type.
Find code here.

The trick is to use a compare function that return 0 when the guy's/lady's country does not match the country we are listing inhabitants for.

Read also the same exercize done differently in Java and Haskell: