Deepu Mohan Puthrote

Programming languages, frameworks and general technology thoughts

AWS SNS error - Invalid parameter

So I was playing with AWS Lambdas, and wanted to setup SNS notification for that.

1
aws sns publish --topic-arn arn:aws:sns:eu-west-1:22834709971:my-awesome-sns-topic --subject 'Subject' --message 'Hello, hello!'

However got stuck on the following error - it just says Invalid Parameter

1
An error occurred (InvalidParameter) when calling the Publish operation: Invalid parameter: TopicArn

Turns out this has to do with my region settings.

I’d created SNS in eu-west-1 region (Ireland). But default region on my account was us-west-1
So, clearly that is a conflict.

Solution is easy, I just had to set region in ~/.aws/config

1
2
3
[default]
output = text
region = eu-west-1

That’s it!

Game Of Life (in Scala)

I recently came across an interesting problem/game called Game Of Life. A visual version of this is available here. Also, here is a beginners introduction to Game Of Life, or simply Life, written by Alex Bellos at Guardian.

It is a cellular automation problem created by John Horton Conway, a British mathematician in 1970, not a game you play to win or lose. This problem has a few rules that we need to follow.

Rules

  1. A cell in the grid can be dead or alive.
  2. A live cell with less than 2 live neighbours will die.
  3. A live cell with 2 or 3 live neighbours will stay alive.
  4. A live cell with more than 3 live neighbours will die.
  5. A dead cell with exactly 3 neighbours will become alive.

These rules are pretty simple. Once we know the number of live neighbours of a given cell, we can determine it’s fate in a step. In this post I’m going to dicuss method to find neighbours of a given cell in a matrix.

In Game of Life, we take a seed grid and eventually get another grid as a output, by applying the rules to each cell.

Game Of Life grid

Game of Life is played in an infinite two dimensional grid. But for the sake of simplicity let us take a 4x4 grid.

0 1 2 3
0 Cell(1) Cell(1) Cell(1) Cell(1)
1 Cell(1) Cell(0) Cell(1) Cell(0)
2 Cell(0) Cell(0) Cell(0) Cell(0)
3 Cell(1) Cell(0) Cell(1) Cell(0)

1 is alive and 0 is dead

A cell can have a maximum of 8 neighbours depending on it’s position in the grid, horizontally, vertically, or diagonally adjacent to it.

After applying rules first step, we will get the following

0 1 2 3
0 Cell(1) Cell(0) Cell(1) Cell(1)
1 Cell(1) Cell(0) Cell(1) Cell(1)
2 Cell(1) Cell(0) Cell(1) Cell(1)
3 Cell(0) Cell(1) Cell(0) Cell(0)

Representation in Scala

Let’s first represent the above seed grid in Scala.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
case class Cell(value: Int)
case class Point(row: Int, col: Int) {
def unapply() = (this.row, this.col)
}
val alive = Cell(0)
val dead = Cell(1)
val row1 = Array(alive, alive, alive, alive)
val row2 = Array(alive, dead, alive, dead)
val row3 = Array(dead, dead, dead, dead)
val row4 = Array(alive, dead, alive, dead)
val board = Array(row1, row2, row3, row4)

Now we have a basic Representation of our models required in Game of Life.

Find neighbours of a given Cell in two dimensional array

Given the co-ordinates of a cell, we should be able to find list of it’s live neighbours.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def findNeighbours(board: Array[Array[Cell]], point: Point):List[Cell] = ???
def findLiveNeighbours(board: Array[Array[Cell]], point:Point):List[Cell] =
findNeighbours(board, point).filter(c => c.value == 1)
// one step
def step(board: Array[Array[Cell]]): Array[Array[Cell]] = {
val rows = board.length
val cols = board(0).length
// create a new Matrix of same dimensions
val output = Array.ofDim[Cell](rows, cols)
// loop through the array and set value for output cell
// based on number of live neighbours
for(i <- 0 until rows) {
for(j <- 0 until cols) {
val point = Point(i,j)
val liveNeighbours = findLiveNeighbours(board, point).length
// apply rules based on number of live neighbours
output(i)(j) = deadOrAlive(liveNeighbours)
}
}
output
}
// apply rules based on number of live neighbours
def deadOrAlive(liveNeighbours: Int): Cell = {
if (liveNeighbours < 2) dead
else if (liveNeighbours == 2 || liveNeighbours == 3) alive
else if(liveNeighbours > 3) dead
else if(liveNeighbours == 3) alive
else dead
}

Find neighbours

Now this is the important bit.

A neighbour is any cell that is adjacent to a given cell, horizontally, vertically or diagonally. (1,1) is adjacent to (0,0), (0,1), (0,2), (1,0), (1,2), (2,0), (2,1), (2,2).

We will implement findNeighbours functions.

1
def findNeighbours(board: Array[Array[Cell]], point(x:Int, y:Int)):List[Cell] = ???

Algorithm

Here is an algorithm, I found on StackOverflow, by Seb

http://stackoverflow.com/questions/652106/finding-neighbours-in-a-two-dimensional-array

1
2
3
4
5
6
7
8
9
10
11
row_limit = count(array);
if(row_limit > 0){
column_limit = count(array[0]);
for(x = max(0, i-1); x <= min(i+1, row_limit); x++){
for(y = max(0, j-1); y <= min(j+1, column_limit); y++){
if(x != i || y != j){
print array[x][y];
}
}
}
}

Here is a direct implementation of this algorithm in Scala.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def findNeighbours(board: Array[Array[Cell]], point: Point): List[Cell] = {
val (row, col) = point unapply
var cells: List[Cell] = List.empty[Cell]
val rowLimit = board.length - 1
val columnLimit = board(0).length - 1
for (x <- math.max(0, i - 1) to math.min(row + 1, rowLimit)) {
for (y <- math.max(0, j - 1) to math.min(col + 1, columnLimit)) {
if (x != i || y != j) {
cells = cells :+ board(x)(y)
}
}
}
cells
}

A pattern to use RequireJS config in a separate file

If you have used RequireJS, you most definitely have come across this: Repeatation of requirejs configuration.

You end up repeating the shims and paths in multiple locations. We can minimise this by using requirejs.config(config) method.

requirejs.config() will help you define or override your dependencies configuration.

1
requirejs.config({'baseUrl': '/my/app/url/'});

Here is an examlpe on how I use requirejs.config method for minimising repeatation.

First let us define the reusable configuration file. Let’s name it rconfig.js.

rconfig.jsview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/** Put common requirejs configurations here **/
define([''], function () {
'use strict';
return {
packages: [
{
name: 'common-directives',
location: '/apps/common/directives/'
},
{
name: 'common-filters',
location: '/apps/common/filters'
},
{
name: 'common-interceptors',
location: '/apps/common/interceptors'
}
],
shim: {
'angular': {
deps: ['jquery'],
exports: 'angular'
},
'angular-cookies': ['angular'],
'ui-router': ['angular'],
'ui-utils': ['angular'],
'ui-utils-ieshiv': ['angular'],
'bootstrap': ['jquery'],
'ui-bootstrap-tpls': ['angular'],
'ui-bootstrap': ['angular', 'ui-bootstrap-tpls'],
},
paths: {
'jquery': ['/lib/jquery-2.1.1.min'],
'angular': ['/lib/angular.min'],
'angular-cookies': ['/lib/angular-cookies'],
'ui-router': ['/lib/angular-ui/angular-ui-router.min'],
'ui-utils': ['/lib/angular-ui/ui-utils.min'],
'ui-utils-ieshiv': ['/lib/angular-ui/ui-utils-ieshiv.min'],
'bootstrap': ['/lib/bootstrap.min'],
'ui-bootstrap': ['/lib/angular-ui/ui-bootstrap.min'],
'ui-bootstrap-tpls': ['/lib/angular-ui/ui-bootstrap-tpls.min'],
'lodash': ['/lib/lodash.min'],
'constants': ['/apps/common/constants']
}
}
});

Here we have defined the paths for all the common dependencies.

Now let us use it in two different places.

main.js for home appview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(function (requirejs) {
'use strict';
// -- RequireJS config --
requirejs.config({
// Packages = top-level folders; loads a contained file named 'main.js"
packages: ['home', 'dashboard',
{
'name': 'dashboard-settings',
'location': '/apps/dashboard/settings'
}
],
paths: {
'rconfig': ['/apps/common/rconfig']
}
});
requirejs.onError = function (err) {
console.log(err);
};
// here we are loading rconfig as dependency
require(['rconfig'], function (rconfig) {
// update config
requirejs.config(rconfig);
// Load the app. This is kept minimal so it doesn't need much updating.
require(['angular', 'jquery', './home', 'ui-router', 'ui-utils', 'angular-cookies',
'common-directives', 'common-filters', 'common-interceptors',
'bootstrap', 'ui-bootstrap', 'ui-bootstrap-tpls'], function (angular, $) {
angular.bootstrap(document, ['home']);
});
});
})(requirejs);
main.js for dashboard appview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(function (requirejs) {
'use strict';
// -- RequireJS config --
requirejs.config({
// Packages = top-level folders; loads a contained file named 'main.js"
packages: ['home', 'dashboard',
{
'name': 'dashboard-settings',
'location': '/apps/dashboard/settings'
}
],
paths: {
'rconfig': ['/apps/common/rconfig']
}
});
requirejs.onError = function (err) {
console.log(err);
};
// here we are loading rconfig as dependency
require(['rconfig'], function (rconfig) {
// update config
requirejs.config(rconfig);
// Load the app. This is kept minimal so it doesn't need much updating.
require(['angular', 'jquery', './dashboard', 'ui-router', 'ui-utils', 'angular-cookies',
'common-directives', 'common-filters', 'common-interceptors',
'bootstrap', 'ui-bootstrap', 'ui-bootstrap-tpls'], function (angular, $) {
angular.bootstrap(document, ['dashboard']);
});
});
})(requirejs);

The file-layout here is like the following.

Hope it helps!

SOLID design principle

When it comes to OO designing, it is a good idea to have a strategy to follow.
A strategy allows you to think and organize ideas in a particular fashion that is comfortable to you.
I’ve found SOLID principle easy enough for beginners and experts to understand and practice.
This was originally put forward by Rober C. Martin, who is a pioneer in agile software development and extreme programming.

Checkout Cleancoders to know more.

Chances are, you may be already using SOLID principle, just without giving it a name.

tl;dr

S - Single Responsibility Principle (SRP)

O - Open/Closed Principle (OCP)

L - Liskov Substitution Principle (LSP)

I - Interface Segregation Principle (ISP)

D - Dependency Inversion Principle (DIP)

Thats right, SOLID stands for a bunch of other acronyms.

S - Single Responsibility Principle (SRP)

The single responsibility principle states that every class should
have a single responsibility, and that responsibility should be
entirely encapsulated by the class. All its services should be
narrowly aligned with that responsibility.

Fairly simple. A Student class should manipulate Student properties and not the the properties of School. Specification of the class remains mostly untouched unless there is a big change in requirement of your software.

It is good to keep in mind that, there can be some exceptions to this. For example a utility class may provide methods for managing both Students and Schools.

O - Open/Closed Principle (OCP)

Software entities should be open for extension, but closed for modification.
The idea is that once a class is implemented completely, it should not be modified for including new features. Bugs and error correction must be done as and when required. Adding a new feature will require new specification and release of newer version.
http://en.wikipedia.org/wiki/Open/closed_principle

L - Liskov Substitution Principle (LSP)

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. See also design by contract.
That right there is the basic use of Dependency Injection, right? You define a parent interface and replace them by concrete implementation which are derivatives.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Animal {
public void makeNoise() {
System.out.println("noise");
}
}
class Dog extends Animal {
@override
public void makeNoise() {
bark();
}
public void bark() {
System.out.println("bow bow bow");
}
}
Dog dog = new Animal();
dog.makeNoise();
@Inject
Animal animal; // can be replaced by Dog

http://en.wikipedia.org/wiki/Liskov_substitution_principle

I - Interface Segregation Principle (ISP)

Many client-specific interfaces are better than one general-purpose interface.
Interfaces provide abstraction no implementation. When developing client interfaces, it should be kept at minimum and should only expose those methods which are essential for that client.

http://en.wikipedia.org/wiki/Interface_segregation_principle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
interface Jumbable {
void jump();
}
interface Climbable {
void climb();
}
class Dog extends Animal implements Jumbable {
@override
void jump() {
//jump
}
}
class Monkey extends Animal implements Jumbable, Climbable {
@override
void jump() {
}
@override
void climb() {
}
}

##D - Dependency Inversion Principle (DIP)

One should “Depend upon Abstractions. Do not depend upon concretions.”
Dependency injection is one method of following this principle.

1
2
3
4
5
6
7
8
9
10
11
interface JumpringService {
public void jump();
}
public class JumpingServiceImpl implements JumpingService {
@Inject
private Jumpable jumbable;
@override
public void jump() {
jumpable.jump();
}
}

http://en.wikipedia.org/wiki/Dependency_inversion_principle

Closures

Closures are a bit hard to understand concept for someone from an Object Oriented programming background, mainly because popular OO programming languages, (Java/C++) does not have this feature (As bjzaba pointed out in reddit, C++ 11 has Closures) haven’t embraced or promoted this feature until recently. It is more of a functional programming concept, although many Object Oriented languages has started to support Closures through first class functions or higher order functions.

I first heard about Closures while developing something in Javascript. If you have used the popular javascript library jQuery, you have already used closures, knowingly or unknowingly.

Here is my attempt to explain Closures, through examples in few programming languages.

From Wikipedia:

In programming languages, a closure (also lexical closure or function
closure) is a function or reference to a function together with a
referencing environment—a table storing a reference to each of the
non-local variables (also called free variables or up values) of that
function. A closure—unlike a plain function pointer—allows a
function to access those non-local variables even when invoked outside
its immediate lexical scope.

What that means:

  1. Closure is a function (or a reference to a function)
  2. You get a pointer to closure
  3. So you can pass it around like an object
  4. It knows about non-local variables
  5. It can access those non-local variables, even when invoked outside of its scope
  6. So we say, closures ‘closes’ on its environment
  7. A function may create a closure and return it.

Few programming languages, that support Closures

  • Lisp
  • Javascript
  • Scala
  • Clojure
  • Ruby
  • Python
  • Haskell
  • PHP

In closures procedure/method/function contexts become first-class. That means, with closures you can create functions that return functions, although that is only an outcome. An important point to understand here is, the closure methods refer to the context in which it was created, not to the one it was called.

To better understand closures one has to understand a variable’s scope, the best read about that would be understanding javascript varialble scope.

Closures store references to the outer function’s variables; they do not store the actual value. So if we change the value of reference in closure it changes value outside of its scope.

You may implement closures using Anonymous functions, but all anonymous functions need not be a closure, although many of them are.

Why would anyone use Closures?

  • Reusability
  • Do more with less code
  • Make functional code stateful

Closures help us to write more expressive and concise code(once you get a hang of it!). We know objects have a state, using Closure we can give state to functions as well.
Now, let us take a look at examples of how to use closures in a few programming languages.

Examples

All of the following examples do the same thing: Create a closure to increment a number by another number.

1. Closure example in Javascript

This would be the easiest to understand code.

Closure example in Javascriptview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var incrementBy = function(x) {
return function(y) {
return x+y;
};
};
// this will remember the value of 'x' forever
var incrementBy2 = incrementBy(2);
var incrementBy3 = incrementBy(3);
console.log(incrementBy2(4));
console.log(incrementBy3(8));
// Here you are creating a closure and calling it immediately
console.log(incrementBy(5)(8));

In the Javascript example above, we define a function incrementBy(x), which returns a function, that accepts parameter ‘y’ and returns sum of x and y. Here the value of ‘x’ will go into the closure of the returned function and will be accessible whenever the function is invoked.
Note that when calling incrementBy2(4) our closure remembers the value 2 (i.e ‘x’) that was passed earlier when doing var incrementBy2 = incrementBy(2);. And when invoking incrementBy2(4) we are actually passing the value of y as 4. Hence the statement return x+y will transform to return 2+4. Cool right!!?

2. Closure example in Scala

Closure example in Scalaview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
object Closure {
// define the closure using one line of code, power of Scala
def incrementBy(x:Int) = (y:Int) => x + y
def main(args: Array[String]) = {
var incrementBy3 = incrementBy(3)
val incrementBy5 = incrementBy(5)
println(incrementBy3(5))
println(incrementBy5(10))
println(incrementBy(20)(20))
}
}

The example in Scala is similar to the example in Javascript except for Scala’s awesome one liner syntax.

3. Closure example in Lisp (Clisp)

1
2
3
4
5
6
7
8
(defun increment(y)
(defun incrementBy(x)
(+ y x)
)
)
(increment 4)
(incrementBy 5) ; 9
(incrementBy 10) ; 14

There are several ways of doing this is lisp. This is only one way of doing it. Here you are not getting a pointer to the closure function and will probably make it useless. See this link to see how to return functions is Clisp.

4. Closure example in Clojure

1
2
3
4
5
6
7
8
(def increment (fn [y]
(def incrementBy (fn [x]
(+ x y)
))
))
(increment 4)
(incrementBy 5) ; 9
(incrementBy 10) ; 14

As you can see, the syntax of clojure and lisp are extremely similar.

5. Closure example in Python

1
2
3
4
5
6
7
8
def incrementBy(x):
def increment(y):
return x+y
return increment
incrementBy4 = incrementBy(4)
incrementBy6 = incrementBy(6)
print incrementBy4(5) # 9
print incrementBy6(10) # 16

The above example in Python is pretty similar to the one in Javascript and is easy to understand. Here we define the closure function increment(y) and then return it. Just remember to take care of the tabs!

6. Closure example in Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Closure
# Ruby example using Proc.new
def incrementBy(x)
return Proc.new {|y| x+y }
end
# Ruby example using lambda
def incrementByLambda(x)
lambda {|y| x+y}
end
end
closure = Closure.new
increment5 = closure.incrementBy(5)
incrementByLambda7 = closure.incrementByLambda(7)
puts increment5.call(6)
puts incrementByLambda7.call(6)

Ruby can do this is in two ways, using Proc and using lambda. The lambda functions in Ruby are similar to lambda’s in lisp.

So that was examples about closures. Hope that gives you at least some idea about Closures. If you know how do the same in any other languages, please feel free to share. Also checkout the references, they are good reads.

##References:

Thanks to Craig for sharing Go snippet

Reddit thread here: http://redd.it/223b7p

Sample app for Play! 2.2, Spring-data-jpa and Backbonejs

I have made a sample application to demo Play! framework 2.2, Spring data jpa and Backbonejs.

It is available at https://github.com/WarFox/play-spring-data-jpa-backbonejs.

Also a demo app is hosted in Heroku at http://play-spring-data-jpa-backbone.herokuapp.com/

It started as a fork from https://github.com/typesafehub/play-spring-data-jpa and then created a separated repo to include backbonjs.

Feel free to fork and play with it.