Understanding Clojure Concurrency Primitives

If you're like me, you got confused when first researching Concurrency in Clojure. Should I use a ref here? How about an atom? Maybe an agent?

Refs

Refs are used when you have more than one piece of mutable state and they all needed to be updated at the same time; if they weren't updated at the same time, the data would be invalid. This happens synchronously.

Atoms

Atoms are used when you have a single piece of mutable state which is independent of any other mutable state. This happens synchronously.

Agents

Agents are used when you have a single piece of mutable state. This happens asynchronously.

Ladder of Control

With refs at the top, atoms mid-way, and agents at the bottom, it is a ladder of control. The higher you climb the ladder, the more control you have.

What I've been working on

Messing around with an in-browser XHTML/CSS editor. It currently supports selecting elements via mouse; entering selectors; keyboard short-cuts for margin and padding, and css generation.

Please note: the properties sidebar is just for testing purposes at the moment - more functionality is next on my todo list.

Here it is in action:

I recorded these usingĀ Screencast-o-matic. It uses a java plugin - nothing to "download". It then exports it to YouTube. Pretty good.
Filed under  //   css   html   jquery   oditor  

Euler Project #10 in C#

Euler Project #10 asks for the sum of all prime numbers below 2,000,000.

There are hundreds of different ways of generating prime numbers; some are fast but memory-hungry, others are slow but use only the space they need.

Generating 2,000,000 primes by means of looping through every number and checking whether it is divisible by more than 2 numbers is awfully slow. Don't fret, there is another method; more memory-hungry, but lightning fast. It's know as the sieve approach.

The sieve approach works by "sieving" all of the prime numbers.

First, there needs to be an array of booleans as big as the limit of primes you need to calculate. For example, we need to calculate all prime numbers below 2,000,000 - so we need an array of 2,000,000 booleans. All of these booleans should be set to true by default.

Next up, comes the sieving part. Starting at 2, we see whether this is currently marked as a prime number (this is the beginning, so it will be). Now we know that any multiple of 2 (that's bigger than 2) is not going to be a prime number. So we loop through every multiple of 2, marking it as not a prime number (setting the boolean to false), up until the end of the array. Now we move on to the next number, 3. We loop through all of the multiples of 3, and mark them as not prime numbers. After this has looped through every number, we are left with an array of booleans. The index of every boolean that is set to true is a prime number.

There is also nice demo online which shows you it happening.

The only addition we need to make is to keep a running total.

The finished product:

Syncing Folders Outside of the Dropbox Directory

The only way to share folders outside of the Dropbox directory is to use symbolic links (Unix) or shortcuts (Windows).

ln -s /Target/Directory ~/Dropbox/Directory

And for Windows users, I'm sure you can work it out yourself.

Evolution and Mutation in Ruby

Been messing around with genetic mutation algorithms, and this is what I got after messing around in Ruby for 15 mins:

Horribly coded - just messing around.

Metarocket Launch

The Metarocket website has just launched. You can also follow @metarocketnet on twitter. 

I'm a Web Developer at Metarocket. I designed and developed metarocket.net, let me know what you think of it.

That's all for now, folks.

Textmate Bundles

Textmate doesn't support an easy way of downloading new bundles (from http://svn.textmate.org/trunk/Bundles/). Here's a little script to help you.

It downloads the new bundle, and tells Textmate to reload the bundles.

Scratching my own itch.

Function Composition in Haskell

Function Composition:

Media_httplearnyouahaskellcomcompositionpng_mwxahilqnbieeji

In haskell, it's defined as:

As you can see, there is a lambda in the definition. An obvious use for function composition then is for the map function. Often there are times when you need to apply two functions for every element in a list, and this is pretty much made for that occasion.

It also has a simpler use: getting rid of that jungle of parenthesis. These two are identical:

The only reason that function composition can be implemented (and in such a beautiful way) is through high-order functions. They are the life and soul of Haskell. For those of you that don't know; a high-order function is any function that passes a function as an argument and/or returns function.

One sign that I may be embracing Haskell a little too much: I'm beginning to think being lazy isn't a bad thing.
Filed under  //   haskell  

Separating Structure and Content in HTML

I've always had a feeling that structure and content should be separated in HTML. And I'm going to do something about it.

This is currently how I envision it:

So you have a structure file, and a content file (or multiple content files).

"Why?" you ask. I'll tell you. If you're making a web app in Rails, and you want to put some content in the sidebar, chances are you have to fiddle with content_for() or something similar. This is fine but, for me, it feels too hacky.

When structure and content have been separated, you can add content to the page from anywhere.

Bare in mind this is a quarter-baked solution in my head, so I imagine there are plenty of holes in this theory.

I'm going to be building a parser (in Scala) to see how if it's actually a good idea or not.

I'll keep you folks updated.

Filed under  //   html  

Easy Website Base Setup

Whack this in your ~/.bash_profile

And away you go

About

Hi, I'm Sam Nardoni, a freelance Web Developer available for hire - feel free to contact me. I'm also a student - studying BSc Computer Science at University of Plymouth.

TwitterLinkedInFlickrYoutube