Wednesday, 25 November 2015

REST API

One really useful book about REST API design is 'The REST API Design Rulebook' by and published by O'Reilly. 

One of my favourite bits was in Chapter 2 about resource archetypes. 

Document


A Document is a THING (for want of a better description). 


Collection


A collection is a server-managed collection of resources. The client can propose additions to the collection. 

From the above examples, collections would be:


Store


A store is a client-managed collection of resources. 

For example to store my favourite female names (my user name = polyglotpiglet): 


Controller


A controller resource is like an executable function with inputs and outputs. It is used in REST APIs when you want to do something that doesn't fall naturally into the CRUD operations. 

For example:



(weird example but hopefully it makes sense).

In this book I thought that the description of controller resources was awesome - it seems to be a common misconception that REST APIs are only appropriate for designing CRUD systems and if you want to do anything complicated it's not possible with REST. This is not my experience. 

Other notes from the book

Some really useful tips about headers (making sure you set the appropriate headers and why it might be useful eg cache control, last modified time, content types etc)

Sunday, 27 September 2015

Euclid's Formula for generating Pythagorean Triples

Pythagorean triple

$$a^2 + b^2 = c^2$$

Euclid's formula:

$$ m,n \in N, m > n $$ $$ a = m^2 - n^2, b = 2mn, c = m^2 + n^2$$ $$ => a^2 + b^2 = c^2$$
Trivial proof.

Primitive pythagorean triples are ones that cannot be reduced (think $ k.a^2 + k.b^2 = k.c^2 $). The triple  is primitive if $a$, $b$ and $c$ are coprime (ie they share no common divisors except 1). An example of a primitive pythagorean triple is (3,4,5).

The pythagorean triples generated using Euclid's formula are primitive iff $m$ and $n$ are coprime and $m-n$ is odd.

Clearly this second point is true because if both $m$ and $n$ were even, then all three terms will be even when squared, so $k$ could equal two so the triple would not be primitive.

Similar if they are not coprime, you could obvs divide every term.




Sunday, 1 March 2015

Why should you expect failures in your datacenter?

Not only should you expect failures but you should expect FREQUENT failures.

If you can estimate that every server in your data centre will fail once every ten years then that sounds pretty good right?

Failure rate = Once / 10 years = once / 120 months

But... if you have 120 servers then that will mean you should expect a failure every month!

According to this article, in 2010 Facebook was running at least 60,000 servers across its data centres. 

If these each of these 60,000 servers is expected to fail once every 10 years, then at that time Facebook would have expected a server failure about every hour and a half (120 months / 60000 ~= 1.46  hr)

Eeeek. 





Scala Puzzler 1: Overrides in Constructors

Question:


What is printed when the following code is executed?


Answer:


This code will print out 'null'.

This is because  the Person constructor is called before the override, meaning that val name is instantiated before the override is called. The default name value is never set because the compiler is clever enough to know we don't wan stringName to be set to "I dont have a name" so it skips that assignment.


Interestingly... 


If the stringName was declared with the 'lazy keyword' the code would work properly and print 'Alexandra'. This is because a lazy variable is declared as soon as it is needed, which in this case would be when the name class is instantiated.



Monday, 9 February 2015

Spray-can/Akka simple application

Spray?


The spray-can module of spray.io provides a framework with which you can build http servers and clients which easily integrate with your own akka system.

Note that according to the akka roadmap, in future releases of akka, spray will be built in and will be known as akka-http.

The Code



Under the covers


Spray request handling

When you start your spray app, spray generates an HttpListener actor that listens on port 8181 for incoming requests. For each client connection, spray spawns a new HttpServerConnection actor which receives a request from an open connection and passes an HttpEntity message to your listening actor (the first parameter to the Http.Bind method).

Binding

You must call bind from within an actor.

IO(Http) ! Http.Bind(self, interface = "localhost, port=8181")

This threw me for a bit because initially the first parameter I passed to Http.Bind was the actor that is handling my Http requests. However, when you call Http.Bind you send an async message off to trigger the bind, then spray sends a Tcp.Bound message back to the actor from which you called Http.Bind, not the actor that you pass in as the first parameter.

Try it out



curl localhost:8181/ping

Saturday, 24 January 2015

Simple hacks for Project 5 in the Arduino Uno Starter Kit

As you can see from this thread, there are some minor obstacles to overcome when wiring up project 5 in the arduino starter kit. I thought I'd give it a go and see if it's as annoying as this guy makes out.

This project uses the following components:

- 2 100uF decoupling capacitors
- 1 servo motor
- 1 potentiometer
- a few wires

Constructing the circuit: 


- hook up the potentiometer with 1 of the capacitors
- and the motor with the other capactior

Program it to read from the potentiometer and move the motor (which has a 180 degree range) accordingly.


Tacking the problems in the 'official' instructions


1) "The base of the potentiometer is too wide to fit as per the book diagram. It stretches 5 holes across, in the book the diagram shows it as 2. I cannot make the circuit in the book."

Yeah, ok, I'll give him this. It's a bit annoying to order a starter kit with a solderless breadboard then have components that are clearly meant to be soldered on.

My solution: 3 jumper wires (2 tiny ones and 1 slightly longer one) and some blue tac. (I didn't say this would be pretty)

I looped the jumper wires around each of the pins and secured it with blue tac. Jobs a good 'un.



2) The header short end is too short to fit into the servo female end.

Someone on the thread suggests taking out pliers and forcing the plastic off to make these male header pins the right size.

My solution: 3 short jumper wires

I just ignored the male header pins entirely and stuck three wires in there.



3) "The potentiometer 'base' just doesn't fit well into the board at all and whilst the circuit I've made works, it's all over the place unless I press down very very hard on the base. "

As mentioned above I didn't attach the potentiometer directly.

4) "I also discovered that the white leads in this mouser kit actually vary in length"

Unable to reproduce this problem. :-)

5) "In the book circuit the leads are from the top down black, red, white, but on the supplied servo the order is different, black, white, red. "

Is this really a problem? For a servo motor with arduino, attach the white wire to a PWM enabled digital pin (the example in the instructions it says attach to ~9 but it doesn't matter which), the black wire to ground and the red to 5V.

My additional problem #6

I didn't have a screwdriver tiny enough screw the arm into the motor, but I found that a pair of tweezers did the job.

My final circuit



Simple Arduino PWM Example with Cathode RGB LED

Cathode RGB LED



These LEDs have 4 prongs. The longest prong is the cathode, which you should hook up to ground.

The other 3 prongs are attached to separate elements in the head of the diode and these elements produce red green and blue light separately.

Our aim: To manipulate these RGB elements independently to produce different colours.

Digitally setting RGB elements on and off


Create this circuit:



Features of the circuit:
- the red prong of the LED is connected to digital pin 11.
- the blue prong of the LED is connected to digital pin 10.
- the blue prong of the LED is connected to digital pin 9.
- appropriate resistors are in place to ensure the LED doesn't get too many Volts which would cause it to blow. (Choosing the resistor is not covered in this post.)

DigitalWrite()


We can switch on/off the red, green and blue components independently using digitalWrite().



Mixing RGB


My arduino outputs 5V. However, I want to be able to reduce the voltage flowing through my LED to get colours other than the set above. For example to produce yellow, I want a bit of red and lots of green.

This can be achieved using Pulse Width Moderation.

Voltage going through the circuit can be switched on and off extremely quickly to effectively give voltage between zero and 5V.

- the more time it spends in HIGH the greater the effective voltage
- the more time in LOW the lower the effective voltage.

Because we are connected to the three separate pins (9, 10, 11) we can apply this technique independently across R, G and B.

To code this we use analogWrite().

AnalogWrite()

You will notice that the three pins (9, 10, 11) to which we are connected are marked with a ~. This means that it is possible to do PWM when connected to them.

The following code fades the LED from RED - BLUE - RED - ... .




Monday, 12 January 2015

brew cask

Most developers working on Mac will use homebrew to install packages. From the homebrew documentation on github:

"Our policy is that formulae in the core repository (Homebrew/homebrew) must be built from source. Binary-only formulae should go to Homebrew/homebrew-binary."

I imagine this is because it's safer to compile from source because anyone could put anything in a executable. However there isn't a great deal here in this homebrew-binary directory. In practice, for binaries on mac people generally use cask.

To install cask:

        brew install caskroom/cask/brew-cask

Install binary applications with cask:

        brew cask install visualvm
        brew cask install google-chrome

Cask is implemented as an external command in homebrew. 

Get syntax highlighting and formatting for Scala in Vim


There are a few copies of some .vim files floating around the internet which provide syntax highlighting and nice indentation for .scala and .sbt files. I have put them onto my github and here is a one liner to download the scripts and store them in the the vim configuration directory so they are picked up when you use vim: 

mkdir -p ~/.vim/{ftdetect,indent,syntax} && for d in ftdetect indent syntax ; do curl -o ~/.vim/$d/scala.vim https://raw.githubusercontent.com/apojha/scala-vim-support/master/$d/scala.vim ; done

Note that you must have 'syntax on' in your .vimrc file (or just ':syntax on' when your scala file is open in vim but that might get tedious if you are editing a lot of files in which case go use something sensible like IntelliJ. . :-) )

Monday, 5 January 2015

Zero to running memcached in 10 minutes (if you have a good download speed)

In two weeks I am moving to a Scala job. Someone suggested that a nice way to get started with a new language is to implement a 'mini memcached'. Open a socket, send some key/value pairs, store in a hash map: the idea is it's good to do a bit of network programming and get exposed to some libraries (collections, concurrency etc). I haven't quite figured out the spec for my mini memcached but to start, I though I'd try sending some stuff to a real instance of memcached and then swap it out for my mini version.

Installing Memcached on Ubuntu


sudo apt-get install memcached

After doing this the memcached service was running.

When I did this, memcached was not running on all network interfaces by default. Therefore I changed the memcached config file:

sudo sed -i 's/127.0.0.1/0.0.0.0/g' /etc/memcached.conf

Then I restarted the service:

sudo service memcached restart

Telnet to memcached to store a key/value pair


alexandra$ telnet 192.168.2.2 11211
Trying 192.168.2.2...
Connected to 192.168.2.2.
Escape character is '^]'.
set keyname 0 60 2
hi
STORED
get keyname
VALUE keyname 0 2
hi

END

If anything weird is happening, check the log:

 tail /var/log/memcached.log 

Vagrant VM running memcached


https://github.com/apojha/vagrant-vms/tree/master/memcached

Scala with Cats: Answers to revision questions

I'm studying the 'Scala with Cats' book. I want the information to stick so I am applying a technique from 'Ultralearning...