28 September 2009

0 Comments
Posted in Arrangement, Art, Business

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) {
override def doReceive(result:B):C = contracted(result);
}



Thus, you get something that looks like this:


fire {
myVar:myType => myMethod(myVar1)
} andlisten { //optional
myVar2 => myMethod2(myVar2) // the type of myVar2 is the return of the previous method. If the return type is Unit, then this section is unnecessary
} forall myList


This is effectively the same as:


myList foreach {
myMethod2(myMethod(myVar1))
}


except that it performs better on multi-core computers.
0 Comments
Posted in Arrangement, Art, Business

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 'removeDuplicates' call. Second, I decided that, to keep the goal of this method succinct, I wasn't going to support projection unless there was a compelling reason.

So, now, I can just call it like this:


val assetsMap = assets groupby { asset => asset.getName.substring(0, asset.getName.indexOf("_file")) }


Doesn't that look a lot nicer?
0 Comments
Posted in Arrangement, Art, Business

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 I am not using the classextension.EasyMock class. However, if I use that, then Scala complains that "expect" isn't a member of classextension.EasyMock.

So, here is the solution that I came up with:

I use import shortnames to include both EasyMock classes:


import org.easymock.{EasyMock=>EM};
import org.easymock.classextension.{EasyMock=>CEM};


And then in the code, I do the following:


EM expect(myClass myMethod myParam) andReturn myReturn
CEM replay myClass
...unit test stuff...
CEM verify myClass
CEM reset myClass


And it works great!

Has anyone else found a good solution for this?

The next problem that I have run into is method matching when one of the values passed in is an array projection.

Observe the following code:


myClass.myMethod(param1, param2, str split "_" drop 1)


The last parameter returns type Array.Projection. If I have the exact same code in my expect, then this particular parameter is still listed as being unequal.

The only way that I have found so far around this is:


EM expect(myClass.myMethod(EM anyObject[type1], EM anyObject[type2], EM anyObject[type3])) andReturn myReturn


Not a big fan of this strategy, but it works. Any ideas?

25 September 2009

0 Comments
Posted in Arrangement, Art, Business

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,B,C](keyresolver:C=>A, valueresolver:A=>List[B], list:List[C]) = {
Map[A,List[B]](
(list map { el => keyresolver(el) } removeDuplicates)
map { key => (key, valueresolver(key)) }
: _*
)
}


So, this is thread-safe. What it does is takes the list of C and applies a function to find the group by properties. Then, iterating through those, it applies a function with each key to get a list of values that match.

"But what about 'put'?" you say. The thing is, once you have immutability built in, you will find that the need to be able to modify a map doesn't come up as often. This example is prime--because Maps and Lists are immutable in Scala, we are nearly there before we know it regarding thread safety.

04 August 2009

0 Comments
Posted in Arrangement, Art, Business

This is a test


viva

15 July 2009

0 Comments
Posted in Arrangement, Art, Business

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 all reason.
0 Comments
Posted in Arrangement, Art, Business

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 surprise.

Many of the software requirements bore the same trend as the overall relationship as Mike often deferred to my expertise by saying "if it is possible, please to XYZ this way, otherwise...". If I'm not mistaken, I believe that we were always able to go with his primary request.

Novel Creations

One such example of an "if-it's-possible" requirement was an HTML slider. Mike's description was that one could slider an indicator horizontally across the screen. When the indicator was on the far left, a certain input would have the value of -100, and when on the far right, the value of 100.

Well, such a slider does not exist natively in the HTML tags. Further, a back-end technology was not an option nor was a rich client like Flash. Of course, never to be overcome by a challenge, a few hours of work revealed that one can use a "span" tags and a healthy amount of JavaScript to do the trick.

File formats

Of course, not everything in the web world comes up roses. I'm not sure, but I believe that Mike spent a few hours learning about creating web-ready images. While not the article to talk about it, digitial image resolution and web-friendly image formats were a mystery to Mike, but, impressively, he spent the time to figure it out in order to make sure that the image was crisp and attractive on his site.

02 July 2009

0 Comments
Posted in Arrangement, Art, Business

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 thought dawned on me:

What if we assigned a prime number to each value and created composite numbers for each product's unique set of values, which we could then extract back out using modular arithmetic?

I'm sure that the thought occurred to you right away. Of course, it took me a couple of weeks to see it.

So here is what I did in the end:

Each page number would be assigned a unique prime number. So "Page 1" would be assigned the number "2", "Page 2" would be assigned "3", "Page 3" would be assigned "5", etc. The set of pages that an individual product should appear on can then be expressed as a set of those prime numbers:

Product A's 'page_numbers'-> {17, 3, 93, 41}

"But wait," you say. "Google Base won't let us specify a list of integers as a value in the data feed!" You speak well. What we will do now is multiple those prime numbers together to get a composite number, which will become the value for the "page_numbers" attribute.

17*3*93*41 = 194463

Product A's 'page_numbers' -> 194463

Now, if you stretch back to high school math, you'll remember that ever composite number is a product of primes (remember what they called "prime factorization"). This concept ensures that there is only one prime factorization per integer and thus, will not be divisible by any other primes than those that make up its prime factorization.

This is important because now I can say with confidence that if product xyz's 'page_numbers' attribute is divisible by 43, then it must belong on the page that is assigned to the prime number '43'.

Modular arithmetic is perfect for such a calculation. The question "Does x mod y equal 0?" is the same as asking "Does y divide evenly into x?"

So, the math works. Now, will Google Base support it?

It turns out that their Ranking Language does support modular arithmetic and can act as a secondary filtering mechanism after its Query Language. Yes!

Upon reading the documentation, I use the following Ranking Language code:

orderby=[x = page_number(int) : if ( x % 43 == 0 ) then x else fail()]

Drum roll.....

............

...It fails. Expected an 'int' for the '%' operator, but found 'page_number(int)'.

What? **Heart sinks**

Well, I won't tell you about the resulting melancholy that weighed on my stricken heart for the next 48 hours while I stewed over what had gone wrong.

Instead, I'll just tell you that I found out that since each attribute is able to have multiple values (just not necessarily through a data feed), I needed to apply an aggregating function on the value before applying the mod.

So, then I tried:

orderby=[x = page_number(int) : if ( max(x) % 43 ) then max(x) else fail()]

And it worked!

It's always fun running into problems like that because there is an interesting solution hanging right around the corner.
0 Comments
Posted in Arrangement, Art, Business

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.
0 Comments
Posted in Arrangement, Art, Business

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.
0 Comments
Posted in Arrangement, Art, Business

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:




29 May 2009

0 Comments
Posted in Arrangement, Art, Business

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 way wherein they should bwalk, and give rain upon thy land, which thou hast given to thy people for an inheritance.

This is King Solomon praying for his people. Can you hear the compassion in his voice, crying for the Lord to help his people? I see you, my son, and not King Solomon praying when I read that scripture. I see you yearning for the well-being of those around you and pleading that God will aid them.

I love this scripture because of its relationship to Malachi 3:10 where the Lord says that he will "pour" out blessings upon those who follow His commandments, maybe just like the way rain pours from the heavens.

You have opportunities everyday to pray for others, that they might be the beneficiary of a pouring of blessings.

Remember your name, and remember that the Lord really, really hears you, just like your dad hears you, but all the better.

18 April 2009

0 Comments
Posted in Arrangement, Art, Business

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 she is extremely happy to see anyone that she knows. If she feels lonely, she resorts to singing or swaying or laughing to get the attention of an adult instead of to crying. If she gets tired, she will often just fall asleep in your arms instead of getting really fussy.

In fact, at the beginning of each calendar year, all my children get father's blessings. In her blessing, I felt distinctly impressed to tell her (even though she is only 9 months) that a gift of hers was to always find happiness in life, and that her role on our family would be to cheer up her family members when they were down.

That night, she cried. She cried and cried and cried and cried. She squirmed and twisted her body, and she could not be comforted. We rocked her and consoled her for and hour with no progress. We took her temperature--it was 103 degrees.

We worried.

It was 12:30 at night, and I was in my pajamas, but I handed Seren off to Kristi, and I began to put on my suit. I excused myself from the room and went to the living room to offer a fervent prayer to my Heavenly Father.

I would like to say that the prayer was an incredible outpouring of emotion, but it wasn't. I had faith that my Heavenly Father loved Seren and had ordained me to an office of His Priesthood, and that seemed sufficient this time. My heart was broken and my spirit was contrite as I asked my Heavenly Father to direct the words of the blessing that I would give to Seren.

Those who have the priesthood for giving blessings are called Elders. In James 5:14, it says:

14 Is any sick among you? let him call for the elders of the church; and let them pray over him, anointing him with oil in the name of the Lord:

15 And the prayer of faith shall save the sick, and the Lord shall raise him up; and if he have committed sins, they shall be forgiven him.

Believing this principle to be true, I laid my hands softly on Seren's head, anointed her with oil, as the scriptures instruct, and blessed her. I blessed her that her fever would break so that it wouldn't do any permantent damage to her body and I blessed her that she would find sleep.

Within a few minutes of the blessing, I felt her forehead, and it was cool again. A feeling of gratitude for my Heavenly Father entered my heart, and I said a silent "thank you" to Him as I contined to rock Seren.

She was still fussy for another 20 minutes, and so I walked into the living room, again, still rocking her, and I whispered into her ear another blessing in the name of Jesus Christ, commanding her body to sleep.

I took my little Seren straight into her crib, laid her down. She was asleep before her head hit the pillow.

We haven't had a night like that with her since. She vomited once the following morning, continued to have the diarrhea, but that intense fever never returned and she has easily gone to sleep each night.

Now, I don't mean to imply that anyone should hang their testimony on this story. I simply wished to thank my Heavenly Father for His love for His little ones. I know He loves them, and I am privileged to have the spiritual and temporal means to take good care of the ones that I have.