node.js Screencasts
06 Jan 2013I think that if I was to dip my toes any further into the pool that is node.js, I’ll be hitting up this set of free screencasts. https://learni.st/users/tilley.brandon/boards/3254-nodecasts-node-js-screencasts
I think that if I was to dip my toes any further into the pool that is node.js, I’ll be hitting up this set of free screencasts. https://learni.st/users/tilley.brandon/boards/3254-nodecasts-node-js-screencasts
Performing IO in our pure environments can be dangerous. Who knows what people put in files that our programs are expected to read? There are tools that can help assure ourselves that we’ll at least clean up if an explosion occurs. In this post, I’ll talk about exception handling with IO
actions using bracket.
bracket is a function defined in Control.Exception
and is defined as follows.
bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
Its first parameter is a function that acquires a resource (or handle). Its second parameter is a function that releases the resource acquired in the first. The last parameter is another function that uses the acquired handle to get a result. The error handling comes in because the 2nd parameter, the function that releases the acquired resource is called even if an exception occurs during the execution of the last parameter. That’s nifty. An example use of bracket looks as follows.
bracket
(openFile "myfile.txt" ReadMode)
(\handle -> hClose handle)
(\handle -> do { ... })
So, there’s another safety mat for when you venture into the impure world.
A really easy way to take the headache out of keeping your gems at the right version (especially when source controlling your projects) is to use a package manager. Today’s post is about Bundler which helps maintain your environment for Ruby development.
Install bundler.
$ gem install bundler
You’re now installed and ready to start bundling. The first job that you need to do is to write a metafile containing all of the gems that your application requires and where bundler should fetch those gems from. This metafile is called your Gemfile
. A Gemfile
will take the following format.
source "http://rubygems.org"
gem "nokogiri"
gem "premailer"
gem "tlsmail"
The source
tells bundler what site to download the gems from. The most common ones that I’ve seen are as follows.
source :rubygems
source "http://rubygems.org"
source :rubyforge
source "http://gems.rubyforge.org"
source :gemcutter
source "http://gemcutter.org"
You can see each of the sources here with a symbolic shortcut that you can use also. The gem
tells bundler that you have a dependency. You can also constrain the version of your dependencies on these lines by using version information after the gem. Now that you’ve created your Gemfile
, you can install all of the gems and their dependencies simple by changing directories to where your Gemfile
resides and typing the following at the prompt.
$ bundle install
Now you have all of your dependencies installed for your application. Doing this will generate a Gemfile.lock
file in your directory also. Make sure that you source control both your Gemfile
and Gemfile.lock
.
That’s it.
Doing some further work in the world of Haskell and have come across the Either type from the base library on a few occasions. Today I’ll post about how to work with this type as you’ll come across it a bit and it is quite handy.
Just as its english counterpart describes, Either can represent one value or another. Scenarios where this might be the return value from a function where you may get the successful result value or you might get an error value. The Either data type is defined as follows.
You construct an Either by calling either the Left
or Right
constructor. So, Either is just a wrapper around a value that can be either one or the other.
In GHCI I have created two instances of Either. “lefty” is constructed using Left, “righty” with Right.
> let lefty = Left 10
> let righty = Right "John"
> :t lefty
lefty :: Either Integer b
> :t righty
righty :: Either a [Char]
That all seems pretty straight forward. Calling Left
or Right
gives us back a value of an incomplete type. Haskell only really knows how to fill the types out that we’ve actually used. Having a look at the values that we’ve created, we’re reminded that they are either Left
or Right
values as such.
> lefty
Left 10
> righty
Right "John"
Convenient, but if we’re going to have a chance of using these values for anything real, we’ll need to extract or unbox the value from the Either construct. The Either type has two functions which will take the boxed values into array called lefts
and rights
. This makes sense. Take a look at how these functions interact with lefty and righty.
> lefts [lefty]
[10]
> rights [lefty]
[]
> lefts [righty]
[]
> rights [righty]
["John"]
They’ve now been taken out of the Either
construct and are values ready to be processed sitting in a list. In the next example, we use pattern matching to detect if we’re trying to divide by zero. Even though my preference is to always say that it’s infinity, computers just like to complain about it.
safeDiv :: Float -> Float -> Either String Float
safeDiv x 0 = Left "Divison by zero"
safeDiv x y = Right (x / y)
The type that’s used here Either String Float
says that we’re either going to receive a String
or a Float
in this value. You can see the case for zero division offering a String
on the Left
, otherwise we supply the quotient on the Right
.
There ya have it!
One day, when I get a chance I will get through this entire article and actually give it a crack, but, until they invent the 48 hour day I’m stuck with just making a bookmark post. I’ve scratched the surface of this article and it’s comprehensive. Really very interesting if it’s your sort of thing.