London Cinema Revamped

I’ve revamped londoncinema.bluebones.net.

As well as a new design it has much-improved cinema and film pages, movie posters, trailers and imdb.com ratings.

Search is now even better because as well as taking your location it also uses imdb.com ratings to push better films higher up. With so many films showing it’s so easy to miss something good and with so many cinemas in London it’s so much better than trawling through cinema-by-cinema.

Formatting Decimals in Haskell

A formatting function to go from numbers like 333999333.33 to “333,999,999.33” in Haskell. Copes with negative numbers and rounds to 2 dp (easy to add a paramater for that should you wish).

Examples:

*Main> formatDecimal 44
"44.00"
*Main> formatDecimal 94280943.4324
"94,280,943.43"
*Main> formatDecimal (-89438.329)
"-89,438.33"
import Data.Graph.Inductive.Query.Monad (mapFst)
import List
import Text.Printf

formatDecimal d
    | d < 0.0   = "-" ++ (formatPositiveDecimal (-d)) 
    | otherwise = formatPositiveDecimal d 
    where formatPositiveDecimal = uncurry (++) . mapFst addCommas . span (/= '.') . printf "%0.2f" 
          addCommas = reverse . concat . intersperse "," . unfoldr splitIntoBlocksOfThree . reverse 
          splitIntoBlocksOfThree l = case splitAt 3 l of ([], _) -> Nothing; p-> Just p

If you know a simpler way or spot anything that should be done differently, please add a comment.

Speed Up Very Slow darcs Push/Pull

After investigating a lot of blind alleys I think I’ve solved a problem with pushing and pulling to and from a remote repository being very slow. darcs was issuing hundreds of scp commands just to push up a one line change. This seemed to be related to the _darcs/inventory file being a couple of thousand lines long (should be much smaller) on the remote repo.

In the final analysis my advice is very simple:

  • Make sure you have recently done a darcs tag
  • Issue a darcs optimize --reorder-patches command (the reorder patches part may be unnecessary and cause it to be much slower, but it worked for me although it took three hours).

This reduced push/pull time from 3 minutes to nearer 3 seconds.

AdBlock Plus

After reading A World of Endless Advertisements I thought it would be interesting to see the AdBlock Plus version next to the unblocked version.

SD Times with Ads
SD Times without Ads

How “ad-supported content” is supported without ads, I don’t know. But as Banksy says:

“Any advertisement in public space that gives you no choice whether you see it or not is yours. It belongs to you. It’s yours to take, re-arrange and re-use. Asking for permission is like asking to keep a rock someone just threw at your head.”

(Faintly amusingly I actually had difficulty writing this post because AdBlock blocked the images I was using because their name contained the word “ads”.)

Combining make and cabal

I had to integrate a haskell program into an existing make-based intrastructure this week. After a few false starts I managed to proxy to cabal through make with the following Makefile. I also added a feature to copy the binary created to a “bin” folder at the same level as the src folder. I’ve used the Haq example name from How to Write a Haskell Program and my cabal setup is similar to the one described there. Trying to compile Setup.lhs or anything more complicated is a mistake and I am assured that runhaskell is portable.

all:
	runhaskell Setup.lhs configure
	runhaskell Setup.lhs build
	cp dist/build/haq/haq ../bin/

clean: 
	runhaskell Setup.lhs clean
	rm -rf ../bin/*

Replace in Haskell

Haskell seems to be missing a String replace function. Text.Regex.subRegex seemed like overkill. So I wrote one. It actually works on any list, not just Strings.

replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace [] _ _ = []
replace s find repl =
    if take (length find) s == find
        then repl ++ (replace (drop (length find) s) find repl)
        else [head s] ++ (replace (tail s) find repl)

Some examples:

*Main> replace "hello" "h" ""
"ello"
*Main> replace "hello" "l" ""
"heo"
*Main> replace "hello" "x" ""
"hello"
*Main> replace "100,000,000" "," "hello"
"100hello000hello000"
*Main> replace "100,000,000" "," ""
"100000000"
*Main> replace [1,2,3] [1] [9]
[9,2,3]
*Main> replace [4,5,6,1,2,3,7,8,9,2,3,6,5,4,1,2,3] [1,2,3] [10]
[4,5,6,10,7,8,9,2,3,6,5,4,10]

If this function is already in the standard libraries somewhere or if this can be improved in some way please leave a comment to let me know. Thanks!