Elixir – another language on the Erlang Runtime

Having read learning 7 languages in 7 weeks a couple of years ago I became impressed with erlang. The idea of using the actor model to create stable scalable, self healing systems is very compelling. The problem was the odd syntax that you had to put up with.

This week I found the follow up book 7 more languages in 7 weeks

This includes elixir a ruby like language that lives on the erlang vm. This looks to be a useful addition to the platform especially as it includes some string handling that Erlang is weak at.

One thing to note when installing elixir – you will need to upgrade your erlang version to 17 or 18. It requires a more recent version than was used when the first book came out.  Without this the command line tool iex will crash with a cryptic erlang error.

Minimal Groovy neo4j client

@Grab(‘org.codehaus.groovy.modules.http-builder:http-builder:0.7.2’)
import groovyx.net.http.RESTClient
import groovy.json.JsonSlurper

def authToken = ‘neo4j:password’.bytes.encodeBase64().toString()
def auth = “Basic: $authToken”.toString()
// send Cypher statement to Neo4j Server
def client = new RESTClient( ‘http://localhost:7474’, ‘application/json’ )
client.defaultRequestHeaders.’Authorization’ = auth

def body = “””{
“statements” : [ {
“statement” : “CREATE (wordsworth:Author {firstname:’William’, lastname: ‘Wordsworth’})”
} ]
}”””
def resp = client.post( path : ‘/db/data/transaction/commit’, body : body )

println resp.data.toString()

Handlebars (Java) Templates and Extensions are really powerful

I have recently moved a project from using Thymeleaf to using Handlebars.

Handlebars is a templating library (think mail merge for code if you are a non-developer).

Handlebars uses the {{ }} tags to surround items to be replaced.

{{{   }}} will not escape content provided.

{{ > path/to/file }}  Renders a template. You can supply named blocks that get substituted into that template.

By default the template uses whatever data that the template was bound to. Typically we bind nested Maps.

One of the principles of handlebars is that the templates should be logic free. This means that you are limited to {{#if }}, {{#unless }} and {{#each }} blocks.

#If will check the truthiness of a value. The intent is to include a block of template only if a certain item is present, say an optional middle name.

#unless is the inverse, it will only show the block if the condition is false. This is ideal for providing defaults for missing data.

Any logic above this must be handled by an extension method.

This is not as difficult as it sounds. With a little effort extension methods can be written to:

  • Invoke a template name specified in a variable.
  • Perform custom sorting of data acting as a custom loop over a supplied block.

By keeping the logic isolated it becomes independently testable and simplifies the templates.

Thoughts on Groovy

I have been working in Groovy for about a month now.

It’s definitely a step up from Java in terms of dialing down the ceremony.

However the scripting nature can be nasty on occasions where the compiler just does not protect you from obvious mistakes.