#scala #hackingHacking ScalaTumblr (3.0; @hacking-scala)http://hacking-scala.org/Generic OPTIONS HTTP Method Handling With akka-http<blockquote> <p>The OPTIONS method represents a request for information about the communication options available on the request/response chain identified by the Request-URI.</p> </blockquote> <div style="text-align: right">— <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.2" target="_blank">HTTP/1.1 Spec</a></div> <p>Sounds like pretty helpful HTTP method to use and implement, especially for the REST API. Unfortunately, not many people decide implement it for their own API. I think partly we should thank for this our web frameworks and libraries. In past I tried to implement generic <code>OPTIONS</code> method handler in many different frameworks (like <a href="https://www.playframework.com/" target="_blank">Play</a>, <a href="http://unfiltered.databinder.net/Unfiltered.html" target="_blank">Unfiltered</a> or some java frameworks), but unfortunately my experience was very frustrating to say the least (I really don&rsquo;t want to write this logic manually for every single resource in my API). In most cases you ether required to use reflection or speculatively try all methods against routing black box in order to find out which HTTP methods are handled and which are not.</p> <p><a href="http://doc.akka.io/docs/akka-stream-and-http-experimental/current/scala.html" target="_blank">akka-http</a> is the first HTTP library in my experience that provides right set of abstractions for this problem. I was able to implement <code>Options</code> handler with it in very natural way and in just few lines of code, so without further ado let me show you how it works.</p> <!-- more --> <h3>Rejections</h3> <p>I really like the concept of rejections in <a href="http://doc.akka.io/docs/akka-stream-and-http-experimental/current/scala.html" target="_blank">akka-http</a>. Just imagine following route definition:</p> <pre><code>val route: Route = pathPrefix("orders") { pathEnd { get { complete("Order 1, Order 2") } ~ post { complete("Order saved") } } ~ path(LongNumber) { id =&gt; put { complete(s"Order $id") } } } </code></pre> <p>When, for instance, <code>PUT /orders</code> request arrives, library will try to match it and go through <code>get</code> and <code>post</code> directives of <code>"orders"</code> path. Both of them will reject the request, because user tried to <code>put</code>. The interesting thing is that along the way akka-http will collect all these rejections and give them to the <code>RejectionHandler</code> when it will realise, that no handlers are defined for this request.</p> <p>By providing implicit instance of our own <code>RejectionHandler</code> we can define generic responce for <code>OPTIONS</code> method on any path and also define behaviour in case requested HTTP method has no route defined:</p> <pre><code>implicit def rejectionHandler = RejectionHandler.newBuilder() .handleAll[MethodRejection] { rejections =&gt; val methods = rejections map (_.supported) lazy val names = methods map (_.name) mkString ", " respondWithHeader(Allow(methods)) { options { complete(s"Supported methods : $names.") } ~ complete(MethodNotAllowed, s"HTTP method not allowed, supported methods: $names!") } } .result() </code></pre> <p>According to the spec, you need to at least return <code>Allow</code> header with the list of supported HTTP methods. Additionally we also list supported HTTP methods in the response body.</p> <p>Now let&rsquo;s try it out with <code>curl</code>:</p> <pre><code>$ curl -X OPTIONS -v http://localhost:8080/orders &lt; HTTP/1.1 200 OK &lt; Allow: GET, POST &lt; Server: akka-http/2.3.11 &lt; Date: Sun, 21 Jun 2015 14:59:49 GMT &lt; Content-Type: text/plain; charset=UTF-8 &lt; Content-Length: 30 &lt; Supported methods : GET, POST. </code></pre> <p>we also get similar result for <code>/orders/{id}</code>:</p> <pre><code>$ curl -X OPTIONS -v http://localhost:8080/orders/1234 &lt; HTTP/1.1 200 OK &lt; Allow: PUT &lt; Server: akka-http/2.3.11 &lt; Date: Sun, 21 Jun 2015 15:01:25 GMT &lt; Content-Type: text/plain; charset=UTF-8 &lt; Content-Length: 24 &lt; Supported methods : PUT. </code></pre> <p>If you ask for non-supported method like <code>PATCH</code>, you will get <code>405 Method Not Allowed</code>, as expected:</p> <pre><code>$ curl -X PATCH -v http://localhost:8080/orders &lt; HTTP/1.1 405 Method Not Allowed &lt; Allow: GET, POST &lt; Server: akka-http/2.3.11 &lt; Date: Sun, 21 Jun 2015 15:03:08 GMT &lt; Content-Type: text/plain; charset=UTF-8 &lt; Content-Length: 54 &lt; HTTP method not allowed, supported methods: GET, POST! </code></pre> <p>I hope you enjoyed. I encourage you implement <code>OPTIONS</code> HTTP method support in your own API in order to provide better REST API for people to consume (now there is no excuse not to).</p> <p>For your convenience I also created this gist with the all of the source code:</p> <script src="https://gist.github.com/OlegIlyenko/c4c7199e6eba3d1dff37.js"></script>http://hacking-scala.org/post/122084354623http://hacking-scala.org/post/122084354623Sun, 21 Jun 2015 17:17:36 +0200akka-httpapi designDependency Injection in Akka with Scaldi<p>As a follow-up to the post about <a href="http://hacking-scala.org/post/51407241538/easy-dependency-injection-in-play-framework-with-scaldi" target="_blank">Scaldi Play integration</a> I would like to introduce you an integration with the Akka in this post.</p> <p>Akka integration is pretty recent addition to scaldi and interestingly enough has not much to add to the core library in order to smoothly integration with the Akka. In order to use it you need to add <a href="https://github.com/scaldi/scaldi-akka" target="_blank">scaldi-akka</a> to the SBT build, so let&rsquo;s do this first:</p> <pre><code>libraryDependencies ++= Seq( "org.scaldi" %% "scaldi-akka" % "0.3.1" ) </code></pre> <p><!-- more --></p> <p>You may notice, that the groupId of scaldi has changed to <code>org.scaldi</code> since version 0.3. Version 0.3 is also a recent release, which has introduced some nice features like binding lifecycle (you can find the complete list of new feature in the <a href="https://github.com/scaldi/scaldi/blob/master/CHANGELOG.md#v03-02032014" target="_blank">changelog</a>.</p> <p>Now you should be ready to go. The only new thing that <code>scaldi-akka</code> adds is <code>AkkaInjectable</code>, which provides 2 additional inject methods:</p> <ul><li><code>injectActorRef</code> - creates a new actor with the help of <code>ActorRef</code> factory which should be implicitly available in the scope.</li> <li><code>injectActorProps</code> - injects <code>Props</code> for the <code>Actor</code>, so that you can create new <code>Actor</code>s yourself with the help of the <code>ActorRef</code> factory.</li> </ul><p>If you still wondering what <code>Injectable</code> actually is, I can recommend you to read <a href="http://hacking-scala.org/post/51407241538/easy-dependency-injection-in-play-framework-with-scaldi" target="_blank">one of my previous posts</a>. It explains the basic concepts of the library and shows an example application that uses Scaldi for the dependency injection.</p> <p>I mentioned <code>ActorRef</code> factory above. It can be one of two things:</p> <ul><li><code>ActorContext</code> - it always implicitly available within an <code>Actor</code> and can be used to create a new actors in the context of current actor</li> <li><code>ActorSystem</code></li> </ul><p>Here is a small example of how you can use <code>AkkaInjectable</code> to inject (which actually means create in case of actors) another actor:</p> <pre><code>class Receptionist (implicit inj: Injector) extends Actor with AkkaInjectable { val userService = inject [UserService] val orderProcessorProps = injectActorProps [OrderProcessor] val priceCalculator = injectActorRef [PriceCalculator] def receive = { case PlaceOrder(userName, itemId, netAmount) =&gt; val processor = context.actorOf(orderProcessorProps) // ... } } </code></pre> <p>Or alternatively, if you want to create an actor somewhere else (not inside an actor), you need to provide an implicit <code>ActorSystem</code> in the scope:</p> <pre><code>import scaldi.akka.AkkaInjectable._ implicit val appModule: Injector = // ... implicit val system = inject [ActorSystem] val receptionist = injectActorRef [Receptionist] </code></pre> <p>So far so good. We have created some actors that are able to use <code>inject</code>. The only thing that remains now is to create a module that binds them together with other dependencies and the <code>ActorSysyem</code> itself:</p> <pre><code>class OrderModule extends Module { bind [UserService] to new SimpleUserService bind [ActorSystem] to ActorSystem("ScaldiExample") destroyWith (_.shutdown()) binding toProvider new Receptionist binding toProvider new OrderProcessor binding toProvider new PriceCalculator } </code></pre> <p>As you can see, now you also can define how binding can be destroyed with the <code>destroyWith</code> function. It is part of the binding lifecycle feature that was introduced in Scaldi version 0.3.</p> <p>At this point I would like to point out how <code>Actor</code> are bound. It is important, that you bind then with <code>toProvider</code> function. It will make sure, that Scaldi always creates new instances of the <code>Actor</code> classes when you injecting them with <code>injectActorRef</code> or <code>injectActorProps</code>. These two methods actually use Akka mechanisms to configure an actor instance under-the-hood, but the actor instance creation itself is always delegated to Scaldi. During this process, Akka requires the delegate to always create new instances of an actor, so by binding <code>Actor</code>s with <code>toProvider</code> you are fulfilling the protocol, that Akka implies.</p> <p>I created a <a href="https://github.com/scaldi/scaldi-akka-example" target="_blank">small example application</a> that demonstrates an integration between Akka and Scaldi and all the things described in this post. You can also use it as a <a href="https://typesafe.com/activator/template/scaldi-akka-example" target="_blank">Typesafe Activator template</a>.</p> <p>Hope you enjoyed this post. As always, any of kind of feedback is very welcome!</p>http://hacking-scala.org/post/79931102837http://hacking-scala.org/post/79931102837Tue, 18 Mar 2014 03:18:00 +0100scaldiscaldi-akkaakkadependency injectionAdvanced Type Constraints with Type Classes<p>This weekend I was continuing my work on the next version of <a href="http://scaldi.org" target="_blank">Scaldi</a>. Once again I faced the problem that was bothering me quite some time. In <a href="http://scaldi.org" target="_blank">Scaldi</a> you can inject a dependency with something like this:</p> <pre><code>val server = inject [HttpServer] </code></pre> <p>In this case <code>inject</code> method will get <code>HttpServer</code> class as a type argument and everything works as expected. But what would happen if you forget to specify the type or you just defined the type of the <code>server</code> but don&rsquo;t provide it explicitly to the <code>inject</code> method? Will it even compile or maybe type inference will be able to figure out the type?</p> <pre><code>val server = inject val server1: HttpServer = inject </code></pre> <p>Unfortunately, in both cases it will compile, but the inferred type would be <code>Nothing</code>. It appears to be a <a href="https://issues.scala-lang.org/browse/SI-2609" target="_blank">known problem</a> and it will not be fixed. Ideally I would like to see a compilation error in this case, so I started to look for a solution. After some time I actually was able to find the way to prevent <code>Nothing</code> inference and, by taking this idea even further, found a way to define type constraints in a very generically in the method signature. They will allow you, for example, to describe union types with <code>(T =:= String) Or (T =:= Int)</code> or even more complex rules like <code>(T &lt;:&lt; Color) And Not[T =:= Color] And Not[(T =:= Yellow.type) Or (T =:= Blue.type)]</code>.</p> <p><!-- more --></p> <h3>Preventing &ldquo;Nothing&rdquo; Inference</h3> <p>Type <code>Nothing</code> has very interesting property by being a <a href="http://en.wikipedia.org/wiki/Bottom_type" target="_blank">bottom type</a>, which means that it&rsquo;s a subtype of all other classes. And it turns out that you can use (or misuse) this property in order to implement a type class, that can detect that something is <strong>not</strong> <code>Nothing</code>, because all other classes simply don&rsquo;t have this property (except other bottom types). So lets define <code>NotNothing</code> type class:</p> <pre><code>@implicitNotFound("Sorry, type inference was unable to figure out the type. You need to provide it explicitly.") trait NotNothing[T] object NotNothing { private val evidence: NotNothing[Any] = new Object with NotNothing[Any] implicit def notNothingEv[T](implicit n: T =:= T): NotNothing[T] = evidence.asInstanceOf[NotNothing[T]] } </code></pre> <p>Interestingly enough compiler would not be able to find an <code>NotNothing[Nothing]</code> evidence in this case. It still puzzles me why exactly this is the case, but looks like it has something to do with type inference algorithm. I made a small experiment that shows, that as soon as compiler tries to infer T, then <code>=:=</code> will fail for <code>Nothing</code>:</p> <pre><code>scala&gt; def foo[T](implicit a: T =:= T) = a foo: [T](implicit a: =:=[T,T])=:=[T,T] scala&gt; foo[Nothing] res2: =:=[Nothing,Nothing] = &lt;function1&gt; scala&gt; foo &lt;console&gt;:9: error: type mismatch; found : =:=[Nothing,Nothing] required: =:=[T,T] Note: Nothing &lt;: T, but class =:= is invariant in type From. You may wish to investigate a wildcard type such as `_ &lt;: T`. (SLS 3.2.10) Note: Nothing &lt;: T, but class =:= is invariant in type To. You may wish to investigate a wildcard type such as `_ &lt;: T`. (SLS 3.2.10) foo ^ </code></pre> <p>If you know exactly how and why it works like this, then I would appreciate it you share it in the comments.</p> <p>Anyways, this seem to work, so let&rsquo;s define <code>inject</code> method in terms of <code>NotNothing</code>:</p> <pre><code>def inject[T](implicit tt: TypeTag[T], nn: NotNothing[T]) = tt.toString </code></pre> <p>and then test it in REPL:</p> <pre><code>scala&gt; inject &lt;console&gt;:12: error: Sorry, type inference was unable to figure out the type. You need to provide it explicitly. inject ^ scala&gt; inject[Nothing] &lt;console&gt;:12: error: Sorry, type inference was unable to figure out the type. You need to provide it explicitly. inject[Nothing] ^ scala&gt; val foo: String = inject &lt;console&gt;:11: error: Sorry, type inference was unable to figure out the type. You need to provide it explicitly. val foo: String = inject ^ scala&gt; inject[String] res6: String = TypeTag[String] scala&gt; inject[Int] res7: String = TypeTag[Int] </code></pre> <p>As you can see, it works as expected - code does not compile as soon as <code>Nothing</code> is involved. And as a bonus, you can even specify a nice error message with <code>implicitNotFound</code> annotation.</p> <h3>Implementing &ldquo;Not&rdquo; Type Class</h3> <p>It this point I decided to bring this idea even further. If I&rsquo;m able to produce an evidence that some type <code>T</code> is not <code>Nothing</code>, then I also should be able to prove, that some type <code>T</code> is does <strong>not</strong> have some evidence. It actually was my dream for quite some time to be able to get an evidence that some other evidence does not exist rather than get an evidence when it exists (which is the standard ways to work with type classes). It turns out that it&rsquo;s not only possible, but also pretty straightforward to implement.</p> <p>In order to do this, we need some type to represent an existence/non-existence:</p> <pre><code>sealed trait Existence trait Exists extends Existence trait NotExists extends Existence </code></pre> <p>Now lets create a type class, that can prove, that some other evidence exists or does not exist:</p> <pre><code>trait IsTypeClassExists[TypeClass, Answer] object IsTypeClassExists { private val evidence: IsTypeClassExists[Any, Any] = new Object with IsTypeClassExists[Any, Any] implicit def typeClassExistsEv[TypeClass, Answer](implicit a: TypeClass) = evidence.asInstanceOf[IsTypeClassExists[TypeClass, Exists]] implicit def typeClassNotExistsEv[TypeClass, Answer] = evidence.asInstanceOf[IsTypeClassExists[TypeClass, NotExists]] } </code></pre> <p>And finally lets implement <code>Not</code> type class, that can be easily expressed in terms of <code>IsTypeClassExists</code>:</p> <pre><code>@implicitNotFound("Argument does not satisfy constraints: Not ${T}") trait Not[T] object Not { private val evidence: Not[Any] = new Object with Not[Any] implicit def notEv[T, Answer](implicit a: IsTypeClassExists[T, Answer], ne: Answer =:= NotExists) = evidence.asInstanceOf[Not[T]] } </code></pre> <p>This type class is pretty powerful and can give us a proof, that some other type class does not exist for some arbitrary type <code>T</code>. Lets first reimplement <code>inject</code> method in terms of <code>Not</code>:</p> <pre><code>def inject[T](implicit tt: TypeTag[T], nn: Not[T =:= Nothing]) = tt.toString </code></pre> <p>and repeat our REPL session:</p> <pre><code>scala&gt; inject &lt;console&gt;:11: error: Argument does not satisfy constraints: Not =:=[T,Nothing] inject ^ scala&gt; inject[Nothing] &lt;console&gt;:11: error: Argument does not satisfy constraints: Not =:=[Nothing,Nothing] inject[Nothing] ^ scala&gt; val foo: String = inject &lt;console&gt;:10: error: Argument does not satisfy constraints: Not =:=[T,Nothing] val foo: String = inject ^ scala&gt; inject[String] res3: String = TypeTag[String] scala&gt; inject[Int] res4: String = TypeTag[Int] </code></pre> <p>As you can see, it behaves exactly like previous version, but now it&rsquo;s implemented in much more generic way. Error message is a little bit more cryptic but I find it pretty straightforward to understand it&rsquo;s meaning.</p> <h3>Describing Arbitrary Complex Type Constraints</h3> <p>In order to make this picture complete, we are still missing <code>Or</code> and <code>And</code> type classes. Implementing these should be even easier than <code>Not</code>, so let&rsquo;s start with <code>And</code>:</p> <pre><code>@implicitNotFound("Argument does not satisfy constraints: ${A} And ${B}") trait And[A, B] object And { private val evidence: And[Any, Any] = new Object with And[Any, Any] implicit def bothExistEv[A, B](implicit a: A, b: B) = evidence.asInstanceOf[And[A, B]] } </code></pre> <p>and <code>Or</code>:</p> <pre><code>@implicitNotFound("Argument does not satisfy constraints: ${A} Or ${B}") trait Or[A, B] object Or { private val evidence: Or[Any, Any] = new Object with Or[Any, Any] implicit def aExistsEv[A, B](implicit a: A) = evidence.asInstanceOf[Or[A, B]] implicit def bExistsEv[A, B](implicit b: B) = evidence.asInstanceOf[Or[A, B]] } </code></pre> <p>Now we should be able to describe any kind of logical expressions, that constrain the type, in the method signature. Let&rsquo;s go though some examples.</p> <p><strong>Union types? No problem whatsoever</strong></p> <pre><code>def union[T](t: T)(implicit c: (T =:= String) Or (T =:= Int)) = t match { case s: String =&gt; println(s"Some nice string: $s") case i: Int =&gt; println(s"Some int: $i") } </code></pre> <p>or if like, you can define nice type lambda:</p> <pre><code>type V[A, B] = {type l[T] = (T &lt;:&lt; A) Or (T &lt;:&lt; B)} def union[T: (String V Int)#l](t: T) = t match { case s: String =&gt; println(s"Some nice string: $s") case i: Int =&gt; println(s"Some int: $i") } </code></pre> <p>and make sure that it works as expected:</p> <pre><code>scala&gt; union("Test") Some nice string: Test scala&gt; union(265) Some int: 265 scala&gt; union(2.0) &lt;console&gt;:11: error: Argument does not satisfy constraints: =:=[Double,String] Or =:=[Double,Int] union(2.0) ^ </code></pre> <p><strong>More complex examples</strong></p> <p>Here I want to sort the list of any element type except lists with strings and numbers in it</p> <pre><code>def sort[T: Ordering](list: List[T])(implicit c: Not[(T =:= String) Or Numeric[T]]) = list.sorted </code></pre> <p>Or in this example I want method to accept only specific colors:</p> <pre><code>sealed trait Color case object Red extends Color case object Green extends Color case object Blue extends Color case object Yellow extends Color case object Magenta extends Color def printColor[T](t: T)(implicit c: (T =:= Red.type) Or (T =:= Green.type) Or (T =:= Magenta.type)) = println(t) </code></pre> <p>or instead of defining a whitelist, I can define a blacklist:</p> <pre><code>def printColor[T](t: T)(implicit c: (T &lt;:&lt; Color) And Not[T =:= Color] And Not[(T =:= Yellow.type) Or (T =:= Blue.type)]) = println(t) </code></pre> <p>and in REPL:</p> <pre><code>scala&gt; printColor(Yellow) &lt;console&gt;:11: error: Argument does not satisfy constraints: And[&lt;:&lt;[Yellow.type,Color],Not[=:=[Yellow.type,Color]]] And Not[Or[=:=[Yellow.type,Yellow.type],=:=[Yellow.type,Blue.type]]] printColor(Yellow) ^ scala&gt; val c: Color = Magenta c: Color = Magenta scala&gt; printColor(c) &lt;console&gt;:12: error: Argument does not satisfy constraints: And[&lt;:&lt;[Color,Color],Not[=:=[Color,Color]]] And Not[Or[=:=[Color,Yellow.type],=:=[Color,Blue.type]]] printColor(c) ^ scala&gt; printColor(Magenta) Magenta </code></pre> <p>This is the power of type classes. I hope you enjoyed! I also created <a href="https://gist.github.com/OlegIlyenko/8508980" target="_blank">a gist for you to play with</a>.</p>http://hacking-scala.org/post/73854628325http://hacking-scala.org/post/73854628325Sun, 19 Jan 2014 19:54:00 +0100type classesEasy Dependency Injection in Play Framework with Scaldi<p>In this post I would like to make small introduction to Scaldi and show you how easy it is to use it together with Play. <a href="http://scaldi.github.io/scaldi/Scaldi.html" target="_blank">Scaldi</a> is dependency injection library for Scala. It&rsquo;s very lightweight (without any dependencies) and provides nice Scala DSL for binding dependencies and injecting them. It&rsquo;s more dynamic approach to do dependency injection in comparison to the cake pattern, because not everything is checked at the compile time. This can be seen as an disadvantage, but I personally believe, that there is place for this approach. It can be vary useful in many circumstances and many people can find it more natural (and easy), especially if they are coming from Java background.</p> <p>There are only 3 most important traits that you need to know, in order to make dependency injection with Scaldi:</p> <ul><li><strong>Injector</strong> - it&rsquo;s a container for the bindings, that you have defined in the module.</li> <li><strong>Module</strong> - gives you nice syntax to create bindings with <code>bind</code> and <code>binding</code>. <code>Module</code> also extends <code>Injector</code> trait and implicit <code>Injector</code> instance always available when you are defining your bindings</li> <li><strong>Injectable</strong> - the only responsibility of this trait is to provide you with <code>inject</code> function (so it just provides nice syntax for injecting dependencies). It&rsquo;s important to understand, that it&rsquo;s the only the purpose of it. So it completely stateless and knows nothing about actual bindings you have defined in the module. In order to actually find and inject dependencies, <code>inject</code> function always takes an implicit parameter of type <code>Injector</code></li> </ul><p><!-- more --></p> <h3>Defining Managed Classes</h3> <p>Let&rsquo;s take a small example. Suppose you have have <code>MessageService</code> trait like this:</p> <pre><code>trait MessageService { def getGreetMessage(name: String): String } </code></pre> <p>Now let&rsquo;s define one implementation of it, where we will inject greeting string:</p> <pre><code>class OfficialMessageService(implicit inj: Injector) extends MessageService with Injectable { val officialGreeting = inject [String] (identified by "greeting.official") def getGreetMessage(name: String) = s"$officialGreeting, $name!" } </code></pre> <p>You have probably noticed 2 things, that are required for injection to work:</p> <ul><li>We extended <code>Injectable</code> in order to make <code>inject</code> function available</li> <li>We declared implicit parameter of type <code>Injector</code> in order to provide bindings to the inject function.</li> </ul><p>You are not required to always extend <code>Injectable</code> in order to use <code>inject</code> - you can just import it. This will work as good as in previous example:</p> <pre><code>import scaldi.Injectable._ class SomeService(implicit inj: Injector) extends MessageService { val dep = inject [SomeOtherService] } </code></pre> <h3>Creating a Module</h3> <p>Now that we&rsquo;ve created a managed class, we need to add it to the <code>Module</code>:</p> <pre><code>class UserModule extends Module { bind [MessageService] to new OfficialMessageService binding identifiedBy "greeting.official" to "Welcome" } </code></pre> <p>In the module context you are able to instantiate <code>OfficialMessageService</code> because it always have implicit <code>Injector</code> available in a scope, as I mentioned earlier. I also defined another binding with identifier <code>greeting.official</code> because <code>OfficialMessageService</code> needs it and will try to inject it at some point.</p> <h3>Integration with Play App</h3> <p>Now we will try to integrate our new module in the Play app. The first thing you need to do is to add dependency on <strong>scaldi-play</strong> in the project file (<code>build.sbt</code>). Something like this:</p> <pre><code>name := "scaldi-play-example" version := "1.0-SNAPSHOT" libraryDependencies ++= Seq( "com.github.scaldi" %% "scaldi-play" % "0.2.2" ) play.Project.playScalaSettings </code></pre> <p>Now you are able to use Scaldi in the project. Play application normally has it&rsquo;s initialization logic in the <code>Global</code> object, so we need to add <code>ScaldiSupport</code> in it:</p> <pre><code>object Global extends GlobalSettings with ScaldiSupport { def applicationModule = new UserModule } </code></pre> <p>Nice! Your Play application is now uses Scaldi for the dependency injection, but unfortunately it doesn&rsquo;t do anything, so let&rsquo;s fix it. Let&rsquo;s create a simple index page (<code>app/views/index.scala.html</code>) that will show greeting message to the user:</p> <pre><code>@(message: String) @main("Test Page") { &amp;lt;h1&amp;gt;@message&amp;lt;/h1&amp;gt; } </code></pre> <p>Now we need to create a controller for it. But instead of creating a singleton object, let&rsquo;s make a managed class (simmilat to the <code>OfficialMessageService</code> class):</p> <pre><code>class Application(implicit inj: Injector) extends Controller with Injectable { val messageService = inject [MessageService] def index = Action { Ok(views.html.index(messageService.getGreetMessage("Test User"))) } } </code></pre> <p>As you can see, <code>MessageService</code> is injected and its used to produce nice greet message.</p> <p>As you probably already aware, Play only works with singleton controllers, which means, that it requires <code>Application</code> controller to be an <code>object</code> instead of <code>class</code>. Thankfully Play 2.1 introduced new feature, which makes integration with dependency injection framework like Scaldi possible. In order to use this feature, you need to prefix managed controllers with <code>@</code> in the <code>conf/routes</code> file. So let&rsquo;s make it:</p> <pre><code>GET / @controllers.Application.index </code></pre> <p>The one last thing that remains is to add controller to the new module:</p> <pre><code>class WebModule extends Module { binding to new Application } </code></pre> <p>and to compose <code>WebModule</code> with <code>UserModule</code> in the <code>Global</code> object:</p> <pre><code>object Global extends GlobalSettings with ScaldiSupport { def applicationModule = new WebModule :: new UserModule } </code></pre> <p>At this point you should be able to run Play app and view the index page, that will show you: <code>Welcome, Test User!</code>.</p> <h3>Injecting Play Configuration</h3> <p><strong>scaldi-play</strong> provides integration with Play configuration (<code>conf/application.conf</code>) out of the box. So you can, for example, define <code>greeting.official</code> property there:</p> <pre><code>greeting.official = Welcome </code></pre> <p>and then just remove one extra binding for it from the <code>UserModule</code>:</p> <pre><code>class UserModule extends Module { bind [MessageService] to new OfficialMessageService } </code></pre> <p>It will continue to work as before. You can also inject other primitive types like <code>Int</code> or <code>Boolean</code> and not only <code>String</code>. If you would like to use <code>configuration</code> object directly, then you need inject it like this:</p> <pre><code>val config = inject [play.api.Configuration] </code></pre> <h3>Distinguishing Between Modes</h3> <p>Suppose we have another implementation of <code>MessageService</code> that we want to use when Play app is in the <code>dev</code> or <code>test</code> mode:</p> <pre><code>class SimpleMessageService extends MessageService { def getGreetMessage(name: String) = s"Hi, $name" } </code></pre> <p>With Scaldi it&rsquo;s pretty easy to make. You just need to define <code>UserModule</code> like this:</p> <pre><code>class UserModule extends Module { bind [MessageService] when (inDevMode or inTestMode) to new SimpleMessageService bind [MessageService] when inProdMode to new OfficialMessageService } </code></pre> <p><code>inDevMode</code>, <code>inTestMode</code> and <code>inProdMode</code> are just functions that produce <code>Condition</code> objects. <code>Condition</code>s are used by Scaldi to decide, whether binding is available for injection.</p> <p>I hope you liked this small introduction to <a href="http://scaldi.github.io/scaldi/Scaldi.html" target="_blank">scaldi</a> and <a href="https://github.com/scaldi/scaldi-play" target="_blank">scaldi-play</a>. You can find example application, that I described here, in this github repo:</p> <p><a href="https://github.com/scaldi/scaldi-play-example" target="_blank">https://github.com/scaldi/scaldi-play-example</a></p> <p>Please feel free to fork and hack it :)</p>http://hacking-scala.org/post/51407241538http://hacking-scala.org/post/51407241538Sun, 26 May 2013 20:21:00 +0200scaldiscaldi-playplayplayframeworkdependency injectionRegular Expressions Interpolation in Pattern Matching <p>With introduction of <em>string interpolation</em> in Scala 2.10 we finally got a feature, that many modern languages already have. What made me extremely exited though, is the fact, how good this feature was integrated into the language and how customizable it is. In this post I would like to show you some examples of it. I will not concentrate on explaining how exactly it works, but instead I will show you one very cool application of it, which I found recently. It combines string interpolation with regular expressions. What is especially interesting is the way you can use it in the pattern matching expressions.</p> <p>It is something that I wanted to be able to make for a long time and actually <a href="/post/49051516694/introduction-to-type-dynamic" target="_blank">found one way to implement with type Dynamic</a>. That solution was a little bit crazy and looked like something that you normally will never use in real projects. Now I&rsquo;m happy to show you solution that is actually looks nice (at least for regular expressions). I also want to notice that things I would like to share with you are shamelessly stolen from (were inspired by) this <a href="http://marakana.com/s/post/1461/what_is_new_in_scala_2_10_adriaan_moors_video" target="_blank">video introduction to Scala 2.10 features</a> by <a href="https://twitter.com/adriaanm" target="_blank">Adriaan Moors</a> and <a href="http://stackoverflow.com/a/16256935/576766" target="_blank">this answer at StackOverflow</a> by <a href="http://stackoverflow.com/users/465304/sschaef" target="_blank">sschaef</a>. I want to thank authors for giving me inspiration and educating me :)</p> <!-- more --> <h3>Adding String Interpolation to Regular Expressions</h3> <p>You probably already know how to create and use regexps in older versions of Scala:</p> <pre><code>val FromToPattern = """(\d+) - (\d+)""".r val FromToPattern(from, to) = "100 - 200" println(s"From: $from, To: $to") // From: 100, To: 200 </code></pre> <p>This code generally defines a regexp pattern and uses it in pattern match.</p> <p>In Scala 2.10 we now have <code>StringContext</code> which compiler creates for interpolated strings. And you can actually enrich it with new methods, that can interpret interpolated string differently. That&rsquo;s exactly what I will implement for regular expressions by using <em>implicit class</em>:</p> <pre><code>import util.matching.Regex implicit class RegexContext(sc: StringContext) { def r = new Regex(sc.parts.mkString, sc.parts.tail.map(_ =&gt; "x"): _*) } </code></pre> <p>It&rsquo;s all you need in order to be able to make pattern matching on regular expressions directly in <code>match</code> - <code>case</code>:</p> <pre><code>"Amount is 100 USD" match { case r"Amount is (\d+)$amount ([A-Z]{3})$currency" =&gt; println(s"Amount: $amount, Currency: $currency") } // Amount: 100, Currency: USD </code></pre> <p>As you can see I even able to save captured groups in the variables <code>amount</code> and <code>currency</code>.</p> <h3>Combining Extractors within Regular Expression</h3> <p>Now I would like to show you more complicated example. This time I would like to save both amount and currency in <code>Money</code> object:</p> <pre><code>case class Money(amount: Int, currency: String) </code></pre> <p>In order extract it from the string, I will define custom extractor for it, that generally parses string and extracts both values:</p> <pre><code>object Money { def unapply(str: String) = str match { case r"(\d+)$amount\s+([A-Z]{3})$currency" =&gt; Some(Money(amount.toInt, currency)) case _ =&gt; None } } </code></pre> <p>With these definitions I&rsquo;m able to &ldquo;chain&rdquo; these extractors together directly in regexp like this:</p> <pre><code>"Amount is 100 USD" match { case r"Amount is (.+)${Money(money)}" =&gt; println(money.copy(amount = money.amount + 15)) } // Money(115,USD) </code></pre> <h3>Regexp Pattern Matching in val Definition</h3> <p>As you can imagine, you can also apply the same mechanism in all places, where you can use pattern matching. This includes <code>val</code> definitions. Let&rsquo;s rewrite our first example:</p> <pre><code>object Int { def unapply(str: String) = Try(str.toInt).toOption } val r"(\d+)${Int(from)} - (\d+)${Int(to)}" = "45 - 123" println(s"from: $from, to: $to") // from: 45, to: 123 </code></pre>http://hacking-scala.org/post/50360896036http://hacking-scala.org/post/50360896036Mon, 13 May 2013 21:57:00 +0200Scala 2.10pattern matchingregular expressionsstring interpolationcustom extractorsIntroductory Scala Presentation<p>Recently I gave introductory tech talk about Scala at my company. The target audience were mostly Java developers. So I tried to give some impression about Scala that, from one hand, tries to explain basics, but from other hand, tries to motivate people to learn Scala further.</p> <p>Presentation consists of two parts, which I believe are very important for any project and team:</p> <ul><li><strong>Language features</strong> - Scala has a lot of them, so I tried to show highlights and made some comparisons to Java</li> <li><strong>Scalability</strong> - it can be viewed from different perspectives, so I tried to show some of them</li> </ul><!-- more --><p>A feedback I received was very positive, so I decided to share this presentation with you. Please feel free to share, copy and modify it for your needs. I really hope it will help you to move Scala adoption forward in your organization!</p> <p>And as always, your feedback is highly appreciated!</p> <iframe src="http://prezi.com/embed/opczxtrml2ib/?bgcolor=ffffff&amp;lock_to_path=0&amp;autoplay=1&amp;autohide_ctrls=0&amp;features=undefined&amp;disabled_features=undefined" width="680" height="600" frameborder="0"></iframe>http://hacking-scala.org/post/49090249019http://hacking-scala.org/post/49090249019Sun, 28 Apr 2013 16:03:00 +0200prezipresentationbasicsscalabilityfeaturesIntroduction to Type Dynamic<p>In Scala 2.10 class <code>Dynamic</code> would be enabled by default, so I think that it&rsquo;s the right time to look at it in more detail. This class can be used for many different purposes. I think, that an integration with dynamic languages is the most obvious one, but I would like to show you something different. I will demonstrate you how to create parameterizable extractors, that can be parametrized directly within <code>case</code> expressions.</p> <p>Class <code>Dynamic</code> works very similar to Ruby&rsquo;s <code>method_missing</code> or Groovy&rsquo;s <code>methodMissing</code>/<code>propertyMissing</code>. So you can call non-existing methods on classes that extend <code>Dynamic</code>. Such calls would be rewritten by compiler to following method invocations: <code>applyDynamic</code>, <code>applyDynamicNamed</code>, <code>selectDynamic</code>, <code>updateDynamic</code>. I will describe each of these methods in the next sections.</p> <p><!-- more --></p> <h3><code>applyDynamic</code></h3> <p>It&rsquo;s, probably, the most popular method. You can define it like this:</p> <pre><code>class DynamicTest extends Dynamic { def applyDynamic(methodName: String)(args: Any*) { println(s"You called '$methodName' method with " + s"following arguments: ${args mkString ", "}") } } </code></pre> <p>And as you can see, I can call non-exiting methods on instances of the <code>DynamicTest</code> class:</p> <pre><code>scala&gt; val test = new DynamicTest test: DynamicTest = DynamicTest@fb7ac7 scala&gt; test.method1("a", "b", 123) You called 'method1' method with following arguments: a, b, 123 scala&gt; test method2 "testing" You called 'method2' method with following arguments: testing </code></pre> <h3><code>applyDynamicNamed</code></h3> <p>I find it very nice, that named arguments are also supported. Each argument that you pass to the function is converted to the <code>Tuple</code> where the first element of the tuple is argument name and the second is value. Here is its signature:</p> <pre><code>class DynamicTest extends Dynamic { def applyDynamicNamed(name: String)(args: (String, Any)*) { println(s"You called '$name' method with " + s"following argiuments: ${args map (a =&gt; a._1 + "=" + a._2) mkString ", "}") } } </code></pre> <p>And its usage (as you can see, argument name would be empty string if you don&rsquo;t use named argument):</p> <pre><code>scala&gt; test.createUser("abc", name = "John", age = 20) You called 'createUser' method with following argiuments: =abc, name=John, age=20 </code></pre> <p>By the way, in all examples above I have used <code>Unit</code> as return type, but you actually not limited to it. You can return anything you want from these methods, you can even have as many type parameters as you want.</p> <h3><code>selectDynamic</code></h3> <p>Now comes setter/getter method (properties) support. If you want to handle method invocation, that does not involve any arguments or parenthesis, than you need <code>selectDynamic</code> - it&rsquo;s probably the simplest of them (still it can be very useful as you will see later in my bigger example). You can define it like this:</p> <pre><code>class DynamicTest extends Dynamic { def selectDynamic(name: String) = s"value of $name" } </code></pre> <p>And its usage:</p> <pre><code>scala&gt; test.firstName res0: String = value of firstName scala&gt; test.`some other property ?` res1: String = value of some other property ? </code></pre> <h3><code>updateDynamic</code></h3> <p>This is setter part. You can use it to handle assignments. Its signature:</p> <pre><code>class DynamicTest extends Dynamic { def updateDynamic(name: String)(value: Any) { println(s"You have just updated property '$name' with value: $value") } } </code></pre> <p>Since the REPL generates code to report a value after an assignment, the following won&rsquo;t work yet. (You can witness the generated code with the special <code>show</code> comment.)</p> <pre><code>scala&gt; test.firstName = "John" // show [...] test.firstName = "bob"; val $ires0 = test.firstName [...] :12: error: value selectDynamic is not a member of DynamicTest val $ires0 = test.firstName ^ </code></pre> <p>You can avoid this effect by using a local statement:</p> <pre><code>scala&gt; { test.name = "bob" ; () } You have just updated property 'name' with value: bob </code></pre> <p>Or by just supplying <code>selectDynamic</code>:</p> <pre><code>class DynamicTest extends Dynamic { var value: String = "" def updateDynamic(name: String)(value: Any) = this.value = s"$name is $value" def selectDynamic(name: String) = value } </code></pre> <p>With the expected result:</p> <pre><code>scala&gt; test.name = "bob" test.name: String = name is bob </code></pre> <h3>Parameterizable Extractors</h3> <p>This was my dream for a long time! Wouldn&rsquo;t it be nice, if you can specify regexp directly in pattern matching expression instead of defining it elsewhere and then use your variable in pattern match or to extract specific elements of the <code>Map</code>? With type <code>Dynamic</code> you can actually archive this, and here is example its usage:</p> <pre><code>Map("firstName" -&gt; "John", "lastName" -&gt; "Doe") match { case p.firstName.lastName.Map( Some(p.Jo.StartsWith(fn)), Some(p.`.*(\\w)$`.Regexp(lastChar))) =&gt; println(s"Match! $fn ...$lastChar") case _ =&gt; println("nope") } </code></pre> <p>As you see, I have provided parameters to the extractor and performed pattern match in the same <code>case</code> expression. One <code>selectDynamic</code> is enough to implement it:</p> <pre><code>class ExtractorParams(params: List[String]) extends Dynamic { val Map = new MapExtractor(params) val StartsWith = new StartsWithExtractor(params) val Regexp = new RegexpExtractor(params) def selectDynamic(name: String) = new ExtractorParams(params :+ name) } object p extends ExtractorParams(Nil) </code></pre> <p>As you can see, each call to the <code>selectDynamic</code> adds on parameter to the parameter <code>List</code>, and when I&rsquo;m finished with parameters, I can call concrete extractor like <code>StartsWith</code> or <code>Map</code>. These extractor implementations are pretty straightforward:</p> <pre><code>class RegexpExtractor(params: List[String]) { def unapplySeq(str: String) = params.headOption flatMap (_.r unapplySeq str) } class StartsWithExtractor(params: List[String]) { def unapply(str: String) = params.headOption filter (str startsWith _) map (_ =&gt; str) } class MapExtractor(keys: List[String]) { def unapplySeq[T](map: Map[String, T]) = Some(keys.map(map get _)) } </code></pre> <h3>Conclusion</h3> <p>I hope you enjoyed this small introduction to new type <code>Dynamic</code>. I also hope, that I was able to demonstrate you, that it can be used for pretty unexpected use-cases and not only for integration with dynamic languages. You can find more information about <code>Dynamic</code> type in the <a href="http://docs.scala-lang.org/sips/pending/type-dynamic.html" target="_blank">correspondent SIP-17</a>.</p>http://hacking-scala.org/post/49051516694http://hacking-scala.org/post/49051516694Sun, 28 Apr 2013 03:07:00 +0200scala 2.10dynamicmethod_missingparametrizationpattern matchingProgress Monitoring For Streams<p>In this blog post I would like to show, how you can implement simple monitoring capabilities for standard Java&rsquo;s <code>InutStream</code> and <code>OutputStream</code>. By &ldquo;monitoring capabilities&rdquo; I mean ability to find out information like Speed, Estimated Time, Transfered Size, etc. Recently I implemented this for our internal deployment tool (simple Swing-based application written in Scala). After about 30 minutes of googling without much success, I actually tried to think about this and found out how simple it actually is. So I decided to share this with you. Additionally I also want to give you small demonstration of scala-swing which I&rsquo;m enjoying a lot.</p> <p>After reading this article you will find out how you can write application like this one:</p> <p><img src="http://github.com/OlegIlyenko/my-blog-images/raw/master/posterous/downloader.png" alt="Downloader"/></p> <!-- more --> <h3>Creating Stream Filter</h3> <p>In order to get all metrics shown above, we need to know the full size of the file/stream and currently transfered amount of bytes. The full size is easy to find out from outside of the <code>Stream</code>, especially if you are working with files, but we need to monitor <code>Stream</code> in order to find out how much bytes are transfered already. Here is an example of such <code>InputStream</code>, that reports this with specified threshold:</p> <script src="https://gist.github.com/OlegIlyenko/2387873.js"></script><p>You have probably noticed <code>ProgressListener</code>. It&rsquo;s function <code>Long =&amp;gt; Unit</code> which we will discuss next.</p> <h3>ProgressListener class</h3> <p>In order to show transferred number of bytes, we need to remember it somehow. <code>ProgressListener</code> function makes just this + calculates another metrics:</p> <script src="https://gist.github.com/OlegIlyenko/2387880.js"></script><p>What&rsquo;s interesting about this, is that I do cont define function with function literal like: <code>(bytes: Long) =&amp;gt; Unit</code>. Instead I&rsquo;m creating a new class that extends <code>Function1[Long, Unit]</code> - it&rsquo;s allowed in Scala and sometimes can be very useful. Actually many Scala&rsquo;s collection classes also making this, so you can get i-th element of the list like this: <code>myList(i)</code>. In my case, it allowed me to have some internal state (<code>transfered</code> and <code>startTime</code> fields) and to parametrize function with some arguments (<code>availableBytes</code> and <code>tracker</code>) during it&rsquo;s creation. It&rsquo;s also possible to use function literal for this and still archive the same results - you just need to use currying and closures in this case. But I find, that it&rsquo;s more clear and readable to define <code>ProgressListener</code> as class in this particular case.</p> <p>You can tweak this algorithm and make it more accurate. For example you can calculate <code>bps</code> depending on the bytes, that were transferred during last several seconds. Still I think, that this algorithm does it&rsquo;s job for many use cases.</p> <h3>Progress class</h3> <p>Now let&rsquo;s take a look at <code>Progress</code> case class. It&rsquo;s purpose is to hold different metrics and format it when needed (formatting can be probably done somewhere else, but I decided to put it here just to keep it simple).</p> <script src="https://gist.github.com/OlegIlyenko/2387889.js"></script><p>Here you can also find nice utility functions to format size and time.</p> <h3>User Interface With Scala-Swing</h3> <p>Now let&rsquo;s create simple application that downloads files and shows the progress:</p> <script src="https://gist.github.com/OlegIlyenko/2387901.js"></script><p>This application throws all downloaded bytes away, so it&rsquo;s pretty useless, but I hope that it will demonstrate you scala-swing basics.</p> <h3>Conclusion</h3> <p>Hopefully this will also give you some impressions on how easy it is to integrate with Java standard library from Scala. For your convenience I also <a href="https://gist.github.com/2388377" target="_blank">created gist with all these classes in it</a>, so you can just copy/paste it in your own projects and start playing with it.</p>http://hacking-scala.org/post/49050550539http://hacking-scala.org/post/49050550539Sun, 28 Apr 2013 02:52:00 +0200iojava integrationmonitoringscala-swingstreamsswingScalaz - Resources For Beginners<p><a href="http://code.google.com/p/scalaz/" target="_blank">Scalaz</a> is very interesting Scala library. It can be pretty scary when you first look at it and at examples of it&rsquo;s usage. I also find, that at the beginning, advantages of this library are not very obvious. So at some point I asked myself: why are people so enthusiastic about it? So I started to learn it, but found that it&rsquo;s hard to find any resources that are targeting beginners - people who are new to Haskell, Category Theory, or advanced Scala features like <em>Type Classes</em> or <em>Higher Kinds</em>.</p> <p>In this post I want to summarize all resources I found about scalaz or related to scalaz that are actually approachable by beginners. I started with <a href="http://stackoverflow.com/questions/4863671/good-scalaz-introduction" target="_blank">question at StackOverflow</a> where I received many good answers. I also noticed, that this question was pretty popular, so I decided to write this post where I can summarize the answers and maybe add something more.</p> <p>I personally believe that even if you don&rsquo;t completely understand every aspect of scalaz yet (I&rsquo;m definitely not), it still can help you to write a better code. As far as I can can see, it helps to write more composable, reusable and side-effect free code. But in order to use it, you need to understand some key concepts that it heavily relies on. So let&rsquo;s start with &hellip;</p> <!-- more --> <h3>Type Classes</h3> <p>Type Classes playing important role in Scala and they are really useful concept. They generally allow you to define some additional behavior (methods) to the class independently from this class itself. I believe, that this concept was taken from Haskell and implemented in Scala with the help of implicit parameters. Type classes can also help (at least partly) to solve <a href="http://en.wikipedia.org/wiki/Expression_problem" target="_blank">Expression problem</a>. You can find these presentations helpful on this topic:</p> <ul><li><a href="http://vimeo.com/10482466" target="_blank">Scalaz Presentation</a> (by <a href="https://twitter.com/#!/nkpart" target="_blank">Nick Partridge</a>)</li> <li><a href="http://days2011.scala-lang.org/node/138/275" target="_blank">The ease of Scalaz</a> (by <a href="https://twitter.com/#!/hseeberger" target="_blank">Heiko Seeberger</a>)</li> </ul><p>I can highly recommend Nick&rsquo;s presentation - it&rsquo;s mostly not about Scalaz, but about the concepts it uses. Step by step, he implements these concepts in a simple example. For me personally it was eye opening. Heiko shows some of the features of Scalaz, including interesting of type-safe equals implemented with type class.</p> <p>I can also recommend you to watch Daniel&rsquo;s presentation, described in the next section, which touches this topic.</p> <h3>Higher Kinds</h3> <blockquote> <p><em>Values</em> are to <em>types</em> as <em>types</em> are to <em>kinds</em> - <em>Daniel Spiewak</em></p> </blockquote> <p>It&rsquo;s very nice feature of Scala language, which allows you to work with containers like <code>List</code> or <code>Option</code> in very generic manner. I highly recommend you this watch this presentation on this topic:</p> <ul><li><a href="http://vimeo.com/28793245" target="_blank">High Wizardry in the Land of Scala</a> (by <a href="https://twitter.com/#!/djspiewak" target="_blank">Daniel Spiewak</a>)</li> </ul><p>Daniel describes 3 concepts: <em>Higher Kinds</em>, <em>Type Classes</em> and <em>Continuations</em>. He also shows, how you can implement heterogeneous list with this knowledge, as an example.</p> <h3>Scalaz Itself</h3> <p>Here are some inspiring presentation about scalaz, which I would highly recommend:</p> <ul><li><a href="http://skillsmatter.com/podcast/scala/scalaz" target="_blank">Scalaz</a> (by <a href="https://twitter.com/#!/oxbow_lakes" target="_blank">Chris Marshall</a>)</li> <li><a href="http://skillsmatter.com/podcast/scala/practical-scalaz-2518" target="_blank">Practical Scalaz: making your life easier the hard way</a> (by <a href="https://twitter.com/#!/oxbow_lakes" target="_blank">Chris Marshall</a>)</li> <li><a href="http://www.infoq.com/presentations/Scalaz-Functional-Programming-in-Scala" target="_blank">Scalaz: Functional Programming in Scala</a> (by <a href="https://twitter.com/#!/runarorama" target="_blank">Rúnar Óli Bjarnason</a>)</li> </ul><p>in these videos Chris gives introduction to some useful features of Scalaz, describing them from the more practical perspective.</p> <p>If you want nice and quick introduction to scalaz, than I can recommend you this very nice <a href="https://github.com/arosien/scalaz-base-talk-201208/raw/master/scalaz-cheatsheet.pdf" target="_blank">Scalaz &ldquo;For the Rest of Us&rdquo; Cheat Sheet</a> with correspondent <a href="http://arosien.github.com/scalaz-base-talk-201208" target="_blank">presentation slides</a> by <a href="https://twitter.com/arosien" target="_blank">Adam Rosien</a>.</p> <p>Here are some other resources for beginners:</p> <ul><li><a href="http://dnene.bitbucket.org/docs/scalaz-guide/initial.html" target="_blank">The Scalaz Guide</a></li> <li><a href="https://github.com/jrwest/learn_you_a_scalaz" target="_blank">Learn you a scalaz - project on GitHub</a></li> <li><a href="http://vimeo.com/15264203" target="_blank">Jason Zaugg&rsquo;s Intro to Scalaz</a> (by <a href="https://twitter.com/#!/retronym" target="_blank">Jason Zaugg</a>)</li> <li><a href="http://eed3si9n.com/learning-scalaz/" target="_blank">learning Scalaz</a> (by <a href="https://twitter.com/eed3si9n" target="_blank">Eugene Yokota</a>)</li> <li><a href="http://debasishg.blogspot.com/search/label/scalaz" target="_blank">Debasish Ghosh - posts about scalaz</a> - as far as I saw, he describes more advanced topics</li> <li><a href="http://www.stackmob.com/2011/12/scalaz-post-part-2/" target="_blank">Functional IO in Scala with Scalaz</a> (from <a href="https://twitter.com/#!/stackmob" target="_blank">StackMob</a>)</li> </ul><h3>Lenses</h3> <p>Lenses are generally something similar to getters and setters in function world. Here are some useful resources about them:</p> <ul><li><a href="http://www.youtube.com/playlist?p=PLEDE5BE0C69AF6CCE" target="_blank">Lenses: A Functional Imperative</a> (by <a href="https://twitter.com/#!/kmett" target="_blank">Edward Kmett</a>)</li> <li><a href="http://www.stackmob.com/2012/02/an-introduction-to-lenses-in-scalaz/" target="_blank">An Introduction to Lenses in Scalaz</a> (from <a href="https://twitter.com/#!/stackmob" target="_blank">StackMob</a>)</li> </ul><h3>Category Theory</h3> <p>In the hart of Scalaz (as well as Haskell) lies category theory. It&rsquo;s interesting topic, but can be somehow involving. <a href="https://twitter.com/#!/hseeberger" target="_blank">Heiko Seeberger</a> wrote a great articles on this topic, which I find very beginner-friendly:</p> <ul><li><a href="http://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/" target="_blank">Introduction to Category Theory in Scala</a></li> <li><a href="http://hseeberger.wordpress.com/2011/01/31/applicatives-are-generalized-functors/" target="_blank">Applicatives are generalized functors</a></li> </ul><p>If you still wonder what monad is, then following video will definitively give you some hints. I also find it very friendly to Java developers, that want to find out what actually monads are and their advantages. Video includes explanation of Option, Validation and List monads:</p> <ul><li><a href="http://marakana.com/s/scala_monads_declutter_your_code_with_monadic_design,1034/index.html" target="_blank">Scala Monads: Declutter Your Code With Monadic Design</a> (by Rob Nikzad)</li> </ul><p>A lot of ideas, implemented in Scalaz, are borrowed from Haskell, so it can be helpful to learn some Haskell too. You can find a lot of information on this topic in this chapter of <a href="http://learnyouahaskell.com/" target="_blank">Learn You a Haskell for Great Good!</a> book:</p> <ul><li><a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids" target="_blank">Functors, Applicative Functors and Monoids</a></li> </ul><h3>Conclusion</h3> <p>I found, that absorption of completely new concepts, is pretty hard and slow process where getting motivated is very important. I hope, that this material will motivate you and would be helpful in you thirst for knowledge.</p> <p>If you know some other resources about Scalaz, then please share them with me - I would be very grateful and they will help me to enhance this post with even more material.</p>http://hacking-scala.org/post/49050104489http://hacking-scala.org/post/49050104489Sun, 28 Apr 2013 02:45:00 +0200haskellscalazdocumentationEvery Project Needs a Home<p><figure class="tmblr-full" data-orig-height="629" data-orig-width="972" data-orig-src="https://raw.github.com/OlegIlyenko/my-blog-images/master/hacking-scala/scaldi-pamflet-b.png"><img src="https://66.media.tumblr.com/642435df21bf259c267087c361c49e26/tumblr_inline_p7jf3dYl471rl4dx3_540.png" alt="image" width="680" style="border: none" data-orig-height="629" data-orig-width="972" data-orig-src="https://raw.github.com/OlegIlyenko/my-blog-images/master/hacking-scala/scaldi-pamflet-b.png"/></figure></p> <p>Recently I created home page for my project <a href="http://olegilyenko.github.com/scaldi/Scaldi.html" target="_blank">Scaldi</a>. I wanted to make it for a long time, but from the other hand I don&rsquo;t want to spend much time finding some hosting and maintaining its infrastructure, making page design, etc. <a href="https://github.com/OlegIlyenko/scaldi" target="_blank">Github page</a> is nice, but still I would like to have somethng more simple and unique as a project&rsquo;s front page. So my main goals were:</p> <ul><li>Use very nice feature of Github - <a href="http://pages.github.com/" target="_blank">gh-pages</a> to host my pages</li> <li>Use <a href="http://daringfireball.net/projects/markdown/" target="_blank">markdown</a> to create my pages without spending much time making page design</li> <li>Design that comes out-of-box should be clean and pretty</li> <li>Integrate pages generation and publishing in SBT build process</li> </ul><!-- more --><p><strong>Pamflet</strong></p> <p>And I finally found solution that addresses all these requirements. <a href="http://pamflet.databinder.net/Pamflet.html" target="_blank">Pamflet</a> is very nice Scala library that allows you to define your site using <em>markdown</em>. It&rsquo;s very easy to use: you just create your pages that are named according to their position in the resulting table of contents with <strong>.md</strong> or <strong>.markdown</strong> extension. You can also define some properties in the <strong>template.properties</strong> which you can use in your pages (they would be processed using <a href="http://www.stringtemplate.org/" target="_blank">StringTemplate</a>). If you want, you can add some custom CSS in order to make it even more prettier. So resulting file structure should look like this:</p> <ul><li>00.md</li> <li>01.md</li> <li>02.md</li> <li>custom.css</li> <li>template.properties</li> </ul><p><a href="http://pamflet.databinder.net/Pamflet.html" target="_blank">Pamflet</a> has even more features, but it was enough for me to start creating <a href="http://olegilyenko.github.com/scaldi/Scaldi.html" target="_blank">Scaldi</a>&rsquo;s home page. So I ended up <a href="https://github.com/OlegIlyenko/scaldi/tree/master/docs" target="_blank">with following files in my project&rsquo;s <strong>docs</strong> folder</a>. It was pretty straightforward to create, but now I need to generate actual HTML from my <em>markdown </em>markup.</p> <p><strong>SBT Integration</strong></p> <p>In order to generate my pages and publish them to the <strong>gh-pages </strong>I need following SBT plugins:</p> <ul><li><a href="https://github.com/n8han/pamflet-plugin" target="_blank">pamflet-plugin</a> - it generates actual site</li> <li><a href="https://github.com/jsuereth/xsbt-ghpages-plugin" target="_blank">xsbt-ghpages-plugin</a> - adds generated pages to the <em>gh-pages</em> branch and pushes changes to github</li> </ul><p>At first you need to create  <em>gh-pages</em> branch in your git repository. You can make it like this:</p> <p> <script src="https://gist.github.com/OlegIlyenko/1462353.js"></script></p> <p>After executing this, you need to add <em>SBT</em> plugins in your <strong>project/project/Build.scala</strong>:</p> <p> <script src="https://gist.github.com/OlegIlyenko/1462125.js"></script></p> <p>After adding these plugins, you should be able to generate HTML pages. All you need to do is to execute <strong>write-pamflet </strong>task and your pages would be generated in <strong>target/docs </strong>folder.</p> <p>Now comes your main <strong>build.sbt </strong>file:</p> <p> <script src="https://gist.github.com/OlegIlyenko/1462166.js"></script></p> <p>I hope comments will help you in understanding what each of these settings does. So now in order to publish my pages I need to execute <strong>ghpages-push-site </strong>task. I can also run <strong>ghpages-synch-local </strong>in order to add pages to the <em>gh-pages</em>branch locally.</p> <p>As bonus, Scalasodocs would be also published by <a href="https://github.com/jsuereth/xsbt-ghpages-plugin" target="_blank">xsbt-ghpages-plugin</a>.</p> <h1><strong>Conclusion</strong></h1> <p>It was pretty easy to integrate <a href="http://pamflet.databinder.net/Pamflet.html" target="_blank"><strong>Pamflet</strong></a> with <strong><a href="https://github.com/harrah/xsbt" target="_blank">SBT</a></strong> and <a href="http://pages.github.com/" target="_blank"><strong>gh-pages</strong></a>. I ended up with <a href="http://olegilyenko.github.com/scaldi/Scaldi.html" target="_blank">nice looking site</a> and published Scaladoc. And it&rsquo;s really easy to manage and publish them.</p> <p>I hope this post will also help <em>you </em>to setup your project&rsquo;s home page in a matter of minutes, because, you know, <em>every project needs a home</em> :)</p>http://hacking-scala.org/post/49048514235http://hacking-scala.org/post/49048514235Sun, 28 Apr 2013 02:21:00 +0200documentationgh-pagespamfletsbtscaldiConceptual Surface Area of the Project<p>In this post I want to address (and express my opinion) some of the concerns described in <strong>fhil</strong>&rsquo;s comment to my previous post about <a href="/post/49047488794/scala-complexity-vs-book-of-magic-spells" target="_blank">Scala Complexity vs Book of Magic Spells</a>.</p> <h3>Conceptual Surface Area</h3> <p>First, I want to agree on concerns about language complexity, but I want to look at bigger picture. Scala the language has more concepts in it and from the language perspective it&rsquo;s more complicated than Java. From my experience, every project bigger than <em>HelloWorld</em> involves heavy usage of libraries (including standard library). In <a href="/post/49047488794/scala-complexity-vs-book-of-magic-spells" target="_blank">my previous post</a> I tried to take more higher-level view of the language, so I would like to keep this aspect in scope of our discussion. The problem with Java is that many libraries like Spring, Hibernate, EJB, AspectJ, etc. add completely new concepts which are pretty unique to each of them. And believe me, If you are creating real-world Java project (even small) you are expected to learn and understand a lot of new concepts introduced by these libraries (just try to search job sites for Java position and I assure you, most of them, of not all, require you to know a lot of libraries like Spring or Hibernate). I see Scala as product of evolution of other languages that absorbs new concepts in a language level that has proven to be useful or even essential. So Scala libraries can actually express themselves in terms of these concepts. Your real-world project becomes more simple, regular and consequent in terms of conceptual surface area. Just take a look at <a href="http://unfiltered.databinder.net" target="_blank">Unfiltered</a> web framework, and how elegantly it leverages pattern matching, if you want to see an example. As application developer I care much more about project&rsquo;s conceptual surface area rather than language&rsquo;s conceptual surface area because language is common knowledge and if you invested some time and learned the Scala language you can jump in any Scala project and be very well prepared for the concepts you will face. It&rsquo;s often not the case with Java because knowledge of reflection API and Annotations or even Java the language itself would be of little help in understanding new concepts that project or libraries introduce.</p> <!-- more --> <p>This reminds me of difference between Ant and Maven/Gradle/SBT/Leiningen. In Ant you have very small conceptual surface area. The result of this is that in order to understand what ant script actually does you need to look in it and understand all new concepts that it introduces (on top of ant). Maven, from the other hand, introduces much wider conceptual surface area, and yes, you need to learn it if you want to work with Maven. But as advantage, I can take namely any Maven project and run <code>mvn install</code> or <code>mvn package</code> on it without even looking in pom.xml. I even can run <code>mvn jetty:run</code> because most of the plugins reuse concepts introduced by Maven and stick to them. Concepts like project, dependency, project&rsquo;s version, project&rsquo;s name, etc. are proven to be very useful and even essential to most (if not all) Java projects. So I also see Maven and similar build systems as product of evolution where common and useful practices, patterns and concepts are introduced directly into the core system itself.</p> <h3>Maintenance Concerns</h3> <p>Now I would like to address concerns about maintenance - I actually disagree with points in the comment. I believe that in long run, Scala project can remain very maintainable. The reason for this is static typing and the fact that it&rsquo;s very well accepted and practiced in Scala community. It&rsquo;s much easier to maintain an application if you don&rsquo;t even need to run and debug it in order to understand it&rsquo;s behaviour and interaction/connection between it&rsquo;s components. I don&rsquo;t want to say that Scala completely eliminates necessity in debugging or running you application in order to understand it&rsquo;s behaviour, but it can make it unnecessary in many cases. Let&rsquo;s take perspective of new developer that jumps into the existing Scala project trying to understand it. What tools does he have in his toolbox? First and most important, from my point of view, is IDE - I can navigate code easily with the help of it. I use Intellij IDEA and I can use CTRL + Click almost everywhere. I can run search for usages and make complex refactorings without relaying on the wild guesses from the IDE based on the full text search and very very smart results analysis (which is the case with many dynamic languages and even with Java when you externalize part of your application&rsquo;s logic in XML, text or any other non-java files). Scala aids your development with things like implicits but sill keeps this static link between application components that makes it possible to analyze code without resorting to full text search or debugging. I believe that static typing and generally static nature of the language plays very important role in ability to maintain and grow the project. I already faced problem several times when developers just fail or refuse to maintain (even good structured) JavaScript project because of dynamic nature of language and very poor tools support (and I doubt that these tools can improve any further). Nowadays Java project contain so many dynamic elements, that I&rsquo;m really concerned about their maintainability.</p> <p>Also I don&rsquo;t expect non-scala developer to maintain Scala project in the same sense, that I&rsquo;m not expect Java developer, who knows nothing bout Spring or Hibernate, to main Java project that uses these libraries. Still in case of Scala I at least can be sure that IDE and compiler can really aid him and guard him to some extent from failure (to much greater extent than Java compiler does).</p> <h3>Subjective Stuff</h3> <p>And lastly, I would like to avoid discussion of highly subjective topics like language syntax. I believe that it&rsquo;s comparable to personal taste and is the matter of habituation. And we just can&rsquo;t expect all general purpose languages in this world to have the same syntax, especially the one you personally prefer.</p>http://hacking-scala.org/post/49048019436http://hacking-scala.org/post/49048019436Sun, 28 Apr 2013 02:13:00 +0200complexityantbuild toolsconceptual surface areamaintenancemavenScala Complexity vs Book of Magic Spells<p>Today was the first day of the wonderful <a href="http://www.devoxx.com" target="_blank">Devoxx</a> conference. I got a lot of positive impressions, but what I want to share with you today is something different. Today I was reminded about recent buzz about Scala complexity. I hear a lot about this recently, but I still fail to understand some aspects of it.</p> <p>In order to describe my point of view I would like to talk about Java complexity. Let me ask you a question: How complex Spring or Hibernate frameworks are? Some of you may say, that they are pretty simple (especially in comparison to EJB 2). But why is it so? Do you really believe that they are that simple&hellip; or may be they are just familiar? I think we can view these frameworks from 2 perspectives: from user perspective and from library developer perspective. I don&rsquo;t think that you expect all developers in your team to actually be able understand, how Spring/Hibernate is internally implemented. You want them to be able to use these frameworks and understand their purpose, ideology. What does it actually take to understand their internals? Which knowledge about Java can help them to understand these frameworks? Actually users should edit XML files or add annotations in their code and with the help of the Spring/Hibernate <strong>magic</strong> interesting things will happen. Spring will magically instantiate object and wire them. Hibernate will be able to load/save objects from/to the database. Do you really expect new Java developer, which you want to hire, to understand <strong>how</strong> exactly this magic works?</p> <!-- more --> <p>I want to talk more about this &ldquo;magic&rdquo;. Java reflection lies behind most of the modern popular Java frameworks. What does it actually means? We are just compromising the safety, performance, common java semantics of our code for convenience. Today I watched interesting talk from Spring guys called &ldquo;Spring Data JPA - Repositories done right&rdquo;. To make long story short, one part of these new API will allow you to write interface like this:</p> <pre><code>interface UserRepositry { User findByFirstNameAndLastName(Sting firstName, String lastName) } </code></pre> <p>Library will &ldquo;parse&rdquo; method signature and generate JQL query and all plumbing code for you&hellip; When I see code like this, Spring or other similar library looks more like book of magic spells to me rather than library or API. And I already can predict the first question of junior Java developer (after hours of code search): Where can I find <strong>UserRepositryImpl</strong>? (I still have a hard time explaining young java developers that these XML tags out there are actually creating new instances of Java classes and injecting them into other objects)</p> <p>And what can i answer? No, really, is there any standard way to find answer for this puzzle, except looking into the &ldquo;Spring Data Reference Manual&rdquo;? I&rsquo;m pretty sure that this library will have good documentation. Spring always has excellent reference manuals. But what about Javadoc? As was noted today, the first place new user will search for the help is in the examples and Javadoc. But we can&rsquo;t have it anymore. Actually we don&rsquo;t have any semantics of the library in the code, we just replaced public API with reflection and externalized semantics in the reference manual. Sometimes I have situation in which I can&rsquo;t find solution to my problem in the documentation. In such moments I just need to look in the source code of the library&hellip; but where should I start my journey? Definitely not from <strong>UserRepositry</strong> interface&hellip;</p> <p>Seems that each library adds it&rsquo;s own kind of magic in the project. Then problem Is that the only common concept they have is usage of reflection. For example, can I apply my knowledge about Spring in order to better understand how Hibernate works? Seems that Scala&rsquo;s <strong>map</strong> method from the collections API is pretty popular example that frightening people. But the signature of the <strong>map</strong> method is simply uses concept called <strong>Type classes</strong> . This concept adds great flexibility of the code, but also requires you to understand it, if you want to understand why map method will return you new <strong>Set[String]</strong> when you are executing it on <strong>BitSet</strong> and converting each bit to String. Actually this concept of <strong>Type classes</strong> is very common and a lot of Scala libraries are using it. Even Scala itself is using it a lot. Naturally you can invest your time in order to understand how Scala collections work (if you want, but you don&rsquo;t have to, you can just use map method according to the <a href="http://www.scala-lang.org/api/current/scala/collection/immutable/List.html" target="_blank">use case in the scaladoc</a> for this method and treat it&rsquo;s method signature as a magic). But when you actually invested this time and understood it, you have learned a bunch of concepts along the way that will make your understanding of other completely different library much much easier.</p> <p>In Java world you can invest time in understanding Spring school of magic, but it won&rsquo;t be of any help with Hibernate&rsquo;s one. From the other hand in Scala you have set of concepts that are shared between all the libraries. I also believe that Scala brings us more safety and can free us from the reflection craziness that has got a lot of popularity in Java wold recently. Also notice, that magic has not proven itself to be very reliable. It can always fail you at Friday evening a the production environment. I don&rsquo;t think that in most cases Scala actually adds more complexity, it just offers you to explore new world were magic is replaced with technology which solves the same problems but in more consistent and regular way.</p> <p>So my point is: You don&rsquo;t need to worry about Scala&rsquo;s complexity. Actually I believe that this topic is relevant only to library developers who want to make it very robust and very general. With Scala they actually have much better opportunity to make <strong>your</strong> code more safe and faster. This philosophy goes even in language design: Have you ever tried to use Java generics with all these wildcards and definitions of co/in/contravariant types? Several weeks ago I tried to use it and ended up with something like:</p> <pre><code>Map&amp;lt;String, ? extends Type&amp;lt;Integer, ? extends Map&amp;lt;String, ? extends List&amp;lt;String&amp;gt;&amp;gt;&amp;gt;&amp;gt; result = // ... </code></pre> <p>Java forces you to actually know how exactly variance works. I believe that it&rsquo;s too complicated to get it right. From the other hand in Scala, variance information is described in the type definition itself, so, as user of the library, you don&rsquo;t have to understand it. According to Scaladoc type parameter of immutable list is covariant, for the mutable list it&rsquo;s invariant and in function it&rsquo;s arguments should be contravariant and result type covariant. Can you explain why? Last time I thought about it, it took me about 1 hour of mental work before I got more or less clear understanding. And after about 5 minutes I have lost the focus and landed at the same point where I started. The problem is that Java forces you to understand these concepts if you are going to use them. In Scala, library designer can save your time and do this for you.</p> <p>Hope you enjoyed this post. As always, any feedback is highly appreciated!</p> <p>PS: By the way, I have nothing against the magic. If it would really exist in our world I&rsquo;d probably become Archimage rather than developer :)</p>http://hacking-scala.org/post/49047488794http://hacking-scala.org/post/49047488794Sun, 28 Apr 2013 02:05:00 +0200complexitydevoxxmagicspringComposing Your Types on Fly<p>Let me show you class from one of my projects:</p> <pre><code>class FileTransferHandler extends TransferHandler with Publisher { // ... } </code></pre> <p><code>TransferHandler</code> is swing class that generally handles copy/paste and D&amp;D stuff. <code>Publisher</code> is trait that comes from scala-swing. So I generally composing here 2 independent things and making then work together.</p> <p>As next step I define method, that requires it&rsquo;s argument to be both - <code>TransferHandler</code> and <code>Publisher</code>. In my codebase I have only one type that extends both of them, and it&rsquo;s <code>FileTransferHandler</code>. So I can use it to define my method, right?</p> <pre><code>def register(handler: FileTransferHandler) { // ... } </code></pre> <p>The problem here is that the body of the <code>register</code> method does not care whether <code>handler</code> is <code>FileTransferHandler</code> or <code>ImageTransferHandler</code> or even <code>StringTransferHandler</code>. It only requires it to be <code>TransferHandler</code> and <code>Publisher</code>. And it&rsquo;s exactly what Scala allows you to express using <code>with</code> keyword! So now we can write improved version of <code>register</code> method:</p> <pre><code>def register(handler: TransferHandler with Publisher) { // ... } </code></pre> <p>So you don&rsquo;t actually need any concrete class or trait to express such constraint.</p> <!-- more --> <h3>Combining with structural types</h3> <p>Scala has very nice feature called <strong>structural types</strong>. Let me describe it to you in example. Recently I was writing some Scala tests for Java classes. It appears, that a lot of classes had methods like <code>init()</code> and <code>destroy()</code>. Not all classes had both of them - some need only to be initialized, but don&rsquo;t require explicit destruction and vice versa. Also there are no interfaces for this - they all were normally instantiated using <em>spring</em> with something like</p> <pre><code>&amp;lt;bean class="..." init-method="init" destroy-method="destroy" /&amp;gt; </code></pre> <p>Structural typing helped me a lot to deal with such classes. I was able to define methods like this one:</p> <pre><code>def cleanup(list: List[{def destroy()}]) = list foreach (_.destroy()) </code></pre> <p>Next I would like to define method, that will initialize and destroy objects automatically for me, because I can just forget about one of these steps. So here my first attempt:</p> <pre><code>def safe[T &amp;lt;: {def init(); def destroy()}, R](t: T)(body: T =&amp;gt; R) = try { t.init() body(t) } finally { t.destroy() } safe(new PaymentService) { service =&amp;gt; service.makePayment() } </code></pre> <p>So it works! But can we implement it better? The problem here - is that we repeating ourselves. I already used signature of <code>destroy</code> method in the <code>cleanup</code> method, and I don&rsquo;t want to repeat myself again. It would be nice to define some traits/interfaces like <code>Init</code> or <code>Destroy</code>, but I don&rsquo;t want to change all existing classes in order to implement them. Actually I even don&rsquo;t need any concrete trait for this - I just need to define some kind of alias for <code>{def init()}</code> and <code>{def destroy()}</code>.</p> <p><strong>Type aliases</strong> serve exactly this purpose. Let&rsquo;s define them:</p> <pre><code>type Init = {def init()} type Destroy = {def destroy()} </code></pre> <p>Nice, now I have pretty names for the structural types. <code>safe</code> method needs both of them and, as you probably already guessed, I can combine them using <code>with</code>:</p> <pre><code>def safe[T &amp;lt;: Init with Destroy, R](t: T)(body: T =&amp;gt; R) = // ... </code></pre> <p>If you wish, you can also define type alias for the combination:</p> <pre><code>type InitDestroy = Init with Destroy def safe[T &amp;lt;: InitDestroy, R](t: T)(body: T =&amp;gt; R) = // ... </code></pre> <p>Hope you enjoyed.</p>http://hacking-scala.org/post/49047265161http://hacking-scala.org/post/49047265161Sun, 28 Apr 2013 02:01:00 +0200structural typestype aliastypesInverting the Flow of Your Code<p>In <a href="/post/49044250853/side-effecting-without-braces" target="_blank">previous post</a> I described, how you can make side-effects in very concise way. There is one complementary operation that I also use a lot. This time it helps you to visualize the flow of the code. Let me demonstrate you this code:</p> <pre><code>case class Item(id: Int, name: String) def getItem(id: Int): Item = // ... def prepareForView(item: Item): Map[String, String] = // ... def renderPage(model: Map[String, String]): String = // ... def getItemPage(itemId: Int) = renderPage(prepareForView(getItem(itemId))) </code></pre> <p>You have probably already saw this kind of code a lot. Such method chaining, that I showed in body of the <code>getItemPage</code> method, is pretty common. I personally find it awkward - the code flow is turned inside out. I call the method <code>renderPage</code> at first, even if this is something that would be used only at the end and produces the result value of the whole function. My brain just work in other direction: When I&rsquo;m writing this code, at first I think, how I can get <code>Item</code> from <code>id</code> and then, how can I get the model for the view &hellip; and finally how to produce page. We can do better!</p> <!-- more --> <p>What we can do is to define another small implicit conversion:</p> <pre><code>object WorkflowHelper { implicit def anyWithWorkflowHelpers[T](target: T) = new { def |&amp;gt;[R](fn: T =&amp;gt; R) = fn(target) } } </code></pre> <p>It adds <code>|&amp;gt;</code> method to any object and allows us to provide some function that transforms it&rsquo;s argument to something else (definition is even easier than <code>~</code> method defined in <a href="/post/49044250853/side-effecting-without-braces" target="_blank">previous post</a>).</p> <p>Now let&rsquo;s try to use it:</p> <pre><code>import WorkflowHelper._ def getItemPage(itemId: Int) = getItem(itemId) |&amp;gt; prepareForView |&amp;gt; renderPage </code></pre> <p>Now it feels much more natural, isn&rsquo;t it?</p> <p>Ok, in this example for every bit of it&rsquo;s logic I had some other function. It&rsquo;s also common, because we often split our logic in several smaller pieces and than have some main method that calls them all. But sometimes methods do not have suitable signature. For example <code>prepareForView</code> most probably will also require locale:</p> <pre><code>def prepareForView(item: Item, locale: Locale): Map[String, String] = null </code></pre> <p>But in order to handle this we can use vary nice Scala feature called <strong>currying</strong>. It allows use to adapt <code>prepareForView</code> method to our needs:</p> <pre><code>def getItemPage(itemId: Int) = getItem(itemId) |&amp;gt; (prepareForView(_, Locale.ENGLISH)) |&amp;gt; renderPage </code></pre> <p>In this case <em>underscore</em> plays the role of placeholder. You generally saying, that provided <code>Item</code> instance should be inserted here.</p> <p>But wait&hellip; often we have more than one object to deal with! How can we handle this? Let&rsquo;s say, we have also <code>User</code> in play which also should be prepared for view. This time tuple comes to rescue. Here is how we can implement this:</p> <pre><code>def prepareForView(item: Item): Map[String, String] = // ... def prepareForView(item: User): Map[String, String] = // ... def renderPage(model: Map[String, AnyRef]): String = // ... def getCurrentUser: User = // ... def getItemPage(itemId: Int) = (getItem(itemId) |&amp;gt; prepareForView, getCurrentUser |&amp;gt; prepareForView) |&amp;gt; {case (item, user) =&amp;gt; renderPage(Map("item" -&amp;gt; item, "user" -&amp;gt; user))} </code></pre> <p>In this case I prepare tuple and give it to the another function. Actually it&rsquo;s partial function, that&rsquo;s because I&rsquo;m also able to make pattern matching directly there! Of course, you can make it a little bit differently and prepare complete model for the page first and then just render the page:</p> <pre><code>def getItemPage(itemId: Int) = Map( "item" -&amp;gt; (getItem(itemId) |&amp;gt; prepareForView), "user" -&amp;gt; (getCurrentUser |&amp;gt; prepareForView) ) |&amp;gt; renderPage </code></pre> <p>I&rsquo;m not claiming, that <code>|&amp;gt;</code> method is useful in any situations, but I definitely believe, that there are a lot of situations where it can make code more readable and easier to write.</p> <p>By the way, I&rsquo;m not the first one who came up with this idea. For example <a href="http://code.google.com/p/scalaz/" target="_blank">scalaz</a>&rsquo;s <a href="http://scalaz.github.com/scalaz/scalaz-2.9.1-6.0.2/doc.sxr/scalaz/Identity.scala.html#48273" target="_blank">Identity</a> trait also has <code>|&amp;gt;</code> method which has exactly the same signature and purpose.</p>http://hacking-scala.org/post/49046871971http://hacking-scala.org/post/49046871971Sun, 28 Apr 2013 01:55:46 +0200implicitscurryingSide-effecting without braces<p>Sometimes I find myself in following situation: I have nice one-expression method like this:</p> <pre><code>def formatUsers(users: List[User]): String = users.map(_.userName).mkString(", ") </code></pre> <p>Now I want to print the results to the <code>System.out</code> in order to quickly check the result that this function produces.</p> <p>Common way of solving this problem is to write something like this:</p> <pre><code>def formatUsers(users: List[User]): String = { val result = users map (_.userName) mkString ", " println(result) result } </code></pre> <p>Too much code for such trivial task. In Scala we can do better than this!</p> <!-- more --> <p>Here is the way you can improve it. At first we need to define small implicit conversion:</p> <pre><code> object SideEffects { implicit def anyWithSideEffects[T](any: T) = new { def ~(fn: T =&gt; Unit) = { fn(any) any } } } </code></pre> <p>With this code I can make <code>~</code> method available on any object. The only thing it does - is executes function you provided and returns original object. So the result of this function is completely ignored (that&rsquo;s because it&rsquo;s return type is <code>Unit</code>), but it&rsquo;s Ok for our purpose - it&rsquo;s exactly what we want to archive here.</p> <p>Ok, now we can use it. Here is my first attempt:</p> <pre><code>import SideEffects._ def formatUsers(users: List[User]): String = users.map(_.userName).mkString(", ") ~ (s =&amp;gt; println(s)) </code></pre> <p>And, as you can guess - it works. But we can do even better! <code>println</code> is also functions that accepts one argument, so we don&rsquo;t need to wrap it in another function:</p> <pre><code>def formatUsers(users: List[User]): String = users.map(_.userName).mkString(", ") ~ println </code></pre> <p>Now it looks much better with much less code, isn&rsquo;t it?</p> <p>You also can use it everywhere in expressions. Here is another example:</p> <pre><code>def formatUsers(users: List[User]): String = users.map(_.userName ~ println).mkString(", ") </code></pre> <p>It just prints all processed user names for me.</p> <p>Generally side-effects is not very good thing, but sometimes than can be helpful. Debug output, that I demonstrated, is one example. Another good example is integration with Java, which consists mostly of side-effects. Let&rsquo;s look in this code:</p> <pre><code>val frame = new JFrame frame.setVisible(true) useFrame(frame) </code></pre> <p>You probably already guess how we can improve this situation. Of course with our brand new, shiny <code>~</code>!</p> <pre><code>useFrame(new JFrame ~ (_ setVisible true)) </code></pre> <p>Hope you will find it helpful and enjoyed this post.</p> <h3>Small update about theoretical grounds</h3> <p>Seems that this approach even has some theoretical grounds. So if you are interested, it&rsquo;s called <strong>K combinator</strong> and you can find some information about it (with code examples in Ruby) in <a href="http://www.markhneedham.com/blog/2010/05/03/coding-the-kestrel/" target="_blank">this post</a>.</p> <p>Actually that post reminds me on another use case for the <code>~</code> method, that I&rsquo;m using sometimes. It looks like this:</p> <pre><code> def createFrame = new JFrame ~ { frame =&gt; import frame._ setVisible(true) setTitle("Test Frame") setSize(200, 100) // ... } </code></pre> <p>I like this pattern because it makes intent of the code much cleaner and visually splits method in 2 parts: the first one defines object you want to return and the second configures it.</p>http://hacking-scala.org/post/49044250853http://hacking-scala.org/post/49044250853Sun, 28 Apr 2013 01:15:00 +0200implicitsdebugEssential Scala Collection Functions<p>Let me ask you a question. How many times you wrote similar code?</p> <pre><code>case class User(id : Int, userName: String) val users: List[User] = // .... val resultUsers: List[User] = // .... for (i &lt;- 0 until users.size) { if (users(i).userName != "test") { resultUsers += users(i) } } </code></pre> <p>May imperative languages like Java, C, C++ have very similar approach for iteration. Some of them have some syntactic sugar (like Java for each loop). There are many problems with this approach. I think the most important annoyance is that it&rsquo;s hard to tell, what this code actually does. Of course, if you have 10+ years Java experience, you can tell it within a second, but what if the body of the <code>for</code> is more involving and has some state manipulatin? Let me show you another example:</p> <pre><code>def formatUsers(users: List[User]): String = { val result = new StringBuilder for (i &lt;- 0 until users.size) { val userName = users(i).userName if (userName != "test") { result append userName if (i &lt; users.size - 1) { result append ", " } } } return result.toString } </code></pre> <p>Was it easy to spot the purpose of this code? I think more important question: how do you read and try to understand this code? In order to understand it you need to iterate in your head - you create simple trace table in order to find out how values of <code>i</code> and <code>result</code> are changing with each iteration. And now the most important question: is this code correct? If you think, that it is correct, then you are wrong! It has an error, and I leave it to you to find it. It&rsquo;s pretty large amount of code for such a simple task, isn&rsquo;t it? I even managed to make an error it.</p> <p>I&rsquo;m going to show you how this code can be improved, so that intent is more clear and a room for the errors is much smaller. I&rsquo;m not going to show you complete Scala collection API. Instead I will show you 6 most essential collection functions. These 6 function can bring you pretty far in your day-to-day job and can handle most of your iteration needs. I going to cover following functions: filter, map, flatMap, foldLeft, foreach and mkString.</p> <p>Also note, that these functions will work on any collection type like <code>List</code>, <code>Set</code> or even <code>Map</code> (in this case they will work on tuple <code>(key, value)</code>)</p> <!-- more --> <h3>filter</h3> <p><code>filter</code> is easy one. Let me show you how it can be used in order to simplify my first example:</p> <pre><code>val resultUsers = users filter (_.userName != "test") </code></pre> <p>As you can see, what left is most essential part of my algorithm. <code>filter</code> takes a predicate function as it&rsquo;s argument (function that returns <code>Boolean</code>: <code>User =&gt; Boolean</code> in my example) and uses it to filter the collection.</p> <h3>map</h3> <p><code>map</code> converts one collection into another with the help of your function. Here is an example:</p> <pre><code>val userNames = users map (_.userName) </code></pre> <p>In this code I just converting my list of users in list of user names. As you can guess <code>userNames</code> has type <code>List[String]</code>.</p> <p>What if I have two kinds of users: <code>AdminUser</code> and <code>NormalUser</code>. For each of them I want to use different logic - I don&rsquo;t want to leak user names of my admins! In place of function I can use something called partial function - it&rsquo;s special function that can have only one argument and can be defined only for subset of it&rsquo;s values. The best thing about it, is that it has special syntax, that allows to pattern match on it&rsquo;s argument. Here is an example that describes how it looks like:</p> <pre><code>case class AdminUser(userName: String) case class NormalUser(userName: String) val users = List(AdminUser("bob"), NormalUser("john")) val userNames = users map { case NormalUser(userName) =&gt; userName case _ =&gt; "secret" } assert(userNames == List("secret", "john")) </code></pre> <p>This feature can be also useful, if you are working with <code>Map</code> collection. During iteration <code>Map</code> gives you tuple with key and value in it. First attempt to use <code>map</code> function can look like this:</p> <pre><code>val users = Map(1 -&gt; User(1, "bob"), 2 -&gt; User(2, "john")) val userNames = users map (_._2.userName) </code></pre> <p><code>_2</code> is field on tuple that returns it&rsquo;s second value. As you can see it&rsquo;s a little bit cryptic. But you can improve it by using partial function:</p> <pre><code>val userNames = users map {case (id, user) =&gt; user.userName} </code></pre> <h3>flatMap</h3> <p><code>flatMap</code> is surprisingly very similar to <code>map</code>. It also converts one collection into another. The difference is that it will also flatten all elements of the resulting collection. So if your function returns some kind of container like <code>List</code> or <code>Set</code>, then <code>flatMap</code> will also put all it&rsquo;s elements in the result. Let me show you it in action:</p> <pre><code>val suffixes = List("ain", "o") val words = suffixes flatMap (suffix =&gt; List("P" + suffix, "G" + suffix)) assert(words == List("Pain", "Gain", "Po", "Go")) </code></pre> <p>As you can see, everything is merged in one resulting list. If you will use <code>map</code>, the result would be: <code>List(List(Pain, Gain), List(Po, Go))</code>. Here is another, more practical, example:</p> <pre><code>def downloadAllImages(url: String): List[File] = //.... val urls = List("http://www.google.com", "http://www.example.com") val images: List[File] = urls flatMap downloadAllImages </code></pre> <p>Now that you already know <code>map</code> and <code>filter</code> functions, you probably already have some ideas, how <code>formatUsers</code> can be simplified. We can try to partly rewrite it using <code>map</code> and <code>filter</code>:</p> <pre><code>val safeUserNames: List[String] = users map (_.userName) filter (_ != "test") </code></pre> <p>But what if I have <code>AdminUser</code> and <code>NormalUser</code> like in previous examples, but this time I want to show only normal users. In order to archive this in one <code>flatMap</code> we can use partial function mentioned before. I also need to tell <code>flatMap</code> somehow, that I don&rsquo;t want some of the items from the original collection - namely admin users. We can achieve this with very useful class - <code>Option</code>. It&rsquo;s also container which can hold either a single value (in this case it has type <code>Some[T]</code>) or nothing at all (in this case it has type <code>None</code>). As you can guess <code>flatMap</code> will just ignore all <code>None</code> values because they do not contain anything in them:</p> <pre><code>val users = List(AdminUser("bob"), NormalUser("test"), NormalUser("john")) val userNames = users flatMap { case NormalUser(userName) if userName != "test" =&gt; Some(userName) case _ =&gt; None } assert(userNames == List("john")) </code></pre> <h3>foldLeft</h3> <p><code>foldLeft</code> is just combining all values of the collection into one resulting value. For example you can sum all numbers in the collection with initial value 0:</p> <pre><code>val nums = List(1, 2, 3, 4, 5) val sum = nums.foldLeft(0)(_ + _) </code></pre> <p><code>foldLeft</code> has 2 argument lists - first is the initial value and the second is your function that tells <code>foldLeft</code> how you want to combine two items together. Following imperative code demonstrates how <code>foldLeft</code> actually works, so it will produce the same results:</p> <pre><code>var sum = 0 for (i &lt;- 0 until nums.size) { sum = sum + nums(i) } </code></pre> <p><code>foldLeft</code> can be very useful, especially if you want to pass some value through the whole iteration process. Let&rsquo;s continue our implementation of <code>formatUsers</code> and combine all user names in nice comma separated string:</p> <pre><code>userNames.tail.foldLeft(userNames.head)(_ + ", " + _) </code></pre> <p>As you can see used 2 new small functions: <code>head</code> that returns the first element of the collection and <code>tail</code> - it returns collection of the same type with all element except the first one. But this kind of operations can be simplified, so I encourage you to read further.</p> <h3>foreach</h3> <p><code>foreach</code> just iterates through the whole collection and execute your function on each of the elements. The return type of your function and <code>foreach</code> is <code>Unit</code>. This means, that it does not returns anything of interest and even if you return something - it would be just ignored. This function is useful for making side effects. For example I can print all elements of the collection like this:</p> <pre><code>users foreach println </code></pre> <p>It&rsquo;s behavior is very similar to Java&rsquo;s own for each loop. Here is equivalent in Java:</p> <pre><code>// Java code for (User user: users) { println(user) } </code></pre> <h3>mkString</h3> <p>And the last, but not least - <code>mkString</code>. It&rsquo;s small and useful function that just joins all elements of the collection in one string. You can also provide separator that would be used between elements. Let&rsquo;s continue with our new <code>formatUsers</code> implementation and format user names:</p> <pre><code>userNames mkString ", " </code></pre> <p><code>mkString</code> is very simple function and sometimes people wonder whether it can be useful for some more complicated tasks. Even in our simple case - we originally don&rsquo;t have list of user names, but we have the list of the users. <code>mkString</code> is not enough for this. But we can use already familiar functions to achieve our goal - print nice list of user names by having only list of users:</p> <pre><code>users map (_.userName) mkString ", " </code></pre> <p>This function composition is very nice way to work with the collections.</p> <h3>Improving <code>formatUsers</code></h3> <p>Now that you already know all of the essential tools that collection framework provides us, it should be straightforward to write a better implementation of the <code>formatUsers</code> method. Here is the whole implementation:</p> <pre><code>def formatUsers(users: List[User]): String = users map (_.userName) filter (_ != "test") mkString ", " </code></pre> <p>The first and most important thing to notice - it&rsquo;s correct. I had not enough room to make the same error. I also was able to extract the most essential parts of the algorithm and put it in front of you. Boilerplate and language ceremony is also reduced dramatically.</p> <p>Another important aspect of this implementation is how you reading and writing it. Now you don&rsquo;t think in terms of iterations and variable mutation. You are concentrating yourself on the set of collection transformations. Each transformation/step produces some correct and meaningful result. In my example I have three distinct steps: map, filter and make string. Each of them produces something that is valid and self contained.</p> <p>In imperative version I had only 2 meaningful states: at the beginning and at the end of the method. Everything in between is broken/incomplete state, that can&rsquo;t be used for anything else. When I&rsquo;m trying to understand it, I need to keep the whole algorithm in head in order to understand how it works and what results it can produce. In contrast, in my function version each step is self contained and can be analyzed separately.</p> <p>In my new implementation I can, without any code modifications, extract <code>users map (_.userName)</code>, <code>userNames filter (_ != "test")</code> or <code>safeUserNames mkString ", "</code> to some methods and they can potentially be reused by other code. So my ability to reuse this code is also increased greatly.</p> <h3>for comprehension</h3> <p><code>map</code>, <code>flatMap</code>, <code>filter</code> and <code>foreach</code> are so popular, that Scala even has special syntax for them and you already saw it. <code>for</code> is not a <code>for</code> loop that you know from languages like Java and C. It&rsquo;s much more powerful and allows following three things: iterate collections, filter them and assign variables. Here is an example:</p> <pre><code>val safeUserNames = for (user &lt;- users; userName = user.userName; if userName != "test") yield userName </code></pre> <p>The same can also be written without semicolons with following syntax:</p> <pre><code>val safeUserNames = for { user &lt;- users userName = user.userName if userName != "test" } yield userName </code></pre> <p>What&rsquo;s interesting about <code>for</code> is that under the hood it would be compiled to the series of <code>map</code>, <code>flatMap</code>, <code>filter</code> and <code>foreach</code> method calls. It can provide very nice syntax when you have some nested loops like this:</p> <pre><code>val allUserPrivileges = for { user &lt;- users role &lt;- user.roles privilege &lt;- user.privileges } yield privilege </code></pre> <h3>Performance considerations</h3> <p>Some of you may be concerned about performance, and it&rsquo;s valid argument. It&rsquo;s clear, that now in each step I&rsquo;m iterating through the whole collection. In my concrete example it&rsquo;s not a problem because I need this anyway even in imperative version. But imperative algorithms often use <code>break</code> and <code>continue</code> in order to stop iteration when needed value is found or required amount of the result elements is reached. If you will use methods like <code>map</code> or <code>filter</code>, then collection would be always completely processed.</p> <p>There are two nice ways to deal with this. The first one is <em>laziness</em>. If you will call <strong>view</strong> on a collection, then you will receive lazy version of it. Here is an example:</p> <pre><code>val safeUserNames = users.view map (_.userName) filter (_ != "test") </code></pre> <p>At this point neither <code>map</code> nor <code>filter</code> are evaluated. <code>safeUserNames</code> will evaluate only when it would be explicitly asked to make it. You can use <code>force</code> method to make it. In this case you will apply <code>filter</code> and <code>map</code> on every element in the collection. <code>mkString</code> and <code>foldLeft</code> work the same way. But you don&rsquo;t have to make it. For example you can <code>take</code> first 5 safe user names like this:</p> <pre><code>safeUserNames take 5 </code></pre> <p>If you don&rsquo;t know how many element you need, but you know when to stop, then use <code>takeWhile</code>:</p> <pre><code>safeUserNames takeWhile (un =&gt; un(0) == 'a')</code></pre> <p>assuming that list is sorted, this code will return only user names that start with &lsquo;a&rsquo;.</p> <p>The second way to improve performance is to use parallel collections. All you need to do is to call <strong>par</strong> method on the collection - it will return parallel version of it. So this <code>map</code> operation will process it&rsquo;s elements in parallel:</p> <pre><code>users.par map (_.userName) </code></pre> <h3>Conclusion</h3> <p>I encourage you to use more functional style when you are working with collections. It has a lot of advantages over imperative style. But I think the most important is that it makes you think in terms of collection transformations which makes it much easier to spot the intent of the code. This makes it easier for you to write code and for other people to read it.</p> <p>I hope you will find this post interesting and inspiring. If you have any questions, suggestions or something else to tell me - just drop a comment, I would be very happy :) You feedback is highly appreciated!</p>http://hacking-scala.org/post/49042308259http://hacking-scala.org/post/49042308259Sun, 28 Apr 2013 00:46:00 +0200collections