Tuesday, September 30, 2008

Side Effects and Functional Programming

Side Effects and Functional Programming - Matthew Podwysocki
One of my first posts at CodeBetter was in regards to side effects and how, when unmanaged, can be truly evil. Today, I want to revisit that topic briefly in regards to functional programming and managing side effects. When I was out in Redmond a couple of months ago, I had the opportunity to sit down with Erik Meijer to discuss functional programming among other topics. In there, we discussed a number of issues around managing side effects and state in your code, and how both C# and F# don't intrinsically support such a concept. Languages like Haskell, of course do with IO monads and other such monadic structures. Whether languages such as F# and Erlang are not pure functional programming languages is another matter, due to the fact that you don't have to declare when you are side effecting (reading a database, writing to console, spawning a process, etc).

Side Effecting Functions are Code Smells

Side Effecting Functions are Code Smells - Matthew Podwysocki
I know the title might catch a few people off guard, but let me explain. Side effecting functions, for the most part, are code smells. This is a very important concept in Domain Driven Design (DDD) that's often overlooked. For those who are deep in DDD, this should sound rather familiar. And in the end, I think Spec# and some Design by Contract (DbC) constructs can mitigate this, or you can go the functional route as well.

Monday, June 2, 2008

Chess Problem of the Week No 3

Black to move and win material. Thanks to Tony for this puzzle.

Wednesday, May 21, 2008

Chess Puzzle of the Week No 2

Another stumper! well for me anyway, it took me twice as long as chess puzzle 1 to figure out. I hope you guys enjoy it :).

White to move mate in 2.

Thursday, May 15, 2008

Chess Puzzle of the week No 1

I have decided to put a chess puzzle on my blog at least once a week. Mainly to force myself to do at least one chess puzzle a week along with my chess playing. Hopefully these puzzles will help me improve my game.

The puzzle below was created by Max Euwe who was world champion from 1935 - 1937. White to move, mate in 2. Post up you answer if you think you have it, even better post up your answer in algebraic chess notation - I need to get used to understanding it.

Wednesday, April 2, 2008

Adding a type sysm to Ruby

An interesting post on adding a type system to ruby.
http://www.codecommit.com/blog/ruby/adding-type-checking-to-ruby

Wednesday, March 12, 2008

My groups on Scoodi.com

Just found this new feature on www.scoodi.com where I can create a link to the items I'm selling/giving away on scoodi. All I need to do is follow the steps on the my groups page which you can get to from the quick links box. Here are my items on scoodi. A nice little addition to the scoodi site.

Just as an extra I think it would be nice to be able to embed your items into another website as well, like how youtube does it. As an example with the following embed code from youtube can see a video from without the whole youtube site.

<object height=\"355\" width=\"425\"><param name=\"movie\" value=\"http://www.youtube.com/v/_Cael8grlw8&amp;hl=en\"><param name=\"wmode\" value=\"transparent\"><embed src=\"http://www.youtube.com/v/_Cael8grlw8&amp;hl=en\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" height=\"355\" width=\"425\"></embed></object>


Sunday, January 27, 2008

Lift (scala web framework) vs Ruby on Rails

I found this little comparison between the scala lift framework and ruby on rails quite interesting. The points that stood out were that using lift was a using far less CPU usage than rails and that the defects in scala were far less. I would expect scala to have mush less defects due to the type safety but it is good to see a real world comparison.

Though I do wonder how much longer it took him to write the scala code over the ruby code.

Good Post for Explaining the Java Hashtable

My brain had seemed to have forgotten the underlying details of a hashtable. So I went out looking for an article that could explain how the java hashtable works and how it links to hashcode() method. I found this nice article that explained what I need quite nicely - link.

Saturday, January 26, 2008

My Toowong unit renovation Photos

Here is a link to my unit renovation photos that I have been renovating since may 2007. I hope to get it finished well actually I will get it finished my may 14 th so I can go and renovate my girlfriends unit.

Tuesday, January 22, 2008

Configure window placement in Ubuntu

I have been trying to figure out how to configure my start up applications to display in a certain workspace. The following thread shows how to do this with a tool called Devilspie
http://ubuntuforums.org/showthread.php?t=75749t

I'll be playing with this during the week and I'll blog how easy/hard it was to use.

Monday, January 21, 2008

Scala exercises

Though I would post up some of the exercises I have been working on to help me understand functional programming in Scala. These exercies were given to me by tony Morris to help me get my head around function programing.

1. Create a flat map using a foldRight
solution:
def myFlatMap[I, J](l : List[I], f: I => List[J] ) : List[J] = {
l.foldRight(Nil : List[J])((a,b) => (f(a) ::: b ) )
}

2. Create concatenation for a cons list (:::) using a foldRight
solution:
def append[X](xs: List[X], ys: List[X]): List[X] = {
xs.foldRight(ys)(_ :: _)
}

3. Create concatenation using matcher and recursion
solution:
def append2[X](xs: List[X], ys: List[X]): List[X] = xs match {
case Nil => ys
case x :: xs => {
x :: append2(xs, ys)
}
}

4. Find the max int in a list
solution :
def getMaxInt(xs : List[int]) : int = xs match {
case Nil => error("error")
case x :: xs =>
xs.foldLeft(x)((b,a) => if ( a > b) a else b )
}

Sunday, January 20, 2008

Programming Terms

While learning scala I needed to know some terms to understand what is going on. So here is a list of terms I needed to understand with an example of what they are in the languages I understand and a link to the definition.

First class object: In java this is an object
wikipedia link

First class functions: In ruby this a a block/proc. First class Functions can be created at runtime stored in a variable and passed into other methods.
wikipedia link

Higher order function: In ruby this is a function that take a block.
wikipedia link

Currying: transforming a function that takes multiple arguments into a function that takes 1 arguement. This can be done by exploiting the fact that a function can be returned by a function.
wikipedia link

Partial Application: Passing less than the full argument list for the function.

map: List[A] => (A => B) : List[B]. Apply a function over a list and return the resulting list

flatMap: List[A] => (A => List[B]) : List[B] . Apply the function A => List[B]
on each element in List[A] and concatenate the results and return the concatenated results.

foldLeft: List => B => ((B, A) => b) : B . Iterate over the list from left to right starting with the value B, accumulate on B and return B. This is the same as the ruby inject method

FoldRight
List => B => ((A, B) => b) : B. same as foldLeft but iterates from right to left.

Associativity: Means that without changing the sequence of the operands, the order of calculation does not effect the result. i.e. (1 + 2) + 5 = 8 = 1 + (2 + 5)
wikipedia link
Commutativity: Changing the sequence of the operands does not effect the result i.e 1+ 2 + 5 = 8 = 5 + 2 + 1
wikipedia link


covariance: to be filled in

contravariance: to be filled in

functor: to be filled in

monad: to be filled in

Thursday, January 10, 2008

Adding Startup Programs in Ubuntu

Just found out today how to add start up programs in Ubuntu. Pretty simple really. All you need to do is go to system -> preferences -> sessions. Then you get the following screen
In the start up programs tab click add. In the dialog box add a name, the link to the program and a description. thats it.