Scala call-by-name

The arguement notation : => A denotes passing the argument in call-by-name manner not the traditional call-by-value fashion. That essentially means:

  • If the parameter is never used, it is never evaluated
  • If the parameter is used multiple times, it is getting evaluated each and every time in the by-name invocation

Traditionally call-by-value invocation would be evalutated just once and the calculated value would be just pushed in the method stack:

def myprint(date: java.util.Date):Unit = for(i->1 to 5) println(date)

scala> myprint( {Thread.sleep(1000); new java.util.Date()} )
Sun Apr 27 10:10:37 GMT 2014
Sun Apr 27 10:10:37 GMT 2014
Sun Apr 27 10:10:37 GMT 2014
Sun Apr 27 10:10:37 GMT 2014
Sun Apr 27 10:10:37 GMT 2014

whereas call-by-name would be evaluated each and every time anew:

def myprint(date :=> java.util.Date):Unit = for(i->1 to 5) println(date)

scala> myprint( {Thread.sleep(1000); new java.util.Date()} )
Sun Apr 27 10:12:46 GMT 2014
Sun Apr 27 10:12:47 GMT 2014
Sun Apr 27 10:12:48 GMT 2014
Sun Apr 27 10:12:49 GMT 2014
Sun Apr 27 10:12:50 GMT 2014

How to add Options in Scala

Options are Monads and we can perform operations in their underlying types in a typesafe way using Scalaz:

  import scalaz._
  import Scalaz._

  val a: Option[BigDecimal] = BigDecimal("0.123").some
  val b: Option[BigDecimal] = BigDecimal("1.234").some
  val c: Option[BigDecimal] = BigDecimal("2.345").some

  println( (a |@| b |@| c) { _ + _ + _ } )

  //yields: Some(3.702)

and if the chain is broken, we don’t have to worry since it’s getting short-circuited:

  import scalaz._
  import Scalaz._

  val a: Option[BigDecimal] = BigDecimal("0.123").some
  val b: Option[BigDecimal] = None
  val c: Option[BigDecimal] = BigDecimal("2.345").some

  println( (a |@| b |@| c) { _ + _ + _ } )

  //yields: None

Spring JPA, Hibernate, Data XML configuration

Here’s a cleaned up, plug’n’play version of a Spring database XML config taken from a demo project employing Spring Data for the Repositories interfaces and Hibernate behind JPA.

<beans 	xmlns="http://www.springframework.org/schema/beans"
		xmlns:p="http://www.springframework.org/schema/p"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/data/jpa
		http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
		  p:entityManagerFactory-ref="entityManagerFactory" />


	<jpa:repositories base-package="com.to.my.data.repository"
					  transaction-manager-ref="transactionManager"
					  entity-manager-factory-ref="entityManagerFactory"/>

	<tx:annotation-driven transaction-manager="transactionManager"/>
	

	
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
					p:database="${database.database}"
					p:databasePlatform="${database.databasePlatform}"
					p:showSql="${database.showSql}"
					p:generateDdl="${database.generateDdl}" />
		</property>
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.dialect">${database.databasePlatform}</prop>
				<prop key="hibernate.max_fetch_depth">3</prop>
				<prop key="hibernate.fetch_size">50</prop>
				<prop key="hibernate.batch_size">10</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.to.my.bean"/>
	</bean>
	
	<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"
			p:driverClassName="${database.driverClassName}"
			p:url="${database.url}"
			p:username="${database.username}"
			p:password="${database.password}"
			p:testOnBorrow="true"
			p:testOnReturn="true"
			p:testWhileIdle="true"
			p:timeBetweenEvictionRunsMillis="1800000"
			p:numTestsPerEvictionRun="3"
			p:minEvictableIdleTimeMillis="1800000"
			p:initialSize="1"
			p:maxActive="50"
			p:maxIdle="20"/>

</beans>

Scala Pimp-My-Library example

Recently working on a project test there was an incompatibility of the assert keyword between ScalaTest and Scalaz. I was looking after Scalaz’s feature of invoking someĀ on any data type after import scalaz.Scalaz._

After a while I gave up and wrote it myself, surprisingly simple and concise:

class SomeType[T](t:T){
def some = Some(t)
}
implicit def typeToSomeType[T](t:T) = new SomeType(t)

Making possible:

scala> 123.some
res7: Some[Int] = Some(123)

scala>"hi".some
res8: Some[String] = Some(hi)

Implicits is a nice way to type-safe upon compile time some global state (as long as it doesn’t become unwieldy long).

In Groovy we could achieve same effect via the MetaClass object, but without compile time type-safety as Groovy it’s a dynamic language and all the magic happens at runtime. Also we can’t use parametric polymorphism while invoking the MetaClass object (or at least I don’t know how to do it!).

groovy> String.metaClass.quote={"*"+delegate+"*"}
===> groovysh_evaluate$_run_closure1@4b51ac10
groovy:000> "hi".quote()
===> *hi*