Skip to main content

Posts

Showing posts from 2009

Scala: Multi-core for loop

Two of the things that really attract me to Scala are its immutability and threading approaches. It seems to me that both of these make Scala a great candidate for multi-core and cloud-friendly constructs.

Here is what I developed to help me write a multi-threaded iterator (of course, these are only used when the order of execution for each element doesn't matter):



def fire[A,B](expanded:A=>B):Fire[A,B] = new Fire[A,B](expanded);

class Fire[A,B](val expanded:A=>B) {
def forall(list:Iterable[A]):Any = {
var caller = self;
list foreach {
el =>
actor {
caller ! expanded(el)
}
}

list map {
el =>
receive {
case result:B => doReceive(result)
}
}
}

protected def doReceive(result:B):Any = {

}

def andlisten[C](contracted:B=>C):FireAndListen[A,B,C] = new FireAndListen(expanded, contracted)
}

class FireAndListen[A,B,C](expanded:A=>B, contracted:B=>C) extends Fire[A,B](expanded) {

Scala: Map of Lists, Part II

A couple of posts ago, I talked about an attempt to create sort of a group by method for partitioning an existing list.

Well, I fussed over it for a while longer and came up with this:

Instead, I created an implicit function, and removed the projection part (C to B) to get the following:


implicit def listenhancer[C](list:List[C]) = new {
def groupby[A](keyresolver:C=>A) = {
Map[A,List[C]](
(list map { keyresolver(_) } removeDuplicates)
map { key => (key, list filter { key equals keyresolver(_) }) }
: _*
)
}
}


Now, I know that some in the Scala community don't like implicit functions. For now, I think that they create very clean-looking code, and so they are still on my "Good Coding Practice" list.

Anyway, one of the things that I noticed about the other is that the keys list had a whole bunch of duplicates, and so the valueresolver method was getting called too many times. So, I added the 'removeDuplicat…

Scala and Testing

In our company, we have a rich and mature Java stack that we draw from for nearly every project. However, I politely told my manager that, for this project, we would be using Scala. I explained that we would still be able to use everything on the Stack, and we would get it done faster and cheaper that way.

So, happily, he gave me a little bit of line.

When it came to writing Unit Tests, we wanted to use EasyMock, an awesome Java library for mocking, stubbing, etc.

Everything worked great, but I ran into one little problem:

If I have the following code:


EasyMock.expect(myClass.myMethod(myParam)).andReturn(myReturn);
EasyMock.replay(myClass);
... do unit testing ...
EasyMock.verify(myClass);
EasyMock.reset(myClass);


Then, I get on the replay call:


java.lang.IllegalArgumentException: not a proxy instance
at java.lang.reflect.Proxy.getInvocationHandler(Proxy.java:637)
at org.easymock.EasyMock.getControl(EasyMock.java:1600)
at org.easymock.EasyMock.replay(EasyMock.java:1502)...


This is because…

Scala: Map of Lists

Every developer has had to deal with a Map whose key is of type X and whose value is a list. Additions to the map often happen like this in Java:


List l = map.get(key);
if ( l == null ) {
l = new ArrayList();
map.put(key, l);
}
l.add(value);


Kind of cumbersome, and kind of not thread-safe.

What is to be done?

Well, google-collections tries to take care of the cumbersome-ness with their MultiMap class. The put looks the same as in java.util.Map, but the get returns a List instead.

So, instead of the code above, you get:


map.put(key, value);


Now, if you want thread-safety as well, you call MultiMaps.synchronizedMap, passing your existing MultiMap.

So, I had a specific case that I was working on which was basically a group by function given a list of C. My decision was to create in the end a map with an A for the key and a List of B for the value where A is the type of the group by property and B a projection of C (So, the List could be of C as well).

Here is what I came up with:


def inclAsList[A,…

Beauty Beyond Belief

Three weeks after our trip up to Alaska and Victoria, BC, I am still reeling from shock at our last "tourist" stop: Butchart Gardens.

I had heard about it, and I had even seen several pictures from various people who had gone, and, I must say, I was still hopelessly unprepared for the overwhelming beauty of the whole thing.

Just look how gorgeous it is!



The colors, the variety, the order are unlike anything that I have ever seen.



It's beautiful in the day time....



It's beautiful at night...
It's beautiful through a crappy can't-take-night-time-pictures camera!



Even the garbage cans were exquisite!



It was even beautiful when my finger invaded the upper right portion of the lens during a night-time photo with my can't-take-night-time-photos camera!



And, of course, there was a piece of time and space etched out in Butchart Gardens that perfectly complimented my wife's own striking beauty...



So, yeah, it was absolutely, unequivocally, unmistakably awesome beyond a…

The Cost of Conflict

There are two things that make freelancing attractive and rewarding for me:

1. Finding out what is possible
2. Showing clients what is possible

By the first, I mean that having several small projects from several different clients inevitably produces industry breadth. One month, a freelancer may be developing an online shopping cart in Java or PHP and the next month an online data feed in Google Base.

By the second, which is actually the impetus for this post, I mean that there are several people out there who have no idea what possibilities the software industry can afford them. It is wonderful to hear a person talk about a vision that they have, later to see in a prototype that their vision can often be realized in today's technology.

In this case, #2 was a biggie. Mike, owner of The Cooperation Company, told me at the beginning of our collaboration that he wasn't sure if services like mine even existed. It was a pleasure as always to help give a customer a pleasant surpri…

Print E-Z

This project had a very unique requirement: They wanted to dynamically serve up their products without augmenting their application with a server-side technology like PHP or Java. More specifically, they wanted their products to be pulled from a datasource instead of being statically specified in HTML.

Hmm...

What if we used a data source that could be hosted elsewhere and served over Javascript?

GoogleBase to the rescue!

In this project, we used GoogleBase to store their products and then javascript to read them from GoogleBase and render them on the correct pages.

It was tricky, but we were successful.

Relationships

There was just one problem that we found during implementation, and that was that Google Base just wasn't prepared to have many integer values assigned to a single attribute. We needed this in order to map each product to the pages that it ought to appear on.

To make a long story short, we tried countless strategies over the course of a couple of weeks until the following…

Cummings Studio Chocolates

This is a chocolates website that was a lot of fun to develop because I got some free chocolate out of it! It is based off of ZenCart, which is based off of osCommerce. On this site, I did all of the graphic design, most of the content definition, and all of the custom programming.

ZenCart is a PHP shopping cart application that is a huge step forward from the base osCommerce. While I have a new open source shopping cart application favorite (KonaKart), this was my favorite for a long time.

We customized the shipping options for local delivery as well as regularly shipped as well as some custom programming for a Design-Your-Own-Box section that they were very excited about.

Note: The site is currently in disrepair due to a versioning issue. You can still view quite a bit of it; however, you won't be able to get all the way through the site. I am working to fix it.

The Pi-Dye T-Shirt Shop

So, this one is my baby. I developed this site for a personal business several years ago with the help of my good friend Benji Smith. It is developed in PHP and is based on the open-source shopping cart application osCommerce. Benji did most of the look-and-feel, while I designed the t-shirts and did the custom programming.

Some of the custom programming elements were:

1) Integration with PayPal
2) Attribute-based weights
3) Hot products listing

In summary, this showcases PHP development, integration with open source, and graphic design.

The Longaberger Company

So, I picked up a little bit of freelance work this last month to pay for the Tile Surprise that I gave my wife after our Alaskan Vacation (that was an expensive June).

Anyway, at the end of one job, my client asked for my portfolio to hand it off to others.

Gasp!

It's been so long that I've done freelance work that I'm not sure that I have one!

So, here goes nothin'. This is the first post of several that will "showcase" my past work.

The first that comes to mind is The Longaberger Company:




Isaac,

I remember when I gave you your first blessing, your name blessing. I said what came to my heart. With all of my children, unique things came that I truly believe are God's will concerning your salvation.

I want you to know about something specific that I was impressed to say in your blessing. It was that you would find solemnity and peace through prayer to your Heavenly Father.

Your name means "He will laugh, and God will hear." It means to me that even your laughs are heard by God. That makes you special. When you speak, He listens, and I believe it down to my core.

There is a scripture where I see you when I read it. It goes like this:

35 When heaven is ashut up, and there is no brain, because they have sinned against thee; if they pray toward this place, and confess thy name, and turn from their sin, when thou afflictest them: 36 Then hear thou in heaven, and forgive the sin of thy servants, and of thy people Israel, that thou ateach them the good wa…

Gratitude

My little 9-month-old girl, Seren, was hit with several things at once the other day.

I was plowing away at work that morning when Kristi called me on the phone to tell me that she had just come back from the doctor's with Seren. Kristi worries more about our two little girls than she did with Remi and Zac, and so it wasn't out of the ordinary to hear that she had taken her there.

I asked her what the doctor said.

"A urinary tract infection."

"What?! Are they sure?"

Kristi explained that they were going to run more tests to be sure because a UTI is so rare for little babies. They didn't have any idea of the cause, but they sent Kristi home with a prescription. (To date, while they confirmed the diagnosis, they still aren't sure what caused it.)

Well, the following day, Seren started to vomit, have diarrhea, and a terrible fever.

Seren is our most pleasant baby. Actually, she is the most pleasant baby that I have ever met. She almost never cries, and s…