Sunday, August 19, 2012

No Sound After Suspend/Sleep Mode Mac OS X

The Problem

So this problem has been a thorn in my side for months now. It's really annoying because the only way you can get your sound back is with a full reboot. No one was to be preempted with a reluctant reboot every time they put their computer to sleep, so enough already! It took me a while to find possible solutions to this problem but after some persistent googling, I was able to track down a helpful post on the Apple Support forum. There are a few recommended solutions I was able to glean from the thread.

 1. Disk Utility's Repair Disk Permissions feature

This post explains this feature. I've included a screenshot below of the app (Applications/Utilities/Disk Utility.app).





























I don't know if this really helps, but I did it anyway and it definitely doesn't hurt. I'd recommend this one as some permissions are bound to be out of synch and who knows what side affects they could be causing.

2. Plug Headphones In/Out

Sounds like a hack, right? Well apparently this has worked for some people but it's certainly not a long term solution. Put in your favorite earbuds, take them back out, and be blessed with audio. I've yet to try this technique as I find success via the command line but feel free to cross your fingers and give it a shot.

3. Uninstall Parallels Desktop

"Not I said the cat". 

I don't know how feasible this one is but someone managed to link the problem to Parallels Desktop. If you've ever saw one of my screencasts you'll know that's virtualization software for Windows and other operating systems. I'm not planning to uninstall software I payed 100 bucks for, but if you want your audio back and have some other VM you can run then knock yourself out.

4. Restore your OS

This isn't particular good but assuming you have a TimeMachine backup or fully backed up image on hand then I guess it's not too bad. If you've got the time to spare then a clean restore could turn out to pay dividends. I don't think I'll be doing this until my Lion is old, immobile, and children have graduated to the big leagues capable of hunting on their own and tracking down their own prey. I do have backups in place but I'd rather not have to make use of them anytime soon.

5. Reset Permissions on coreaudiod.plist

These are permissions related to the coreaudio daemon process that's spawned by the OS. This can be accomplished with a simple command line.

sudo chown root:wheel /System/Library/LaunchDaemons/com.apple.audio.coreaudiod.plist

I tried navigating to that path in my shell but it didn't resolve. Maybe it's under a different alias in later versions of Mac. If you're able to locate it then good luck.

6. Set KeepAlive to "True" in coreaudiod.plist

This involves editing the contents of "com.apple.audio.coreaudiod.plist"

<key>KeepAlive</key>
       <true/>

7. Kill/Restart the coreaudio daemon

This one worked for me!!

Simply execute this command from your shell.

sudo kill -9 `ps ax|grep 'coreaudio[a-z]' |awk '{print $1}'`

I've created a script with the contents of the command and placed it in "/usr/local/bin". Make sure that location is included in your path. After creating the file, be sure to make it executable with

chmod 755 <whatever_you_named_the_script>

What you may notice after executing this command is that the OS was queueing up all your audio  requests in the background. You'll here it flushing it's internal buffer and your speakers getting flooded with audio from the moment your computer was summoned from sleep mode up until the time you restarted the coreaudio daemon. So if you pressed the volume button 12 times, you'll here 12 little "pops" synchronously pumped out of your speakers. It's like cleansing your system after years of build up. It all just comes out at once. Hehe :)

The End

I'll admit that none of the solutions are particularly "clean", but they're better than having to use preferences to increase your volume and being robbed of the spoils of devices like Apple Remote. Apple is undoubtedly long overdue for a patch as this seems to have been a problem for at least over a year now. I hope one of these tips works for you, and as always, feel free to refer directly to the forums for additional help, additional clarity, or any solution I may have failed to extract and make known.

Sunday, August 12, 2012

An Interesting Observation About the Google Search Algorithm

In my never ending quest to make my vim editor as perfect as it can be, I was doing a quick query to determine if exuberant ctags had the ability to interrogate tag files in distinct locations, or if it needed all tags to be present in one huge gargantuan tag file.

So I did a quick search in Google:

"aggregate tags vim exuberant ctags"

I found a pretty interesting article with tons of helpful information in it. After scanning though it, I think I was convinced I had the answer I was looking for. But then for some strange reason, curiosity made an impromptu appearance and asked a sequence of two very simple questions...

"Does this document even contain the word aggregate? Why is it ranked so high relative to this query?"

So I thought about it and did a quick search in Chrome (Command + F on Mac), and to my surprise, the word aggregate was nowhere to be found. Want to guess what I found instead? Words that make up the definition of the word aggregate, or are similar to words that do so. Words like "one", "big", "global",  "all", etc.

Based on these results, I could only conclude the following...

Google's search algorithm will not only literally search for a word contained within a document, it will also search for words contained within the definition of that word.

Not that I care what the realists of the world think, but I'm sure (as many may point out when coming across this post) this information is publicly available somewhere and probably no secret to the search experts of the world. That being said, I don't spend a ton of time analyzing the idiosyncrasies of the Google Search algorithm. So yes, I did ignorantly go out of my way to write this post irrespective of the aforementioned givens. I'll bet you there are thousands more that aren't even aware that this feature is potentially embedded within Google Search, and at least they'll get a quick glimpse of it all upon hitting this blog. Naturally deriving something is something I've always viewed as an accomplishment; even if someone else has already done so. It always feels good, and it feels even better when you can blog about it.

At any rate, neat stuff by Google, and pretty cool on top of that.

Tuesday, August 7, 2012

You Lost Your Wallet - Flying with No ID, Credit Cards, etc.


If You've Lost Your Wallet, File a Police Report


I took for granted how much a police report could help you in a situation like this. It's one of the
initial questions the Airtran employee asked me. She was so serious about the police report that she
insisted I go file one with the local authorities at the airport. Lucky for me, they were unavailable
due to how early in the morning it was. I walked back through the double doors with a concerned look on my face; the look of a lost child. Once I heard her summon me back to the kiosk, I knew I'd caught a break. She made several phone calls to headquarters to ensure I could still fly under the circumstances. After receiving an ear full about why I should have filed the police report, her being intermittently put on hold for extended periods of time, and me putting on my best impression of a stranded adolescent, I was good to go. Sandra, the Airtran employee that so diligently worked to get me on my flight back to Washington, was also kind enough to let me know that I'd go through additional screening once I encroached the security checkpoint. If you've flown before, you know that getting past the front counter is only half the battle. Three letters...TSA. I got the same question once I made it to the security checkpoint. "Do you have a police report sir?". It was then that I realized how much of a burden it would have removed from the entire process.

So why didn't I file that report right off the bat? Don't ask....

The Social Security Number is a Lie


If you're trying to fly with no identification, at no point should anyone request your social security number (not even the last for digits). Not TSA, not whomever you booked your flight with, not anyone. I was eager to give out my number in some form or fashion. Whatever it took for me to prove that I'm Antwan Wimberly. I just knew someone would ask me for it. And to my surprise, no one did. They've got other means of verifying your identity. Knowing this up front can alleviate that much more pressure.

Documents with your Name on them will Help


In addition to numerous questions that you'll be hit with by TSA, believe it or not, they actually want to see you fly. They'll try and work with you. The officer asked me did I have any documents with my name on them. I immediately replied with "no", assuming she meant something issued by a public authority. But TSA uses the term documents loosely. It can be anything. A receipt, an old boarding pass, whatever you can show them. So don't be shy when it comes to filtering through you backpack for anything you can find with your name on it. The more, the better.

Be Prepared for Additional Questions and Screening by TSA


Did you think it'd be that easy? Haha..not quite. You'll be hit with numerous questions before they allow you to proceed to your final destination. I won't cover the questions here due to security reasons, but you'll be able to trivially answer them if you are indeed who you say you are. You'll have to fill out a form stating that you're aware that providing false information is a crime. It wont' take you long at all. Once you're done with the form and answering any questions TSA has for you, they'll send you through for additional screening along with the normal protocol (body scan,  putting your items on the conveyer belt, etc.). Don't be frightened by the term additional screening. All it means is that they'll physically pat you down as a precautionary measure. They'll also take samples from anything you're carrying (shoes, backpack, etc.) and run it through a database. It's not that bad though. You should be on your way in no time.

Get There Early


You'll want to arrive to the airport at least 2 hours early. You'll need the extra buffer to account for the additional questioning and screening that you'll be exposed to. So don't get careless and show up an hour before your flight thinking that everything is going to go smoothly. You should expect a hiccup or two and have enough time to recover.

Be Polite


The last thing you want to do is piss someone off. They want to help you, but giving a reason not to do so could result in you missing your flight. These people will have to make phone go and take additional measures to ensure that you can fly. They're working a lot harder than they're used to, and all they want is to be appreciated. If you feel like they're giving you a hard time or your being bombarded with tons of questions, don't panic. Keep your cool and push your ego to the side. They can't make it look like flying without an id isn't frowned upon or won't be met with resistance. How much they sale this depends on their personality. Sandra was pretty tough to start out with, but she eventually loosened up and hopped on the phone for me. As I mentioned earlier, she was even convinced that I should show them a police report. Just know that they'll come around. Express concern and gratitude. I'm not saying that you have to suck up or kiss anyone's ass, but be careful about pushing back once the storm of interrogatives commences.

A Checked Bag will Mike Life a Little Harder


You want to know what made Sandra as upset as she was? The fact that I was trying to check a bag. I always fly with luggage, because I want to be comfortable when I travel and I hate wearing the same clothes over and over. That's just me. But this pesky habit almost lead to my demise. It made things more nuanced than they had to be. That extra wrinkle is what initiated the phone calls and all the waiting up front. She kept saying over and over that the fact that I had a checked bag was what she had a problem with. It wasn't aimed directly at me but was more so her thinking out loud. She knew I'd introduced more complexity and that she'd have to be the one to sort it all out. I'm not saying that you should blindly leave your luggage behind, but if you don't have any, then it'll probably expedite the process.

The Myths About Costco Cards are True


I was reading an article that mentioned that TSA would accept Costco cards as valid authentication since it has your photo ID on it. Costco isn't as trusted an authority as the DMV, but it can help you get through TSA. I heard the officer that was helping me ask another guy if he had a Costco card. I may have also even heard SAM's Club (I definitely heard it if they embed your photo onto your card like Costco does).

Cards in General Help as Well


This may be a no brainer, but Debit cards, credit cards, and others can also help you. TSA inquired about one and so did Sandra of Airtran. My only problem was that I'd lost EVERYTHING in one go. So no social security card, debit card, credit cards, etc. If you happen to have these things then you'll be much better off.

Be Careful About Curbside Checkin


The primary risk here is that well...you may get things out of synch. I don't exactly know how curbside checkin works, but from what I can tell you can get your boarding pass online. In theory you could go directly to TSA/security checkpoint. Say for instance you check your bags curbside but get rejected at the security checkpoint. All in all I think you'll be fine. But Delta did send my bags to Jacksonville when I clearly changed my flight to go to Atlanta. I was initially going there until I found out my grandmother was headed into the city. So I figured why not change? Next thing you know I've taken 2 trips to Duval over a span of like 5 hours. It was pretty hectic. This could no doubt be advantageous to you as you'll have one less dependency being able to bypass the guys at the front counter but at the same time you could be asking for it.

Courtesy of http://www.jaunted.com/

















That's all folks...

Synching Your Vim Configuration Files to Dropbox on Change OS X

If you're a user of Vim of any kind of experience level, you've probably come across vimrc and gvimrc. Your experience with vim is heavily ties to these 2 files, hence the need to have them backed up in a safe place in case anything catastrophic manages to occur on your system. Specifically I've implemented a tiny but useful script to push my most precious vim assets to my Dropbox account in the ever so beloved and mysterious....."cloud".

I haven't tested this on Windows as I don't make many changes to my vim configuration on my win32 computer. I find it more effective to manually synch those changes not to mention I don't use vim nearly as much on win32 as I do on darwin (OS X).

That was kind of a joke btw. If you look closely enough you'll see why this won't work on win32 out of the box without a bit of magic or aliasing. Maybe powershell exposes an alias for the command I'm using in the same way that it maps "ls" to "dir", but anyway.

So here goes...

" Synch vim files to dropbox on change
:augroup VimFiles

autocmd! VimFiles BufWritePost ~/.vimrc :!cp ~/.vimrc $HOME/Dropbox/vimrc
autocmd! VimFiles BufWritePost ~/.vimrc.local :!cp ~/.vimrc $HOME/Dropbox/vimrclocal
I've place the snippet above in my .vimrc.local config. What's vimrc.local? Well, there are some "vim bootstraps" out on the market and Steve Francia's is a pretty good option. His vim bootraps process will source the vimrc.local dotfile from vimrc. This allows you to override or customize the bootstappers default configuration without directly changing its contents (vimrc). This is important because the bootstrapper takes the form of a git repo which allows you to pull in updates. You can imagine your changes conflicting with the author's and all other kinds of hairy predicaments version control systems lend themselves susceptible to.

You'll also notice the usage of the augroup vim command. This is just a neat way to organize your vent subscriptions. Vim can keep track of these and manage them as a whole. For example, you could delete all event subscriptions under a particular group if you wanted to. Just handy namespacing. These groups are optional so you should use them at your convenience.

If the :! syntax is news to you then it's just a way to spin up your local shell (cmd, bash, zsh, etc.) and execute an arbitrary command. The same as backticks/sh/system in Ruby, Exec task in msbuild, etc.

As far as gvimrc goes, I don't have much use for one since I use MacVim. I'm perfectly content with my vim GUI and to be honest my system doesn't even contain a gvimrc dotfile. The two files I care most about are covered.

Now I know this script is way simple, it's only 2 lines for crying out loud, but exactly what is it doing?
The vim runtime publishes events that developers can conveniently subscribe to. These events range anywhere from when vim first loads to when a file changes on your file system outside of the context of vim. There are a multitude of these events wich a varying degree of scope and granularity. To see a full list, just invoke the vim help with "au" as an argument.
:h au
au is an ancillary/alias for autocmd so of course either will suffice.

The event I'm subscribing to is the BufWritePost event. And quote...

"after writing the whole buffer to a file"

Most all the events take a regex pattern as input so you can handles multiple files, check based on extension, etc. For example, the vim runtime doesn't know about well known file names idiomatic to Ruby such as

Rakefile - searched for and loaded by rake by default
config.ru - rack configuration for rack based apps like Rails and sinatra
Capfile - capistrano configuration for deployments

etc...

So I took time out to tell vim that it should treat these files as Ruby code so I could get syntax highlighting. Otherwise they just read as plain text and that's quite the inconvenience for today's developer. We Mac users love our aesthetically pleasing things so the more color the better *wink*.
:augroup UnrecognizedRubyFiles

" Set file type to Ruby for unrecognized ruby files
autocmd! UnrecognizedRubyFiles BufReadPost,FileReadPost Gemfile,Rakefile,config.ru,Thorfile,Capfile,Vagrantfile set ft=ruby

The BufReadPost event is published once vim finishes reading your file. Not a bad time to tell it what we're working with. You could probably even get away with subscribing to an event published earlier in the runtime but this works for me.

Well, that's pretty much it folks. We took a look at subscribing to vim event via the au/autocmd command. Again, this command is nice because it can accept patterns as arguments. We can also namespace our event subscriptions with the augroup command. You'll see this technique employed by many plugin developers. Take rails.vim for example.

























Thanks guys...enjoy.

Saturday, April 7, 2012

Using Set Theory, Ruby, and Map/Reduce to Implement Portable Class Libraries Feature in VS 11


How it all started...

I crunk up my Mac and began perusing the web.

Checked out a pretty nice article from my man Thomas P on joinads.

Man was I craving some F#.

I had to show my usual love...you know..Tweet dat and +1 dat!!

Once I got over to the Googleplex I began to scroll my feed.

Came across my man Hanselman and decided to check out the hype with a new feature coming in VS 11.

It's this multi targeting guy that allows you to implement an assembly that can be deployed across multiple Microsoft Platforms (Xbox, Silverlight, etc.)

So what does this mean?

Well...let's take Silverlight for example.

It's a subset of the CLR and .NET Framework so certain libraries available in say...an MVC app wouldn't be available in Silverlight.

So VS would proceed to remove those libraries that can't be used across both platforms.

So you're basically at the mercy of the platform that can support the least amount of libraries. You're only as strong as your weakest link, right?

Lets take an example

say we have the following platforms

Silverlight = [a, b, c]
MVC = [a, b, c, d]
Xbox = [a, b, c, d, e]

What's the expected output?

Easy...everything in Silverlight!!

[a, b, c]

It can only support libraries a, b, and c.

MVC and Xbox can also support d and e, but they're SOL here b/c their little sister can't hold her liquor.

Sorry Orteguitas...life isn't fair...

What would I expect in a comparison of MVC and Xbox?

[a, b, c, d]...

Ok..I think you get it by now, right?

I've said all that to say this...what if you had to implement this logic?

What would you do?

How many lines of code do you think it would take?

These are questions that I'm always pondering.

It's like..."Hey Antwan, what if LCD tv'S didn't exist and you had to be
the guy to implement them?"

A chin rub and shot of Gatorade later and I'm off to the races..

Haha..sike...I eventually embrace the reality of it all and reluctantly tell my brain that there's noway I'd have been able to make electricity, the first airplane or even the world's first LCD tv...ha..I wish.

But programming is something that I've managed to become pretty good at..and that
my friends...means there's hope.

So what if I had to implement this feature in VS?

Well...I spent a few minutes thinking it over and I came right back to the very foundations of our profession and the bit munching concoction of semiconductors, circuitry, and hardware that we hold so dear to our hearts...math!!

Wait...I'm having a flash back...discrete mathematics from college...oh no..the pain..the horror..is that a turing machine? I'm having a mental aneurysm...

Ok...back to the show..

It's simple set theory if you think about it.

Take the intersection of all the sets..and well...you're done.

We've already implemented this very scenario...at least at a high level..
now lets get into some code..

I couldn't decided whether I'd do this guy in Ruby, C#, or F#...cuz as we all know...loops are just my thing. Enumerable - Enumerable - Enumerable!!

All 3 languages provide pretty sweet implementations of Enumerable...but I've yet to publish any content in Ruby...so here goes.

We'll start out with some tests and then work our way to the final implementation.

Off the top of my head...I know that LINQ provides a native implementation of intersect, but not sure about Ruby...a quick Google aaaaaaand...

Drum role please...

Bam!!

Found it...

Here's the syntax

ary & other_ary → new_ary

we're golden.

Before we get started, I just want to take a brief moment to highlight our plan of attack.

We're going to use aggregation here..you know..take 2 values, generate a result, and push that result through the pipeline as new state to be combined with the next element of our sequence (acculamators).

This concept if the basis of Sum, Product, etc...

so something like

[ 1, 2, 3, 4, 5 ].inject(:+) # => 15

in Ruby..

What inject is doing here is enumerating the sequence, taking 2 values at a time,
adding them together, and returning a new value to be added.

So it goes

Iteration    Seed    CurrentValue

1              1          2
2 3          3
3 6          4
4 10         5

done...break loop

15 is our final answer..

Make sense?

The cool thing about inject is that you can abstractly pass any "function" to it and have that function applied to all elements in your sequence.

We could multiply all numbers if we wanted

[1,3,5,7].inject(:*) # => 105

What you're seeing is a shorthand syntax to inject.

In it's more explicitly form you'd do something like

[1,3,5,7].inject(0) {|sum, element| sum+element}         # => 16
[1,3,5,7].inject(1) {|product, element| product*element} # => 105

passing in a block.

We're using the more succinct shorthand syntax...gotta love that MRI sugar, yea?

I've covered this kinda thing in F# in a previous blog post..so nothing new..jus thought I'd share that again since it's such an essential and rudimentary concept.

I puts quotes around the word "function" up above because this is merely functional programming. I've managed to pick up a lot about in through F#, C# (LINQ), and a variety of other sources over the years. It'd only behoove you as a developer to pick up a functional language at some point during your career. So much of the very code that you write on a daily basis can be tied back to functional programming; and that implicit marriage feels even better once one gets to that point that he/she can explicitly detect it. Everything just starts to make sense.

Ok, I digress.

Didn't I say we'd get into some code? Oh yea...right..just didn't wanna leave anyone behind. I'm sure
the average dev that comes across this post will have jumped to the source by the time they got past
paragraph 2..haha.

If you'd like to see more of those cool Ruby samples I posted above, just checkout the Pickaxe Book. All those samples come straight from that book verbatim. A great read for any novice Ruby dev such as myself and even for the most experienced of Rubyists.

Ok so lets get started.

I'll start with a class to track our library instances and a stub implementation to detect compatibility.

platform.rb

class Platform

    attr_reader :name, :libraries

    def initialize(name, libraries)
        @name = name
        @libraries = libraries
    end
end

portable_class_lib.rb

require './platform'

module PortablePlatform
    def PortablePlatform.get_compatible_libraries(platforms)
    end
end
We'll keep this guy short and sweet...just a few test cases.

portable_platform_lib_tests.rb

require './portable_class_lib'
require 'test/unit'

class TestPortableClassLib < Test::Unit::TestCase
    def test_3_platforms_with_3_common_libraries
        xbox_libraries = %w{ System System.Web.Mvc System.ServiceModel System.Runtime.Serialization }
        xbox = Platform.new('Xbox', xbox_libraries)    

        silverlight_libraries = %w{ System System.ServiceModel System.Web.Mvc }
        silverlight = Platform.new('Silverlight', silverlight_libraries)

        mvc_libraries = %w{ System System.Web.Mvc Mvc.Core System.ServiceModel }
        mvc = Platform.new('ASP.NET MVC', mvc_libraries)

        platforms = [xbox, silverlight, mvc]

        compatible_libraries = PortablePlatform::get_compatible_libraries(platforms)

        expected_output = %w{ System System.ServiceModel System.Web.Mvc }

        assert_equal(expected_output, compatible_libraries)
    end

    def test_3_platforms_with_2_common_libraries
        xbox_libraries = %w{ System System.Web.Mvc System.ServiceModel System.Runtime.Serialization }
        xbox = Platform.new('Xbox', xbox_libraries)    

        silverlight_libraries = %w{ System System.ServiceModel }
        silverlight = Platform.new('Silverlight', silverlight_libraries)

        mvc_libraries = %w{ System System.Web.Mvc Mvc.Core System.ServiceModel }
        mvc = Platform.new('ASP.NET MVC', mvc_libraries)

        platforms = [xbox, silverlight, mvc]

        compatible_libraries = PortablePlatform::get_compatible_libraries(platforms)

        expected_output = %w{ System System.ServiceModel }

        assert_equal(expected_output, compatible_libraries)
    end

    def test_3_platforms_with_no_common_libraries
        xbox_libraries = %w{ System.Runtime.Serialization }
        xbox = Platform.new('Xbox', xbox_libraries)    

        silverlight_libraries = %w{ System }
        silverlight = Platform.new('Silverlight', silverlight_libraries)

        mvc_libraries = %w{ System.ServiceModel }
        mvc = Platform.new('ASP.NET MVC', mvc_libraries)

        platforms = [xbox, silverlight, mvc]

        compatible_libraries = PortablePlatform::get_compatible_libraries(platforms)

        assert_empty(compatible_libraries)
    end
end






And the test results...

Run options: 

# Running tests:

FFF

Finished tests in 0.001343s, 2233.8049 tests/s, 2233.8049 assertions/s.

  1) Failure:
test_3_platforms_with_2_common_libraries(TestPortableClassLib) [-:40]:
<["System", "System.ServiceModel"]> expected but was
<nil>.

  2) Failure:
test_3_platforms_with_3_common_libraries(TestPortableClassLib) [-:21]:
<["System", "System.ServiceModel", "System.Web.Mvc"]> expected but was
<nil>.

  3) Failure:
test_3_platforms_with_no_common_libraries(TestPortableClassLib) [-:57]:
Expected nil (NilClass) to respond to #empty?.

3 tests, 3 assertions, 3 failures, 0 errors, 0 skips

Failures just as we suspected. Lets plug in an implementation, shall we?

require './platform'

module PortablePlatform
    def PortablePlatform.get_compatible_libraries(platforms)
        platform_names = []

        platform_libraries = platforms.collect do |p|
            platform_names << p.name # go ahead and cache the names so we don't loop twice
            p.libraries # retrieve all libraries
        end

        compatible_libraries = platform_libraries.inject(:&)
        libraries_sorted_by_name = compatible_libraries.sort

        puts "Platforms...\n\n"

        platform_names.sort.each { |name| puts name }

        unless compatible_libraries.any?
            puts "\nHave no common libraries."
            puts "\nUnfortunately you cannot create a portable class library targeting these platforms."

            return libraries_sorted_by_name
        end

        puts "\nHave the following libraries in common...\n\n"

        libraries_sorted_by_name.each { |name| puts name }

        puts "\nTo make a portable class library, you'll need to reference these assemblies."

        libraries_sorted_by_name
    end
end

And now after re-running the tests we get...

Run options: 

# Running tests:

Platforms...

ASP.NET MVC
Silverlight
Xbox

Have the following libraries in common...

System
System.ServiceModel

To make a portable class library, you'll need to reference these assemblies.
.Platforms...

ASP.NET MVC
Silverlight
Xbox

Have the following libraries in common...

System
System.ServiceModel
System.Web.Mvc

To make a portable class library, you'll need to reference these assemblies.
.Platforms...

ASP.NET MVC
Silverlight
Xbox

Have no common libraries.

Unfortunately you cannot create a portable class library targeting these platforms.
.

Finished tests in 0.000913s, 3285.8708 tests/s, 3285.8708 assertions/s.

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips

We're golden!!

So how'd it work? Well, lets take our core algorithm (minus all the output) and our first test case.

def PortablePlatform.get_compatible_libraries(platforms)
    platform_names = []

    platform_libraries = platforms.collect do |p|
        platform_names << p.name # go ahead and cache the names so we don't loop twice
        p.libraries # retrieve all libraries
    end

    compatible_libraries = platform_libraries.inject(:&)
    libraries_sorted_by_name = compatible_libraries.sort
end

def test_3_platforms_with_3_common_libraries
    xbox_libraries = %w{ System System.Web.Mvc System.ServiceModel System.Runtime.Serialization }
    xbox = Platform.new('Xbox', xbox_libraries)    

    silverlight_libraries = %w{ System System.ServiceModel System.Web.Mvc }
    silverlight = Platform.new('Silverlight', silverlight_libraries)

    mvc_libraries = %w{ System System.Web.Mvc Mvc.Core System.ServiceModel }
    mvc = Platform.new('ASP.NET MVC', mvc_libraries)

    platforms = [xbox, silverlight, mvc]

    compatible_libraries = PortablePlatform::get_compatible_libraries(platforms)

    expected_output = %w{ System System.ServiceModel System.Web.Mvc }

    assert_equal(expected_output, compatible_libraries)
end

We started out by storing the names of all platforms in an array in addition to retrieving the actual libraries for each platform. As you may already know, collect is the equivalent of map from a functional perspective.

Now for the set manipulation. Remember when we looked up that intersect syntax a while back?

ary & other_ary → new_ary

Well...we used it here for each set of libraries.

First we intersected the set of xbox libraries with itself, which of course just returned the original set of xbox libraries.

[ System, System.Web.Mvc, System.ServiceModel, System.Runtime.Serialization ]

Then that we took everything the xbox libraries and the silverlight libraries had in common. That yielded

[ System, System.Web.Mvc, System.ServiceModel ]


Finally we were left with the MVC libraries. That intersection yielded...well...the same thing

[ System, System.Web.Mvc, System.ServiceModel ]

Done!!

There's that inject function from earlier when we summed up all our numbers. That aggregate/bulk function that I'm always raving about. We applied it to each set of libraries using the intersect operator as our function. It should be obvious by now that when you use inject you'll be dealing with a generic scenario...

let a = a set of values of type b
let s the seed = the first element of a ,or a value of type b
let f = a function that accepts 2 elements of type b and returns a new element of type b
let o the output = anew element of type b

So basically you start with a sequence values and end up with 1. Sum takes a sequence of numbers and returns and individual number as the result. Just like our algorithm takes a sequence of arrays and returns and individual array as a result. This concept is known as reduce in the functional world. We actually used both map and reduce here...didn't we? Sound familiar? We mapped the platforms to a sequence of arrays, and reduced that sequence of arrays down into an individual one.

I've covered map/reduce before but it's shown up yet again. What a coincidence...

I'd like to take this time to conclude my post...

Shout out to Alicia G in the Show Me...

And I can't leave without giving you a glimpse of my exotic and elaborate lady friend that allowed me to pull off such an amazing feat. Her beauty and elegance are second to none. A hue and texture so rare. Her movements equivalent to the speed and grace of a Cheetah...my dear lady..where would I be without you?

Ladies and gentlemen....Vim

Mac Vim


Duces...













oink oink!!
http://www.imdb.com/title/tt0845746/

Friday, September 23, 2011

Using SOLID to Implement Fizz Buzz


So I was having lunch with @jeffreypalermo, @dannydouglass, and my other teammates, and they were chatting about interview processes/techniques. Jeffrey was in town for our annual HeadSpring training and mentioned the infamous fizz buzz problem and I couldn’t but think about how I’d implement it...cleanly!! One given is that whomever reads it does not want to see a proliferation of if statements. But they have to get written at some point, right? So thinking about modern practices and design principles, I thought to myself…why not let SOLID guide me here? And that’s exactly what I did.
















So there are several components at play here; the FizzBuzzCommandProcessor and the FizzBuzzCommandRetriever. I took the processor technique from Jeffrey when he mentioned how they implemented the rules engine. I thought it was pretty cool and would be fun to implement something similar on my own. And with the advent of CQRS, I’m just really hooked on commands in general. They promote such strong usage of Single Responsibility Principle (SRP). The command processor takes input from the command retriever, and matches up each command with the value that it can handle. That’s it.
using System.Collections.Generic;

namespace ADubb.FizzBuzz
{
    public interface IFizzBuzzCommandProcessor
    {
        void Process(IEnumerable<int> numbers);
        void Process(int number);
    }
}
using System.Collections.Generic;
using System.Linq;

namespace ADubb.FizzBuzz
{
    public class FizzBuzzCommandProcessor : IFizzBuzzCommandProcessor
    {
        static readonly IEnumerable<IFizzBuzzHandler> HandlerCache;
        static readonly FizzBuzzCommandRetriever CommandRetriever;

        static FizzBuzzCommandProcessor()
        {
            CommandRetriever = new FizzBuzzCommandRetriever();
            HandlerCache = CommandRetriever.GetHandlers().ToList();
        }

        public void Process(IEnumerable<int> numbers)
        {
            numbers.ToList().ForEach(Process);
        }

        public void Process(int number)
        {
            var handlers = HandlerCache.Where(h => h.CanHandle(number));

            handlers.ToList().ForEach(h => h.Handle(number));
        }
    }
}

Next there is the FizzBuzzCommandRetriever. I decided to keep things simple and only scan the currently executing receiver for command handlers as opposed to the entire AppDomain. This guy helps me separate the responsibility of finding finding IFizzBuzzCommandHandlers at runtime. That’s all it knows how to do. We've achieved a clean separation between the what and the how.

using System.Collections.Generic;

namespace ADubb.FizzBuzz
{
    public interface IFizzBuzzCommandRetriever
    {
        IEnumerable<IFizzBuzzHandler> GetHandlers();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace ADubb.FizzBuzz
{
    public class FizzBuzzCommandRetriever : IFizzBuzzCommandRetriever
    {
        public IEnumerable<IFizzBuzzHandler> GetHandlers()
        {
            var handlers =
                Assembly.GetExecutingAssembly().GetExportedTypes().Where(
                    t => typeof(IFizzBuzzHandler).IsAssignableFrom(t) && t.IsClass)
                    .Select(Activator.CreateInstance)
                    .Cast<IFizzBuzzHandler>();

            return handlers;
        }
    }
}
Then I have actual implementations of IFizzBuzzCommandHandlers. These are consumed by the processor and told to act on their respective inputs. Clear as day.
namespace ADubb.FizzBuzz
{
    public interface ICommandHandler<in TType>
    {
        bool CanHandle(TType target);
        void Handle(TType target);
    }
}
namespace ADubb.FizzBuzz
{
    public interface IFizzBuzzHandler : ICommandHandler<int>
    {
    }
}
using System;

namespace ADubb.FizzBuzz
{
    public class MultiplesOfThreeFizzBuzzHanlder : IFizzBuzzHandler
    {
        public bool CanHandle(int target)
        {
            return target % 3 == 0;
        }

        public void Handle(int target)
        {
            Console.WriteLine("{0} is a multiple of 3.", target);
        }
    }
}
using System;

namespace ADubb.FizzBuzz
{
    public class MultiplesOfTwoFizzBuzzHanlder : IFizzBuzzHandler
    {
        public bool CanHandle(int target)
        {
            return target % 2 == 0;
        }

        public void Handle(int target)
        {
            Console.WriteLine("{0} is even.", target);
        }
    }
}

Lastly we have the Open Closed Principle (OCP) that comes at play. I was telling Jeffrey how the template pattern lends itself very suitable to the OCP. Since we have the command retriever, it’s easy for us to plug additional handlers into the pipeline. It’s also just as easy to take them out. What I admire most is that the mere act of adding or removing a class from the assembly has no adverse affect on the runtime. See for yourself. Just comment out the class definition for one of the handlers and observe as its output never gets logged to the console. Our template comes into play with inheritance of course. There is an IFizzBuzzHandler that each handler implements. It’s merely just a marker interface. This same concept is applied with handlers in NServiceBus. I like J

Program...
using System.Linq;

namespace ADubb.FizzBuzz
{
    class Program
    {
        static void Main()
        {
            var oneToOneHundred = Enumerable.Range(1, 101);
            IFizzBuzzCommandProcessor fizzBuzzCommandProccessor = new FizzBuzzCommandProcessor();
            fizzBuzzCommandProccessor.Process(oneToOneHundred);
        }
    }
}
Output...



























Overall, it was nice to watch all the constructs come together and play nicely with one another. Each component has a single responsibility. Nice fine grained, granular classes that don’t know how to do too much. They specialize at a specific task. They know how to do one thing really, really well. And that’s it.

Tuesday, July 12, 2011

How Do You Handle Aggregates At Your Shop?

I've witnessed this one too many times.

So here's a problem that I've commonly experienced during development. You get requirements, analyze them, and start to implement your domain model based on those requirements. If you're an agile shop, those requirements will come in chunks and be implemented in sprints or iterations (whatever you wanna call it). So your initial set of requirements will only have a part of the big picture. Let's say you're going to make logic for users and profiles. To start out with, a user simply wants to view their profile. So what might some of you proceed to do? You'll only make the ProfileEntity and leave the user out if you can get away with it. Why? Well let's say when you go to the profile page, you don't need to render anything about the user; stricly profile info (let's assume for the sake of brevity that you haven't implemented UserRepository and if you have, you're still not going to use it). So you do something like....

public class ProfileRepository {
    public Profile Get(Guid userId) {
        // call NHibernate or RavenDb, YEAAAA!!
        return session
                .Query<Profile>(p => p.UserId == userId)
                .SingleOrDefault();
    }
}

Now someone like myself would say hey, aren't we in the context of the user? Isn't the user the aggregate root? Shouldn't we be doing...

public class UserRepository {
    public User Get(Guid userId) {
        // call NHibernate or RavenDb. yea!!
    }
}

// dependency inject this of course
var userRepo = new UserRepository();
var user = userRepo.Get(Guid.NewGuid());
var profile = user.Profile;

// map profile to view model if you want
return View(profile);

I'd rather be proactive about making my aggregates if I can help it. Even if they aren't immediately needed. Heck, if you want to make a bare UserEntity with nothing but it's associations until you later find out its specific properties then that doesn't hurt either. At least you'll be prepared once you have to account for the user's information.

What ends up happening is that you have a proliferation of mini repositories that should never be there in the first place. Essentially one per association. When in reality you should be fetching the aggregate and drilling down into it to pick off associations. Since you should never access the associations without first going through the aggregate. That's if you're adhering to DDD.

So instead of implementing ProfileRepository and AddressRepository that both know how to retrieve profiles and addresses by userId, you end up with one wholesome UserRepository that will serve users and their respective associations. Like orders, address, profile, etc. as I've outlined below.

But some will do what is only necessary and refactor later on. The one inherent problem is that in almost every situation, we're always in the context of a user. Which kind of leads to a question within this one. Should everything hang off of the core user object? Think of how many associations that would be.

public class User {
    public Profile Profile { get; set; }
    public Address Address { get; set; }
    public IList<Orders> Orders { get; set; }
    public IList<Comment> Comments { get; set; }

    // and so on and so forth
}

For really big applications, upon hitting Intellisense, you'd be bombarded by about 30+ properties. I guess that's not so bad. You could always house them within some other contextual object. Something like...

User.UserInfo.Profile and User.UserInfo.Address

I dunno.

Monday, May 16, 2011

Implementing read-only mode in ASP.NET Using Inversion of Control, Reactive Programming, & the Visitor Pattern

In this post I wanted to focus primarily on Inversion of Control and a little on Reactive Programming (Rx) since I think the two go hand in hand. I really like these functional style approaches to programming. IoC is very reactive in nature, because the relying party ultimately ends up reacting to the input you created for it (details to follow). These 2 concepts are very mundane in nature, but serve as the core building blocks of some of our most widely used functions and libraries. If my suggestions leave you with any doubt or skepticism, feel free to pay one of my favorite guys a visit.

You're sure to have come across IoC if you're into Domain Driven Design (DDD) like myself. I'd suggest picking up a copy of the book with the bridge. Don't worry. You won't have any problem finding it...trust me. It's been around for 5 years by now. Yea...it's that good.

Inversion of Control/Rx examples

  • how NUnit runs your tests for you
  • how ASP.NET MVC invokes your controller for you, conveniently populating the arguments you requested like route values, models, etc.
  • how jQuery abstracts away AJAX for you and allows you to be ignorant as to how the response was generated and where it came from
  • how WCF invokes your service/host for you
  • how a DI Container creates concrete instance for you as opposed to you doing it yourself
  • how Windows Forms and ASP.NET listen for events for you and allow you to react to them
  • how the Windows and the CLR unite to provide your program with command line arguments via the string[] args parameter to your command line program's Main method. You react to the arguments, but not  once have you ever had to worry about where they came from or how they got there. It's all handled by the runtime.


Let's get into some code shall we?

Recursion....Important but BOOORRRIIIINNNGGGG!!

public static class AdubbExtensions {
    public static void ToReadOnly(this Page page) {
        page.ToReadOnly(new DefaultReadOnlyModeControlVisitor());
    }

    public static void ToReadOnly(this Page page, IReadOnlyControlVisitor visitor) {
        Action<IEnumerable<Control>> recursor = null;

        recursor = controls => {
            foreach (var control in controls) {
                if (control is BulletedList) continue;

                control.IfIs<ListControl>(l => {
                    var placeHolder = new PlaceHolder();

                    l.Items
                    .Cast<ListItem>()
                    .ToList()
                    .Select(i => new ListItemControl(placeHolder, i))
                    .ForEach(visitor.Visit);

                    /* swap the ListControl with a placeholder control so it's children will be rendered. otherwise the ListControl would ignore our child controls that 
                     * we added to the Controls collection. it doesn't read that property when rendering. it reads the Items property which we're not even dealing with. */
                    l.SwapWith(placeHolder);
                });

                control.IfIs<TextBox>(visitor.Visit);
                control.IfIs<CheckBox>(visitor.Visit);

                // for ListControl, the Controls collection will be empty. the actual controls are contained within the Items property.
                recursor(control.Controls.Cast<Control>().ToList());
            }
        };

        recursor(page.Form.Controls.Cast<Control>().ToList());
    }

    public static int Index(this Control control) {
        var index = -1;
        if (control.Parent == null) return index;

        foreach (var c in control.Parent.Controls) {
            index++;

            if (ReferenceEquals(c, control))
                return index;
        }

        throw new InvalidOperationException("Could not find control in parent's control tree.");
    }

    public static void SwapWith(this Control old, Control @new) {
        var index = old.Index();
        var parent = old.Parent;

        parent.Controls.Remove(old);
        parent.Controls.AddAt(index, @new);
    }

    public static void IfIs<T>(this object target, Action<T> action) where T : class {
        var wannaBe = target as T;

        if (wannaBe != null) action(wannaBe);
    }

    public static void ForEach<TType>(this IEnumerable<TType> target, Action<TType> action) {
        foreach (var element in target)
            action(element);
    }
}

internal class DefaultReadOnlyModeControlVisitor : IReadOnlyControlVisitor {
    const string LineBreak = "<br />";
    const string SelectableItemReadOnlyFormat = "<b>{0}{1}{2}</b> {3}{4}";
    const char OpenCurly = '{';
    const char ClosedCurly = '}';
    const char X = 'X';
    const char Underscore = '_';

    public void Visit(TextBox textBox) {
        textBox.SwapWith(new Literal { Text = string.Format("{0}{1}", textBox.Text, LineBreak) });
    }

    public void Visit(CheckBox checkBox) {
        checkBox.SwapWith(new Literal { Text = string.Format(SelectableItemReadOnlyFormat, OpenCurly, checkBox.Checked ? X : Underscore, ClosedCurly, checkBox.Text, LineBreak) });
    }

    public void Visit(ListItemControl listItem) {
        listItem.SwapWith(new Literal { Text = string.Format(SelectableItemReadOnlyFormat, OpenCurly, listItem.Selected ? X : Underscore, ClosedCurly, listItem.Text, LineBreak) });
    }
}

public interface IReadOnlyControlVisitor {
    /// <summary>
    /// Converts a <see cref="TextBox"/> to read-only mode.
    /// </summary>
    /// <param name="textBox">The text box to convert to read-only mode.</param>
    void Visit(TextBox textBox);

    /// <summary>
    /// Converts a <see cref="CheckBox"/> to read-only mode.
    /// </summary>
    /// <param name="checkBox">The check box to convert to read-only mode.</param>
    void Visit(CheckBox checkBox);

    /// <summary>
    /// Converts a <see cref="ListItemControl"/> to read-only mode.
    /// </summary>
    /// <param name="listItem">The list item to convert to read-only mode.</param>
    void Visit(ListItemControl listItem);
}

/// <summary>
/// A wrapper for ListItem since it's not a control.
/// </summary>
public class ListItemControl : Control {
    public ListItemControl(Control parent, ListItem listItem) {
        Selected = listItem.Selected;
        Text = listItem.Text;

        parent.Controls.Add(this);
    }

    public bool Selected { get; private set; }
    public string Text { get; private set; }
}

The beauty of the design is that we handled the recursive part of the code. That's the part that no one wants or even cares to deal with and rightfully so. I picked up this concept while reading Real World Functional Programming. Thomas P talks about how to implement routines like Sum, Max, and Min by encapsulating recursively iterating a list and accepting a function that knows how to do the rest. The client never has to worry about writing the loop. They just provide a function that abstractly accepts 2 values, does something with them, and returns the result. In the case of Sum, you'd provide the + operator as a function by wrapping it in parenthesis. Then you'd have a function like aggregate/reduce (it's really called fold in F#) that accepts the + operator. So a client could call

let seed = 0
let ten = Seq.aggregate seed [1; 2; 3; 4;] (+)

let seed' = 1
let twentyFour = Seq.aggregate seed' [1; 2; 3; 4;] (*)

This approach is backed by this blog post and a must have in any functional style language. It's a lot more verbose to define in C#, but definitely works. You're probably thinking what I'm thinking. Isn't the Aggregate function available in LINQ? Yup. Sure is. And I use it all the time.

In my case, you should only have to worry about reacting to a particular type of control. You inverted control over to me so that you can declaratively tap into the processing of the control tree. That's kind of funny when you think about it. You mean I'm going to give this person a reference to myself and I can't even control when or how many times I'm invoked? That's the beauty of it my friends. It's what IoC is all about. The same thing happens in ASP.NET MVC. When have you ever been in control of when your controller was invoked? You're not. That's the job of the action invoker. You just have to write code. A simple yet powerful concept.

I simply pluck controls from the tree, and if they match, I tell you about it. You're kind of the subject in this case. This is similar to how IQueryable and LINQ Providers work. You write code that knows how to handle each type of expression. Then .NET notifies your expression tree visitor when that particular type of expression shows up. Also when you implement a query provider, you have no control over when your code is executed. .NET will invoke your provider accordingly once the client makes calls to Where, Select, OrderBy, etc. Lastly there's the aforementioned Reactive Framework (Rx) in .NET. It's pretty cool as well with IObserver and IObervable. Their counterparts over in LINQ are IQueryable and IQueryProvider. In both cases .NET has conveniently implemented extesion methods that make use of these 2 heavy weight abstractions. You write code, and the extension methods provided by .NET determine when it will be executed.

I can imagine the next time those Microsoft guys find a standard and generic way of executing code. They'll be some other IX and IXable interface tandem. I have to them credit. They always find ways to formulate the perfect marriage between husband and wife. I wonder if IQueryable and IQueryProvider will be producing offspring in the near future. The world may never know.

Honey, we Have a Visitor...

I'm sure that some of you were attracted to this post to find out how I made usage of the visitor pattern. I used the visitor pattern to handle each type of control. That's exactly what that pattern was created for. You make some high level class that knows how to handle concrete instance of an inheritance hierarchy (2 points for polymorphism). I find it rare that I have a legitimate purpose for using it here, but it worked to perfection in this particular scenario. I created a default implementation of my IReadOnlyControlVisitor interface, but clients are allowed to swap that out if need be. For the record, RadioButton is a CheckBox via inheritance. So I kind of killed two birds with one stone on that regard (lucky me).

Why Make a Separate Control for ListItem?

I had to treat the ListItem object with a special case. Firstly, anytime I come into contact with a ListControl, I immediately drill down into its children. Why should clients have to write the same old loop over and over. All it cares about is the individual items. Secondly, ListItem is not a control, so I needed to make a wrapper for it that encapsulates whether its selected or not. At that point, I can treat it just like any other control and swap it, find its index, etc.

Adding flavor with SwapWith, IfIs<T> and Index

The 2 extension methods, SwapWith, and Index, are 2 pretty clever utilities I implemented. They're both pretty simple and straight forward. SwapWith is just a function if Index. IfIs<T> is an idea I got from a pal and decided to implement myself. I'm sure my implementation matches his line for line. We can both agree that we were fed up writing that POTC (Plain Old Type Cast). So we implemented a more declarative and functional implementation that's not as noisy as the de facto imperative check.

Two can Play that Game

Just to flex the design a little, I came up with a separate visitor that expresses how simple it is for us to customize our implementation. We're wiping out the DefaultReadOnlyModeControlVisitor with a more naive one.

internal class RainbowControlVisitor : IReadOnlyControlVisitor {
    public void Visit(TextBox textBox) {
        textBox.SwapWith(GetDiv("red", "Wacky Red"));
    }

    public void Visit(CheckBox checkBox) {
        checkBox.SwapWith(GetDiv("blue", "Wacky Blue"));
    }

    public void Visit(ListItemControl listItem) {
        listItem.SwapWith(GetDiv("green", "Wacky Green"));
    }

    static Control GetDiv(string color, string text) {
        var red = new WebControl(HtmlTextWriterTag.Div);

        red.Style.Add(HtmlTextWriterStyle.Color, color);
        red.Controls.Add(new Literal { Text = text });

        return red;
    }
}

Getting in Trouble

There are two ways to make this implementation blow up. The first is due to LINQ and that lazy bastard IEnumerable<T> (I love you). Since I'm modifying the incoming control collection, I have to be done enumerating it by the time it arrives. Put simply, the code will fail without a call to ToList which forces eager evaluation. Secondly code nuggets (<%...%>) will make her blow. You can remedy that situation by following this stackoverflow post. There was another good post out there by Rich Strahl, but I can't seem to locate it. The simplest solution is to wrap any code using code nuggets in a PlaceHolder control. All better now?

Samples Anyone?

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>

    <asp:TextBox Text="Antwan As A Literal" runat="server" />
    <asp:RadioButtonList ID="buttonList" runat="server" />
    <asp:CheckBoxList ID="checkBoxList" runat="server" />
    <asp:RadioButton runat="server" Checked="true" Text="Antwan checked me homie!!"  />
    <asp:CheckBox runat="server" Text="R.I.P. to Bone of Cali Swagg" />
</asp:Content>

public partial class Default : Page {
    protected void Page_Load() {
        if (IsPostBack) {
            Response.Write("Why would you bind twice with view state enabled? Don't be silly.");
            return;
        }

        var foods = new List<string> { "pizza", "pineapples", "macaroni" };
        var dances = new List<string> { "cali-duggie", "detroit-jit", "atlanta-shoulder lean" };

        buttonList.DataSource = foods;
        checkBoxList.DataSource = dances;

        buttonList.DataBind();
        checkBoxList.DataBind();

        this.ToReadOnly();

        // skittles
        // this.ToReadOnly(new RainbowControlVisitor());
    }
}

Before


After


Skittles



Conclusion

And that's it. We began by handling the mundane recursive part of our implementation to alleviate the burden on our clients. No one should have to repeatedly implement that code. This set the stage for IoC. It gave us the ability to serve controls to the client in a reactive and convenient fashion. We made use of the visitor pattern to handle each type of control we wanted to convert to read-only mode. We provided clients with a default implementation but gave them the ability to override that implementation by providing their own version of IReadOnlyControlVisitor. Lastly we handled the special case for ListItems since they do not inherit from the base Control class provided by ASP.NET.

Thursday, May 5, 2011

Synchronizing Files With F# and the FileSystemWatcher

So I needed a way to automate change tracking on a set of directories and have those changes merged to another set of directories. In my case, I'm dealing with directories that have a similar make up. That is to say, they contain the same files, folders, etc. Just in different locations. They're essentially clones of one another. The team I'm currently working on calls these packages. I certainly don't agree with the way they implemented it and all the duplication, but I'm not going to manually copy my changes to 2 other directories all day long. So I came up with a simple utility to do the work for me. It's not fully polished yet, but I wanted to get my initial implementation up online. If you actually tried to use the program, things would work, aside from the IOException thats generated after subsequent saves due to the host process somehow maintaining a lock on the files. Now normally it's the developer's fault, and it probably is in my case, but based on the very nature of the function I'm calling, and what it promises to do for me, I doubt it. I'll resolve it in the coming days though. Lastly, I'm working purely with code (.cs, .aspx, .ascx, etc.) so I can get away with calling File.ReadAllText. I'm not even going to think about binary. Maybe it'd still work. Who cares...lol. Enough talking already. Here's the code.

Iteration I - 4 April, 2011

// Learn more about F# at http://fsharp.net
open System.Xml.Linq
open System.Reflection
open System.IO
open System.Linq
open Microsoft.FSharp.Control
open System.Threading
open System

let pathAttributeName = "path"
let xs n = XName.Get(n)
let wildcard = "*.*"

let workflow = async {
    printfn "Started listening at %A..." DateTime.Now

    while Console.ReadLine() <> "q"
        do
            let doc = (Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName) + "\Synch.config" |> XDocument.Load
            let config = doc.Root
            let root = config.Element("root" |> xs)
            let rootDir = root.Attribute(pathAttributeName |> xs).Value

            let mapdirs (e : XElement) =
                e.Elements("add" |> xs)
                |> Seq.map((fun (e : XElement) ->
                                let dir = [|rootDir; e.Attribute(pathAttributeName |> xs).Value;|] |> String.Concat
                                dir))

            let directories = config.Element("directories" |> xs)
            let masters = directories.Element("masters" |> xs) |> mapdirs
            let slaves = directories.Element("slaves" |> xs) |> mapdirs

            let directoryWatchers = masters
                                    |> Seq.map((fun d -> 
                                                    new FileSystemWatcher(d, EnableRaisingEvents = true, Filter = wildcard)))

            directoryWatchers 
            |> Seq.iter((fun w -> 
                            w.Changed.Add((fun e -> 
                                            let merge fp fn =
                                                let targetDir = Path.GetDirectoryName fp
                                                let content = fp |> File.ReadAllText

                                                slaves
                                                |> Seq.iter ((fun d -> 
                                                                    let fileName = [|d; "\\"; fn;|] |> String.Concat

                                                                    if File.Exists fileName then
                                                                        try
                                                                            File.WriteAllText(fileName, content)

                                                                            printfn "Merged %s to %s at %A %s" fp fileName DateTime.Now Environment.NewLine
                                                                        with 
                                                                            | :? IOException as e ->
                                                                                printfn "Antwan said he'd handle it later. He's eager to get his post up now!!"
                                                                    else
                                                                        printfn "File %s did not exist in directory %s. No merge required. Aborting...%s" fileName d Environment.NewLine
                                                                    ))

                                            e.Name |> merge e.FullPath))))

    printfn "Stopped listening at %A..." DateTime.Now
}

let start() = 
    workflow |> Async.RunSynchronously

do start()

And here's the configuration file I use. No it's probably not the most intuitive xml file you've ever seen, but it works for me. I called it Synch.config and placed it in my bin/Debug directory.

<watch>
 <root path="C:\Users\A-Dubb\Documents\" />
 <directories>
    <!-- Directories I'll be working in -->
    <masters>
      <add path="TestDir" />
    </masters>
    <!-- Directories I want my work merged to -->
    <slaves>
      <add path="TestDirII" />
    </slaves>
 </directories>
</watch>

It's nothing too complex. I just listen for changes in the master directories and merge them to the slave directories. Pretty cool though. It's definitely a good candidate for a Windows Service. I cheated with a while loop to force the main thread to wait on me without exiting the program. There are numerous ways to achieve that behavior as well, but it was quick and painless. I bet you something like DropBox makes use of a similar construct like FileSystemWatcher to keep your files in synch between machines. I'll upload the patch to resolve the IOException once I have time to delve into it.

Wanna get her up and running quickly? You got it. Just download Funtastic. It's a lightweight F# editor. Basically just a wrapper around F# Interactive. She's quite handy though.

For now, adios my friends.

Iteration II - 5 April, 2011

Ok. So I figured out what the problem is. First off, my exception handling code is in the wrong place. It should be concentrated on the attempt to read the file that was actually changed. Not the files that need to be patched. Number 2, since I'm subscribing to the Changed event, it gets triggered just by me simply reading the file. It's cause the file's metadata get's changed by the OS upon reading it (LastAccessedDate). So the Changed event happens so fast (probably nanoseconds) that as I'm reading the file the first time around, I attempt to read it again. Don't believe me? Open up once of your tracked files in Notepad++ and watch it get logged to the console. Even better, upon running the application, you'll notice that you always see the same file get merged to each directory twice. So instead of seeing 2 sets of output, you see 4. I'll have to find a way to suppress notifications for reads. I did try opening the file with FileAccess.Read and FileShare.Read. That didn't work 100% of the time but did seem to be a lot better than what I had before. I also like how ReadAllLines and ReadAllBytes are more high level. I don't have to worry about managing streams, disposing them, reading them, etc. The problem is, you don't have control over access permissions when consuming the file because of the defaults .NET sets for you. I'd never have a source file that's over 2 gigs, but that the most you can load in memory with my current approach because of Int.MaxValue. Maybe the guys at Microsoft know a way around that with their implementation. Who knows? Lastly, I'm working with raw bytes now since that's the fundamental makeup of every file whether it be binary or text based. So I take back my statement from earlier. I kind of do actually care now. I thought I'd have to make some fancy factory that knows how to read and write each file based on it's extension. That'd be one of three things: Either an infinite switch block, a jam packed dictionary, or a regex longer than the Mississippi. Anyway, here's my current revision. You're probably starting to think I'm trying to obsolete git by now. Forgive me. I just want an immediate view of how many times I took a swing at this thing. Don't worry. I'll call it a strikeout at 3.

// Learn more about F# at http://fsharp.net
open System.Xml.Linq
open System.Reflection
open System.IO
open System.Linq
open Microsoft.FSharp.Control
open System.Threading
open System

let pathAttributeName = "path"
let xs n = XName.Get(n)

let workflow = async {
    printfn "Started listening at %A..." DateTime.Now

    while Console.ReadLine() <> "q"
        do
            let doc = (Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName) + "\Synch.config" |> XDocument.Load
            let config = doc.Root
            let root = config.Element("root" |> xs)
            let rootDir = root.Attribute(pathAttributeName |> xs).Value

            let mapdirs (e : XElement) =
                e.Elements("add" |> xs)
                |> Seq.map((fun (e : XElement) ->
                                let dir = [|rootDir; e.Attribute(pathAttributeName |> xs).Value;|] |> String.Concat
                                dir))

            let directories = config.Element("directories" |> xs)
            let masters = directories.Element("masters" |> xs) |> mapdirs
            let slaves = directories.Element("slaves" |> xs) |> mapdirs

            let directoryWatchers = masters
                                    |> Seq.map((fun d -> 
                                                    new FileSystemWatcher(d, EnableRaisingEvents = true, IncludeSubdirectories = true)))

            directoryWatchers 
            |> Seq.iter((fun w -> 
                            w.Changed.Add((fun e -> 
                                            let merge fp fn =
                                                let targetDir = Path.GetDirectoryName fp
                                                
                                                try
                                                    use fs = File.Open(fp, FileMode.Open, FileAccess.Read, FileShare.Read)
                                                    
                                                    let size = fs.Length |> int
                                                    let buffer = Array.zeroCreate<byte> size
                                                    
                                                    fs.Read(buffer, 0, size) |> ignore
                                                    
                                                    slaves
                                                    |> Seq.iter ((fun d -> 
                                                                    let fileName = [|d; "\\"; fn;|] |> String.Concat

                                                                    if File.Exists fileName then
                                                                        File.WriteAllBytes(fileName, buffer)

                                                                        printfn "Merged %s to %s at %A %s" fp fileName DateTime.Now Environment.NewLine
                                                                    else
                                                                        printfn "File %s did not exist in directory %s. No merge required. Aborting...%s" fileName d Environment.NewLine
                                                                    ))
                                                with
                                                  | :? IOException as ioe ->
                                                        printfn "exception occured %s %s" ioe.Message Environment.NewLine
                                               
                                            e.Name |> merge e.FullPath))))

    printfn "Stopped listening at %A..." DateTime.Now
}

let start() = 
    workflow |> Async.RunSynchronously

do start()

I'll be back for my last strike later.

Iteration III - 5 April, 2011 3:54 PM

Ok. I spent a few minutes looking around at something I completely ignored to start out with. This line allows you to filter your events. You can filter events using F#, but this is even simpler. It still doesn't work for me though, because as soon as I open the file, that in and of itself is considered a change.

new FileSystemWatcher(d, EnableRaisingEvents = true, IncludeSubdirectories = true, NotifyFilter = NotifyFilters.LastWrite)))

I'm throwing in the flag for now, but you have to admire my persistence. It was kind of fun heuristically playing with the FileSystemWatcher. At least I'm fully aware of its potential limitations. Cool :).

Sunday, April 17, 2011

Implementing an NBA Playoff Bracket in F#

I'm a huge fan of the NBA and sports in general and for years I've been fascinated with the way the NBA structures their playoff bracket. They take the top 8 teams from each conference and seed them based on which teams have to most wins. There are 2 conferences in all so that makes 16 playoff teams each year. The top seed plays the worst team, the second seed plays the seventh team and so on and so forth. So I thought it'd be cool to actually code this setup in F#.

I started with a method that knows how to read in each team from a text file.

// Learn more about F# at http://fsharp.net

open System
open System.IO
open System.Reflection

type Conference =
    | Eastern
    | Western

type Team = {
    Name : string;
    Wins : int;
    Losses : int;
    Conference : Conference
}

let filename = (Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName)  + "\Teams.txt"
let totalgames = 82
let max = totalgames + 1
let r = new Random()
let conferencesize = 15
let playoffteamsperconference = 8
let half = playoffteamsperconference / 2

let getteams() =
    seq {
        let mapteam conference (l : string) =
            let wins = r.Next(0, max)
            { Name = l.Trim(); Wins = wins; Losses = totalgames - wins; Conference = conference;}

        let teams = filename |> File.ReadAllLines

        // first 15 teams are the eastern conference
        let eastern = teams
                        |> Seq.take conferencesize
                        |> Seq.map (mapteam Eastern)
        
        yield! eastern

        // last 15 are the western conference
        let western = teams
                        |> Seq.skip conferencesize
                        |> Seq.take conferencesize
                        |> Seq.map (mapteam Western)

        yield! western
    }

The chunk of code within the seq{...} scope is known as a computation expression. This particular type of computation expression is called a sequence expression. It's a language integrated feature of F# that allows you to use certain operators based on a set of methods you implement. In this case, the compiler will translate my call of yield! to a method call that knows how to accept a sequence and return it's values. Computations in F# are monads; a fundamental feature of all functional languages. Believe it or not, the infamous LINQ as you know it is based on monads.

Amusingly enough, you've been using monads in .NET for quite a while even if you aren't a normal user of LINQ. Ever used Nullable<T>? It's a maybe monad. Even the infamous jQuery is a monad. It either has a value or it doesn't. In F# we represent this type of construct using the generic option type. Options are a discriminated unions. They're represented by Some 'T or None; where 'T is a generic type argument.

An example is

let o = Some 5
let p = Some "string"

Now o is an int option and p is a string option.

This is cool because we never have to worry about a value being null. Null isn't even a valid value in F#, but it is however a valid .NET value.

How do we find out if o or p has a value? We have to use pattern matching.

let printifhasvalue optionvalue =
    match optionvalue with
    | Some v -> printfn "%A" v
    | _ -> ()

You can think of patterns as switch statements. And that's exactly how the compiler translates them. When using pattern matching, you have to handle all cases or the compiler will yell at you. In my case I only have to worry about Some and None. I accounted for Some with the first check, and I used the wildcard value, _, to handle all other cases. If there were a third option, we'd have a problem because there would be 2 more possibilities. The compiler knows that None is the only other option so we're ok.

So when inter operating with other .NET libraries we do have to account for it. F# libraries never return null though. As for discriminated unions, I'll talk more about those later. As a heads up, my Conference type is one.

The map function accepts a function that can take a value and transform it into another type of value. It's just like Select in LINQ. When I called Seq.map (mapteam Western), I used a concept called partial application.

The expression (mapteam Western) isn't actually invoking the mapteam function. It actually returns a function that accepts the remaining arguments for mapteam. In our case it's the actual team. This is also known as currying. If mapteam took 3 arguments, I'd get a compile time error because I'd get a function back that accepts 2 arguments as opposed to 1. In that  case I'd have to do (mapteam Western arg2), to get a function that accepts only 1 argument. Pretty cool.

Most everything is a function in F#. Event the + and - operators. Don't believe me? You can use the operators as functions by wrapping them in parenthesis like so: (+). The output of that expression is a function that accepts 2 ints and returns another. Or (int -> int -> int).

I also needed a datastructure to store each team and its properties. Specifically I used what's known as a record in F#. It's completely separate from a class and the two are not synonymous.

There are 82 games in an NBA season so I wanted to randomly generate the record for each team. I generate a number between 0 and 82 inclusively for the wins and subtract that from the remaining games in the season to compute the losses. Pretty simple.

As far as which team each conference belongs to, I got the names of each team from the NBA.com website and typed them in order into my Teams.txt text file. I tried to keep things simple on that regard.

Teams.txt


Chicago
Miami
Boston
Orlando
Atlanta
New York
Philadelphia
Indiana
Milwaukee
Charlotte
Detroit
New Jersey
Washington
Toronto
Cleveland
San Antonio
L.A. Lakers
Dallas
Oklahoma City
Denver
Portland
New Orleans
Memphis
Houston
Phoenix
Utah
Golden State
L.A. Clippers
Sacramento
Minnesota

The cool thing about F# is that it's functional. That means we should implement light weight composable functions. That's exactly the approach I've taken here. Each function builds atop the other. A simple f(g(x)) relationship if you will.

And by the way, all values in F# are immutable by default. That means we can't change the state of something once we've created it. This greatly simplifies multithreaded programming, because you don't have to worry about multiple threads changing the state of your data. You can rest assured that once you give a function a reference to a value, it'll be in that very state once the operation is completed.

F# isn't a purely functional language so we do have mutable properties and values that we can pass around. Just not by default.

So now that we have a function that knows how to fetch each team and generate their record, we'll make something that can consume the output of that function and print each team.

let printteams (teams : seq<Team>) =
    teams
    |> List.ofSeq
    |> printfn "%A"

The printfn function is a cool utility because it knows how to print a generic object. It can be a sequence, a base type, etc. It's just like printf in C. You can pass a format like %s for strings and %d for integers.

I used List.ofSeq from the List module to convert my sequence to a List. I did that because sequences can be infinite in F# so the printfn function wouldn't print out all the values. A List on the other has is finite. So printfn will iterate the entire sequence and print out each element as opposed to the first few.

The type seq<'a> in F# is equivalent to IEnumerable<T> in C#. It represents a possibly infinite list of elements. I work solely with sequences throughout my implementation. All instances of Seq.x represent a set of functions known as the Sequence Module in F#. You can think of it as a class with a bunch of static methods. The funny looking |> syntax is just an operator. It takes a function and its argument and invokes that function passing in the given argument as a value. F(x) once again. I don't have to use this operator, the forward pipe operator as it's known, but I really like it's logical syntax so I kind of abused it here. It's no different than piping output from one command to another when using bash on Linux or any other command shell. Piping input to grep is really nice by the way.

There is also a backward operator that knows how go go in the opposite direction. It accepts x first then pushes it into f. That one reads from right to left like

List.ofSeq <| x

In order to pull of the same syntax without the pipe operator, I'd have to nest all of my return values. It'd look like f(g(x)) or

printfn "%A" (List.ofSeq teams)

In this case, g if my List.ofSeq function which accepts the teams. The teams are of course x. The output of that function is then passed to printfn. That makes printfn g in this equation. It doesn't look so helpful in simple scenarios, but later on you'll witness me using the pipe operator quite aggressively; and I think you'll start to appreciate the elegant syntax it allows you to exercise.

And since F# is functional, functions are first class citizens. They don't have to belong to any particular object in general just like in JavaScript. You can pass them as normal values just like ints, GUIDS, and any other base types. That makes F# a really powerful language.

You're probably wondering how I got away without specifying types. Don't you expect to see int and string? Well the F# compiler implements what's known as type inference. It's able to infer types based on the way they're used. We rarely have to specify types in F#, but I had to do it a few times with my implementaton using type annotations. These appear in the signature of my method. Method signature syntax goes

functionname arg1[type] arg2[type] arg3[type]...argn[type]

F# interprets this as (arg1 -> arg2 -> arg3 -> argn -> returntype)

The last value is the return type. Just like Func<T> in C#.

So anytime you see (blah -> blah), that means a function. If you ever see this from intellisense when hovering over a method, you can believe that method accepts a function as an argument; so be prepared to pass one.

Arguments are delimited by spaces. The type of the argument is optional and is required based on whether the compiler can use its type inference algorithm to infer the type of the argument. We don't need curly braces also, because F# detects scope based on spaces. Four spaces to be exact.

Now that I can get all the teams in the league, I need to group them by their conference. There are 2 conferences in the NBA. The eastern and western conferences. You'd normally represent something like this an an enum in C#, but we have a more functional construct known as discriminated unions in F#. My discriminated union is called Conference. You're probably starting to notice that the functions I'm using look a lot like LINQ. That's because LINQ's roots are tied deeply to functional programming.


let getteamsbyconference() =
    getteams()
    |> Seq.groupBy (fun t -> t.Conference)

I again made a function that knows how to print the teams out. Since I grouped the conferences, they came back as a pair or tuple as we call it. So I have to drill down into the conference to get the teams. Then from their it's business as usual. I can simply reuse the initial function I created that knows how to print a sequence of teams.

let printteamsbyconference (conferences : seq<(Conference * seq<Team>)>) =
    conferences
    |> Seq.iter (fun (_, teams) -> teams |> printteams)

You probably noticed the weird syntax I used in my function to iterate the conferences. It another form of pattern matching.  As I mentioned before, the _ represents the wildcard character. That means I don't care about the result of the first value; which in this case is the conference. The syntax I used is a pattern for tuples because its wrapped in parenthesis and delimited by a comma. That means I working with a pair, but I could have easily been working with a triple, quadruple, and so on and so forth. I could have called my teams parameter whatever I like. The name you give to your parameters is completely arbitrary.

I can get each conference and its respective teams now. It's time to make something that knows how to get the best 8 teams from each conference.

let getplayoffteams() =
    getteamsbyconference()
    |> Seq.map (fun (c, teams) -> (c, teams |> 
                                      Seq.sortBy (fun t -> t.Losses)
                                      |> Seq.take playoffteamsperconference))

For each conference, I sort the teams in the conference by the number of losses they have. Logically you'd think I've order by wins, but the sortBy function orders in ascending order. That means the teams with the least number of losses will be at the front of the pack. Logically the teams with the lest number of losses are the best right? That means they have the most number of wins. After sorting the teams, I take the top 8 teams from each conference and return them as a tuple to pair them with their conference.

Anytime you see the (fun x -> ...) syntax, that represents a lambda. Lambdas are pretty big in functional languages. It's with the lambda symbol that we mathematically denote a function. That's some old school history related stuff and it's admittedly pretty boring. It is kind of nice to know though.

The last step is to order the teams and make the final bracket.

let printplayoffbracket() =
    getplayoffteams()
    |> Seq.iter (fun (c, teams) -> 
                        Console.ForegroundColor <- ConsoleColor.Red

                        printfn "%A conference matchups\n" c

                        let topfour = teams |> Seq.take half
                        let bottomfour = teams |> Seq.skip half |> Seq.take half |> Seq.sortBy (fun t -> t.Wins)
                        
                        Console.ForegroundColor <- ConsoleColor.Yellow

                        bottomfour
                        |> Seq.zip topfour 
                        |> Seq.iter (fun (topseed, bottomseed) -> 
                                        printfn "%s (%d-%d) vs %s (%d-%d) \n" topseed.Name topseed.Wins topseed.Losses bottomseed.Name bottomseed.Wins bottomseed.Losses)
                        Console.ResetColor())

We consume the playoff teams and match the best teams against the worst teams. I used closures in this case. I know the top for teams are at the front of the pack, so I simply took the first 4 teams out of the 8 available in each conference. After that, I took the last 4 teams and sorted them by the number of wins they had. Again, you'd logically then I'd sort them by the number of losses they had. But again I know the worse teams are the teams with the least number of wins. The zip function knows how to pair up each member of one sequence with a member of another sequence. It will do this for each pair it can find. If one sequence is bigger than the other, it'll stop pairing elements from each sequence once it's paired the number of elements equivalent to the smallest sequence.

Now it's time to watch our little composable puppies in action

do getteams() |> printteams
do getteamsbyconference() |> printteamsbyconference
do getplayoffteams() |> printteamsbyconference
do printplayoffbracket()


We use the do keyword in F# to execute imperative code. That's code that doesn't return a value and just executes an action. It'll have the return type unit, or void in C#. In F# we always have to return a value. It'll either be an actual value like a record or tuple, or unit. Unit is denoted by (). So to return unit from a function you just write

let f() =
    ()

The function I definte above not only returns unit, but accepts it as an argument. So event when you think you're calling a parameterless method in F#, you're really not. And when you think you're not returning anything, you actually are.

And that's it. We started out by making a function that could read in each team and generate wins and losses for it. Then we grouped each one of those teams into the right conference. Next we were able to take the top 8 teams from each conference, which were our playoff teams. And lastly, we pair up the best teams with the worse teams in each conference just as the NBA does it.

If you want to try out my code, you can download F# and fsharp.net. It's deployed as its own toolset, independent of Visual Studio. I'd recommend Visual Studio so you can have intellisense though. If you want to get down and dirty, you use the F# interactive command shell. It's an interpretor so you're allowed to execute raw code without compiling it.

Don't forget my favorite 2 books. Real World Functional Programming and Expert F# 2.0. Also checkout my 2 favorite guys, the authors of my 2 favorite books, Thomas P and Don Syme.

I think it's safe to say we implemented map reduce here.

Cheers!!


Source can be found here.