Text
Solving a simple problem with Underscore
When I first saw Reaktor's recruiting puzzle I knew had to try it. It introduced simple, yet bit painful to implement with vanilla javascript and encouraged utilizing Underscore.js library. They would contact only if you could solve problem in a elegant way.
Anyways they provided string length of 1000 characters. There you have to find the highest multiplication of set of 5 numbers. Implementation must be done in javascript, easy isn't it? This is almost like world famous Fizz buzz test for programmers.
There is nice online editor on page where you can write your solution and you get green light when you find correct number. I used it for getting input string since I really like development tools I have on my laptop.
First I started with simple alert(input); to verify they won't screw with me.
Everything seems to be in order. Convert to numbers, loop through numbers and calculate highest group of five. Or just write "return xxxxx;". It would be the most elegant solution after all, right?
My first try was vanilla javascript described above, it turned out to be one ugly for loop -- doing multiplication and variable keeping the highest number. Nothing really interesting there.
I was really keen to see if Underscore could solve problem more elegant way. After reading documentation a bit, it turned out to be really nice little library for handling collections and arrays. A real gem of the library is at the bottom of the documentation - .chain, but more about it later in this post.
https://gist.github.com/mrasanen/6710723
First refactoring (above) was more into utilizing Underscore, but it was still ugly and reminded vanilla javascript I wrote earlier. Also make note multiplication is done with indexes and static numbers. You would hit a problem if calculation logic is changed even a bit.
https://gist.github.com/mrasanen/6724953
Second refactoring (above) was more into chaining and slicing and dicing number groups. Code looks similar to LINQ queries in C# and it's not really a bad thing. All of console.log(); methods removed before script actually pasted to Github. And now, after 4 weeks I wrote that, I'd do it in different way, but code is readable and you figure out what it does in seconds.
Thank you Reaktor for making great problem to solve and inspiration. I'm sorry I didn't email my solution to you, Maybe next time :)
Bottom line is that they really know programmers are into solving problems and learning new things around their craftmanship. At least I found good utility library and learned a bit how to use it and when. But this puzzle got me curious about functional programming in javascript and that's why I bought this book: Functional JavaScript: Introducing Functional Programming with Underscore.js So far it has introduced me another world. Great book.
ps. My first tries with Underscore were a mess -- tried using .groupBy but it wasn't what I needed..
0 notes
Text
Appharbor and Open Type Fonts
Application deployed fine to Appharbor but after browsing around noticed fonts were wrong. I mean totally wrong. For some reason all custom Open Type fonts failed to load. In Visual Studio 2012 MVC4 project their properties were fine -- in other words Build Action was Content as it should be.
Tried to access font directly with browser with no luck, got 404. It turned out I was missing mimetype for those fonts. When inserting lines to Web.config and their local development environment (Win8+VS2012) all resources refused to load. Ouch. So it's needed only in release deployment and I decided to take shortcut there and inserted following lines to Web.Release.Config inside <system.webServer> element:
<staticContent xdt:Transform="Insert">
<remove fileExtension=".otf"/>
<mimeMap fileExtension=".otf" mimeType="font/otf" />
</staticContent>
Pfft, problem solved.
0 notes
Text
VMWare Fusion and Hyper-V in Windows 8 guest machine
As a .Net developer it's sometimes hard to make your development environment work as you like if you're running Mac. Naturally virtualized OS makes life easier. I had trouble running Windows Phone emulator (SDK 8.0) on Windows 8 guest machine -> when I tried to run application it didn't launch emulator but failed miserable.
It turned out that all those new emulators are actually .vhd files, which is Microsofts format for virtual machine disks and Hyper-V runs them. Well the trouble was that Hyper-V wasn't really working in my guest (Complained there wasn't required services running) and some Hyper-V related device drivers were down. It was time to remove Hyper-V and reboot virtual machine :)
After reboot I went to Control Panel to add Windows features and found out Hyper-V Platform were grayed -> you couldn't select it and tooltip show following text: "Hyper-V cannot be installed a hypervisor is already running". Oh. my.
Then I called my favourite nerd: Google.
After Googling and testing here are the settings I used to make Hyper-V work in my Windows 8 guest machine.
VMWare Fusion Settings:
Processors & Memory: check Enable hypervisor applications in this virtual machine.
Advanced: select Intel VT-x with EPT under Preferred virtualization engine.
After that you have to manually edit machinename.vmx file with your favourite editor and add
hypervisor.cpuid.v0 = "FALSE"
in end of file.
Note: You can locate your machine in Fusion's Virtual Machine Library and right click virtual machine name and select Show in Finder. Next I opened terminal to same location and changed directory to inside virtual machine -> cd machinename.vmwarevm and open .vmx file with editor.
Now you should be able to install Hyper-V (if your hardware supports features hyper-v needs) and run Windows Phone emulator with it. The most important part was manually editing .vmx file, after that everything ran smoothly.
Resource I used
0 notes
Text
Azure web sites vs. Appharbor
Azure launched few weeks ago (7th June) new preview of management portal with new features: Web sites, Virtual machines & Virtual networks and Media services. So far I have been testing Web sites and Virtual machines (Haven't had time for Media services yet).
I'm glad to see that Azure is going to good direction, since it's deployment model with special project types sucked and I really like clean solution what I'm able to deploy host I want to. Also new management portal is smooth compared to old one, but you still need it since not all features are available in new portal. Setting up Web sites/Virtual machines is super easy and Microsoft provides step-by-step instructions for common tasks, like running MongoDB on CentOS Linux virtual machine.
Anyways back to topic; Deployment model to Azure web sites is implemented quite the same way as Appharbor has so in practice they compete directly.
Currently Appharbor is way ahead of Azure Web sites:
Appharbor has background workers
Azure Web sites don't have SSL support
Appharbor has Github publishing
Azure has TFS publishing
Appharbor runs unit tests on build
On the other hand those can be implemented to Azure quite fast and I don't think they are far away when previews are launched to public in fall.
Appharbor has tons of Add-ons for developers
If there will be lots of external service providers for Azure (aka. Add-ons), then you could say Appharbor is in trouble. In the developer's view this would be good alternative (and maybe lower prices with competition). This would also require those add-ins be in same datacenter to avoid latency.
Those are things I usually need/want when developing something. There are more arguments why Appharbor should exist in their blog post: Some note on the recent Azure launch
Azure could be interesting choice to develop in cloud, since nowadays every developer has free computing time there and therefore it can be cheap, but it all depends on traffic site has. After all, it's all about money :)
Quote: "Hope Appharbor didn't turn down M$ money.. if they offered any."
0 notes
Text
OSX 10.7 as a development environment for NodeJs
Getting installer from NodeJs and installing node with it is confusing, since it does global install and you need to use sudo all the time you need to execute node or npm. Which kinda kills productivity while developing.
Here is how you can do local install, but it requires you to compile sources yourself.
Install Xcode from App Store
Install Command Line Tools from Xcode Preferences -> Downloads
Run Terminal
mkdir folderyouwantouseinhome
open .profile (if it doesn't exist, touch .profile)
Add path to .profile: export PATH="/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core":$PATH
git clone https://github.com/joyent/node.git
cd node
./configure --prefix ~/folderyouwantouseinhome
make
make install
Change path on .profile to include path to new binaries: export PATH="$HOME/folderyouwantouseinhome/bin:/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core":$PATH
Restart Terminal
Now you're able to run node and npm without sudo.
2 notes
·
View notes
Text
Using jQuery against tumblr API
I spend too much time figuring out why I wasn't able to pull blog posts from tumblr to our website. It turned out to be too easy _AFTER_ reading API documentation with glasses on..
This is what tumblr API documentation says:
All requests made with HTTP GET are JSONP-enabled. To use JSONP, append jsonp= and the name of your callback function to the request. JSONP requests will always return an HTTP status code of 200 but will reflect the real status code in the meta field of the JSON response.
By default jQuery uses ?callback= query string parameter and it can be changed to ?jsonp= with one configuration option. Refer jQuery ajax documentation for more information.
Example code:
https://gist.github.com/mrasanen/6726863
Most important lines are dataType and jsonp settings. dataType enables jsonp request, and that way you're able to do cross domain requests. Jsonp setting actually changes querystring key for callback function.
Quote: "If you're going to blog, publish post immediately otherwise you will do it after 6 months"
1 note
·
View note