Liftweb CSS Selectors and implicits

When using Ruby, i found myself frequently making things like formatting methods to display a variety of things.

def formatDouble(dbl)
     "%.2f" % dbl
end

Often these output formatters are render-specific, so defining them on the model seems kinda unflexible to me. Of course if you only have one Double to format, this is not the best/quickest way to go, but imagine you have a Snippet which has 3-4 BigDecimals to format.

class MySnippet {
     def formatDouble(a: Box[BigDecimal]) = a match {
           case Full(dbl) => "%.2f".format(dbl)
           case _ => ""
     }

     def render = ".field1 *" #> formatDouble(myRecord.field1.is) &
                  ".field2 *" #> formatDouble(myRecord.field2.is)
}

Putting a format around everything like this would be too much code for me, even for 2 lines this looks like crap to me.

What to do about it?

There is a very simple way of dealing with things, this is due to the fact that #> will only take String and NodeSeq (and a few other’s you usually don’t come to contact with), so we can work with Scala’s implicit.

class MySnippet {
     implicit def formatDouble(a: Box[BigDecimal]) = a match {
           case Full(dbl) => "%.2f".format(dbl)
           case _ => ""
     }

     def render = ".field1 *" #> myRecord.field1.is &
                  ".field2 *" #> myRecord.field2.is
}

In my (humble LOL) opinion, this looks a lot better. You can also apply this on different needs like with Tuples:

class MySnippet {
     implicit def formatPrice(a: (Box[BigDecimal], String)) = a._1 match {
           case Full(dbl) => "%.2f %s".format(dbl, a._2)
           case _ => ""
     }

     def render = ".field1 *" #> (myRecord.price.is, myRecord.currency.is) &
                  ".field2 *" #> (myRecord.shipping.is, myRecord.currency.is)
}

I hope this helps some of you folks to make your code more readable!

And yes, i admit that these example are prolly not the most advanced use cases, but you should get the idea :)

Cheers

London called - @dpp, @skillsmatter and #functionalpx

So i’ve spent the past 3 days in London, to be more specific, at Skills Matter together with David Pollak.

First off, i don’t like London. I don’t know if it’s the UK, but i defiantly hate the City for various reasons (tho i love Fish and Chips). I mainly hate it for it’s public transportation which is a nightmare. I mean i’ve been to Amsterdam, Paris, Palma de Mallorca, Hong Kong and Bali (where they don’t have real roads), but i’ve never had issues like i had in London. Confusing is an understatement.

Skills Matter

Anyhow, i love Skills Matter. The effort they put into Open Source Events is unmatched, i’ve never ever encountered a group that passionate about what they do, which is helping amazing people come together and share their knowledge and make them feel welcome.

This welcoming feeling comes from the location (which is a nice loft transformed into a hackers heaven), filled with loungy chairs as well as real ones for conferences and of course the caring Staff. I loved meeting the folks there again, especially Amy and David (Hall) as well as Wendy and Paul! You guys do an amazing job, seriously, you make me forget all about the things i don’t like about London :)

alt text

Lunch with the Guardian Guys

On the day of my arrival David Pollak and i had a nice lunch with the guys from The Guardian. It’s always nice talking to Graham, i hope next time we’ll get some one on one time :) Also the chicken was AWESOME!!! to say the least :)

The Course

So i was really nervous about what David Pollak expected me to do, i hope i didn’t let anyone down! I loved the way i was thrown into it by David, he is amazing in every aspect! The support and suggestions are unmatched to anyone i’ve ever met, so i hope you understand my excitement about meeting him. It’s always interesting, fun and i am always nervous like i was on a first date :) Ok, those 3 pints helped me loosen up and enjoying private meals with him is awesome!

alt text
(he is the good looking guy, i am the one with the stupid grin ;)

So the course was interesting, it was unlike any course i’ve ever attended. This is probably due to the low number of participants which makes it tailor-made for everyone attending.

It was very interesting learning about other people’s issues and experiences with Lift, since i’ve become a true believer in the framework and i want people’s input (mostly to refute their cons ;). There is nothing like Liftweb, yes, you may quote me on that.

Overall i think i’ll be able to teach David Pollak’s Lift Basics in the future, but i am not sure i can provide a knowledge that deep, because, well let’s be serious, he is the Founder ;)

But i guess it will be a couple of months before i start teaching a course, so i’ll have plenty of time to dig a lot deeper into Lift which i am happy to do!

The Functional Programming Exchange

So this was a last minute decision which i have to thank Paul and David Hall for! :) It was interesting, but sadly Miles Sabin couldn’t make it! :( I’d really wanted to see you again!

Also watching David Pollak showing us Visi.Pro was very nice. At least now i know that if Scala gets boring, i’ll look into haskell :)

Thanks to everyone

I really wanna thank every single one i’ve met, you made me feel welcome in a City which doesn’t welcome me at all! ;)

I especially want to thank our attendees of the course :) Without you that trip wouldn’t have been possible!

Cheers

Why is everyone so fucking stupid? UBERRANT!

I encounter stupidity on a daily basis, wether it is wannabe Linux-Admins like this guy “gehirn”/“gott” i chatted with on several occasions, but it’s not just him, he’s one of the 99% of Occupied by Brainfarts in my line of work.

#1 Source based distros are not for production

You really think that people using source based distros like FreeBSD (ports), NetBSD (pkgsrc), Gentoo (Portage) or Archlinux (AUR) have too much time? LOL

I am not sure what fucking old ass boxes you sell to your customers, but i can imagine that selling them Towers from late ‘05 as “Servers” will make compilation very slow. Guess what, the boxes i buy for my customers are hand picked, not some Hardware i have 20x stacked in my Warehouse because i’m convinced that every customer needs roughly the same stuff - the joke is on you, buddy.

But i don’t mean to tell you how to run your business, oh sorry, you don’t have your own business, you’re just an employee and want to tell me bullshit? LOL Take a hike, mike!

#2 Binary distros are so leet and production ready

Yeah, we’ve all seen how great binary distros are, especially when fucking package maintainer try to fix fucking compiler warnings by vuln’ing up code CVE-2008-0166.

Oh, i’m sorry, your meds prevented you from thinking that far? I figured. Yes, you may cry now “but in source based distros, there can also be bad patches”.

That may be so, but at least people have a chance of READING the fucking code, if you get it in binary, it will be very hard to see what is different from Vanilla. But i guess Enterprise Businesses care more about ease of use and stickers they can put on their hardware than all the flexibility that comes with source based distros.

Ah, and i’d love to see you waiting for a patched binary from upstream when a fresh 0day hit the scene, yes, think about it, find a stupid reason why i am wrong, but you’ll know i am right. I’ll just up a number in my compilation-recipe, and i’m good to go.

Also, it’s always nice to have the same shitty software running EVERYWHERE, that makes it so much more secure! I can see a nice auto-rooter doing its job.

Seriously

If i hear any stupid dumbfuck telling me bullshit about source based distros, consider yourself outed as being unqualified for your job.

Maybe visit a gay club or something, get your rectal exam from a professional like you consider yourself to be, but don’t try to talk with the big boys, elm0 and i will get the shits and giggles so bad..

Why did i write this?

I am so fed up by pseudo-Admins telling me “oooh, source based is only for people administrating 2 servers”, the fucking truth is, on my peak i had to manage 600 of those motherfuckers myself, so please go and fuck yourself when try judging me. You don’t have the slightest idea what i am capable of.

EOD.

David Pollak's Lift Basics

Whew, the past few weeks were like a Rollercoaster ride between my personal downs and professional highs. I got even more high on Liftweb when i was voted Liftweb Committer almost two weeks back! So what’s coming next?

I am doing something truly amazing, something i would’ve never thought almost 2.5 years back when i first looked at Scala and Liftweb, because i will - buckle up - i will be co-teaching David Pollak’s Lift Basics with David at Skills Matter in London on March 15th. Yes, you can wipe your glasses all you want, the text won’t change!

So if you’re interested, be sure to get a seat while they’re available, because we will put the Fun in Functional Web-Programming. ;)

German

Puh, die letzten paar Wochen waren eine reine Achterbahnfahrt zwischen meinen persönlichen Tiefen und meinen professionellen Höhen. Die Höhen wurden noch heftiger, als ich vor knapp zwei Wochen zum Liftweb Committer gewählt wurde. Was kommt als nächstes?

Ich werde was richtig spannendes tun, etwas wovon ich vor 2.5 Jahren, als ich mit Scala und Liftweb mal rumgespielt habe, nie gedacht habe. Ich werde - gut festhalten - Ich werde David Pollak’s Lift Basics zusammen mit David bei Skills Matter in London am 15. März 2012 unterrichten! Jaha, ihr habt richtig gelesen, da könnt ihr die Brillen putzen wie ihr wollt, der Text ändert sich nicht!

Wenn ihr also Interesse habt, dann stellt sicher dass ihr euch einen Sitz holt, solange es die noch gibt!

Liftweb Error Reports by Mail

Having some kind of safety net when something goes wrong is never bad, so today my ass got saved my just a few lines of code (which made this thing much more easier).

So imagine you have a complex actor doing important business calculations (like invoicing), and imagine this one failing at 3 o’clock in the morning. How would you know?

Do you always monitor your error logs? If you go by the let-it-fail principle for example for unauthenticated users, your logs get pretty cluttered after some time. Apply that to a few scenarios where unwanted input or stuff produces stack traces, and you are looking at an unimaginable amount of stacktraces to look through.

So i’ve written just the simples piece of code i want to share today, since it saved my ass tonight ;)

If you’ve setup the mailer correctly in your Liftweb-Project, then lines as simple as these could save you a huge amount of trial and error, since you can already define what the subject and message should be.

As i’ve already said, it’s not much, but it’s often those little pieces that will save you time and headache.

Liftweb Bootstrap - a nice Base-Project

Happy new year!

Yes, you’ve heard right. It’s been that long since i’ve blogged! Anyway, i didn’t spend my time scratching my balls, i used it wisely - yes imagine that for a change. ;)

So what’s the 411 you might ask..

Lift-Bootstrap and Lift-BHtml

What it is? BHtml is a nice wrapper for MappedField and SHtml. It also does Mapper’s validation and produces nice Popovers with Twitter’s amazing Bootstrap Kit.

So what was i unhappy about Liftweb’s Helpers and things? Short answer: Nothing! It has been perfect all the way. It’s flexible, robust and not nearly as much code as you’d expect. So why did it need wrapping?

This is still too much code, also it has 0 validation in the form of feedback. Now since i am one lazy ass guy, i’ve written myself these BHtml wrappers to make it even more comfortable.

And yes, i am not that lazy, so i wrote a few ways to use it:

This will make the onBlur set the field in the Object, it will not auto-save the record if this field changes (so you can make a separate save-button), and it will display validation-errors on Blur as well as on Submit using a Twitter BootstrapJS Popover (3rd example on the Page).

Now don’t be amazed, that wasn’t even half of it! But i won’t dig any deeper into BHtml, since it is Open Source and for everyone to explore at my Github Repo named Lift-BHtml.

I even spent my Sunday morning writing a ScalaDoc API, so at least be sure to check it out before dismissing it. ;)

Buckle Up!

So i hear you’ve got another big thing going? Yes, you’ve heard (or read) right. It is something called Lift-Bootstrap and it provides a basic project with User- and Customer-Models as well as basic Twitter Bootstrap integration for validation. Yes, it sounds heavy and much, but it really is not. Once you get the hang of it, it’s all easy peasy ;)

The basic idea behind this was, that i sometimes want to draw up a rough project over night, not having to worry about Lift’s very unique and basic default layout, and having some guideline on how to style my forms and objects. Getting this basic rough draft is often driving me nuts, since i often just want to copy most of the view-code (HTML) and change a few bits on the form and be done with it. So Twitter Bootstrap gives me a good approach on how to clearly handly those things - but that’s just me.

Yes, i know you’re eager to check it out, so i won’t be holding you back any longer. I proudly present, the Lift-Bootstrap Demo.

Simply Sign Up, there is no Listing of Users, just hypothetical Customers, and check out the Validation- and Notification-Feature.

It is not meant to be perfect! It was designed to get you up and running fast.

So where is the source-code? Right here on my Lift-Bootstrap Github Repo.

A few last thoughts:

  • the Sign Up page is being processed by ProtoUser, i didn’t debug into validation there, since i usually add my Users by hand for most things.
  • to disable User-Registration, simply uncomment the line in User.scala

That’s it for tonight

I will be improving this, since this is going to be my base for future projects.

Thanks again to David Pollak and the whole Liftweb Community!

n8!-

Apple is slipping, are you falling for it as well?

If you read my Twitter-feed, you’ll probably notice me ranting endlessly about Apple. Yes, those nice guys that sell iPods and Macs, they fuck everything up these days. But let me back up a little, since my story begins in 2004.

Linux

Back then i had my beloved Linux Workstation running Gentoo Linux. It was really sweet, everything worked the way i wanted, i’ve spent weeks configuring every little tool the way i wanted to, but then my Disk died. Well, if you buy a new HDD, you usually don’t think it will go south in the first 6 months (at least if it is in your workstation running 9hrs / day in the office).

After having restored most of the configs from my brain, i started to get the sense, that this is not right. Admittedly, my system really was cut to the hardware like a latex suite, but the days it took to get stuff almost to where they were before, it was a pain. After all, it isn’t the same anymore.. the system feels tainted and some configs still aren’t the 100% they were before.

In the end, i quit my job and didn’t care for that workstation anymore, since i was working from at home.

It all ended in my effort, to find me a OS which is easily restorable, or at least drop dead easy to get to the point you’ve had it before.

MacOS X 10.4

Worked like a charm, back then i had a hackintosh, but it worked really really nice. That’s when i decided to buy my first Mid-2007 MacPro. Nice!

Not even 10 days after i got that MacPro delivered, 10.5 was released.

MacOS X 10.5

Also, this one was quite nice! Never had any real troubles!

MacOS X 10.6

Seemed a bit slow on my (back then 2.5 year old MacPro, but it worked). But you know it, when you least expect it, something breaks… In my case the ATI broke (twice in that MacPro). After the 2nd time the Graphicsadapter got exchanged, i thought i’d get myself a GeForce for that Mac, but HA! No more where those came from. My Mac is too old and there are no other Adapters available than the ATI which keeps on breaking…

MacOS X 10.7

So 10.7 came out, and i immediately got myself a new MBP 15” since my old MacPro was really slow. Also the MacBook is nice if you have to go out to customers, since it wasn’t the slowest, even iOS Coding was possible, but still, the MacPro had to be replaced.. so i did.

The Journey ends

Ever since 10.7, Apple’s whole Quality Assurance seems to be either on Vacation or got silently fired.. NOTHING is working right anymore. Somehow that “featureset” of incompetence started swapping to iOS, especially iOS 5 is buggy like hell..

So since 10.7, i am having random kernel panics which force me to reboot on a daily basis.. Not forgetting to mention that those sudden reboots somehow seem to break Covers in my iTunes Library (AWESOME!) and invalidated one of my TrueCrypt Containers (EVEN BETTER!).

So if you are annoyed by me ranting about Apple, please consider that i’ve spend thousands of Euros on Apple Hardware, just being teased by fanboys “you must be doing something wrong”, i am sick and tired.

What i will never do again

Buy Apple Hardware. I’ll never ever ever do that again, it’s the biggest mistake a human can make. It’s crap, real crap. Not the hardware itself, but what they consider a “reliable” or “stable” OS, does not overlap with my definition.

I would understand it if my Hackintosh was rebooting all day, but my original 1.5 year old MacBook Pro acts EXACTLY the same way.

So how would you react, that whenever you are seriously working, you get a kernel panic bringing you COMPLETELY out of your flow? Would you go mad?
Would you want to kick your 30 inch Apple Cinema Display including your hardware off the table?

I know i want to.. but the question was, are you falling for their hipster crap as well? I know for certain that i won’t again.

Scala, Akka and Erlang Actor Benchmarks

Since i am currently intrigued by Erlang for a special project, i thought i look into Actor Performance. I found an interesting Post by Paul Keeble comparing Erlang with Scala Actors. I also found an Akka-Benchmark, but it was outdated and i thought i give 2.0-SNAPSHOT a go.

Here are my results:

Erlang

Erlang R14B04 (erts-5.8.5) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe]

Eshell V5.8.5  (abort with ^G)
1> client:runTest(12000000)
Count is {ok,1200000000}
Test took 26.506394 seconds
Throughput=452720.95480056625 per sec
ok
2> client:runTest2(12000000).
Count is 1200000000
Test took 11.322672 seconds
Throughput=1059820.5087986297 per sec
ok

Roughly about 1 million messages per second, not bad!

Scala and Akka

Starting Akka...
Running Akka 2.0-SNAPSHOT
Deploying file:/usr/local/akka/deploy/scalavserlang_2.9.1-1.0.jar
[INFO] [12/25/2011 17:49:54.560] [main] [ActorSystem] REMOTE: RemoteServerStarted@akka://AkkaTest@127.0.0.1:2552
[INFO] [12/25/2011 17:49:54.572] [main] [Remote] Starting remote server on [akka://AkkaTest@127.0.0.1:2552]
Starting up ag.bett.scala.test.akka.AkkaActorKernel
[akka] Count is 1200000000
[akka] Test took 5.596 seconds
[akka] Throughput=2144388.849177984 per sec
--
[scala] Count is 1200000000
[scala] Test took 21.696 seconds
[scala] Throughput=553097.3451327434 per sec

WOW! Look at that result! Akka made 2.1 Million per second. Looks like Akka 2.0 seems to be a keeper!

I am looking forward to using Akka 2.0 together with my favourite Web-Framework Liftweb

UPDATE LiftActor

Since this post has gotten very much attention, i thought for completeness, i’ll add LiftActor to the Benchmark. It performs still better than the Scala Actors, but not as good as Akka. They’re close to Erlang i’d say.

Starting Akka...
Running Akka 2.0-SNAPSHOT
Deploying file:/usr/local/akka-2.0-M1/deploy/scalavserlang_2.9.1-1.0.jar
Starting up ag.bett.scala.test.lift.ActorKernel
[lift] Count is 1200000000
[lift] Test took 13.009 seconds
[lift] Throughput=922438.3119378891 per sec

Do it yourself

Since i tested it on one of my VPS with 2GB RAM, i am quite sure that there is a lot more behind Akka where that result came from. So if you want, give it a try. I’ve forked the original repository of Paul Keeble into my own.

You can find everything in the designated GitHub repo, i’ve taken the liberty of packaging akka-2.0-SNAPSHOT jars together with sbt for easy compilation/running.

Since you might be using a different git revision than i do (e160179aa91c54284c301a7bea64f96c681f3610), you might want to replace those jars.

[UPDATE] I also updated the Repo so it has an AkkaKernel for each Test-Actor.

Simply copy the .jar to your akka’s deploy/ and run:

./bin/akka ag.bett.scala.test.akka.ActorKernel
./bin/akka ag.bett.scala.test.lscala.ActorKernel
./bin/akka ag.bett.scala.test.lift.ActorKernel

Thanks to everyone at Typesafe and Akka for this present!

Have fun and merry christmas!

Akka/Scala together with Wowza Media Server

So my first Open-Source-Community-Christmas-Present (or OSCCP) is for the Akka and Wowza-Community.

Yesterday i found a nice way to use Scala inside the Wowza Media Server. It’s a Streaming-Server like Red5, but it works better. It can handle Live-Streams, Pre-recorded Content and all those kinds of things, haven’t yet had the chance read the full docs, but as far as my buddy elm0 told me, this is the way to go in regards to flash video crapshoes.

So what does it do?

The Wowza Media Server (as already said) is an alternative to Red5. It is capable of coders writing extensions/callback-Handlers in java, and drop those .jars into the lib-folder of it.

This is in my opinion a very nice way of letting coders decide what the backend should allow/reject, and it also allows for a much more fine grained way to connect multiple of those servers to a “Master”/Coordination-Node and keep each user-session managed.

Lucky for us Scala folks, we can simply drop our own jars in there, and even better, we can use Akka to build a Remote-Connection to our Master- or Frontend-Server.

Since today i don’t feel like much copy and pasting, (plus i wanna have some more fun with Wowza and Akka), i’ll simply redirect you to the code. ;)

May the source be with you

The project can be found on GitHub, i also removed the advertisement clause from the BSD-license for this project, old habits prevail much too often.

Thanks

To my very best friend elm0 who got me into Wowza last night, and also thanks to mikukopteff who’s Github Project Streamlet inspired me to do this.

Happy Christmas

Imagine...

You have a successful company, employ a handfull of people, make enough money to not have to worry about where your food is coming from and how you’re going to pay your rent. Yes, it sounds nice.

But then suddenly your grandma dies, your beloved grandma no one ever could replace. Imagine a warm and loving grandma who loves to cook all day and take long walks with her dog. So that one person you could always turn to who would always have something meaningful AND helpful to say is gone. But she lived half way around the world, so what do you do?

Since money is not an issue, you feel your duty calling and book the next trip to get to your grandmother’s funeral without hesitation. Imagine your best friend saying “hey dude, i’ve taken 2 weeks vacation, you wanna go to hawaii?”, wouldn’t that be awesome?

So a few months pass by, and you decide to take a trip to spain to attend your buddy’s birthday party. While you’re having the time of your life there, you meet that one person. That one person in 7 billion people! Possibly you see parts of your grandmother in her, her attitude towards life, her cooking, what she likes to eat and drink. But she lives 300km away, but okay, you should be able to manage that, you think. You’re positive! Energized!

When you get back home, you catch on the tv-ticker that your biggest client just went bankrupt, and that you probably gonna have to close your shop. This is bad news! But you pull through the whole ordeal, put a lot of money in it so you don’t have to declare Chapter 11, dust yourself off and start fresh!

So you reset your life, start a whole new company and forget the past, looking at the future with your new “girlfriend” ahead. Imagine yourself dreaming of that person all day, drifting away, asking your self what she might be doing, but i guess you know how that feels.

Imagine yourself being apart from the person you love, being forbidden by that person to move there, not knowing what’s happening in that persons life. What would you do?

You get angry, with everything, with everyone except that one person, even more than when you closed your company, even worse than when you’ve had to let all your employees go, and for the worst of all, it makes you even more mad than when your grandma died. Imagine being so angry, that you are unable to think clearly, being in rage all day every day, is this worth living?

But you find the strength, day after day, month after month, year after year, to stay that one person whom you might see once every other month, faithful. Imagine you having to work all day every day, since you cannot enjoy free time. Imagine forcing yourself to work relentlessly despite the psychological pain that is in you. Spare time is being considered punishment since your thoughts only drift around that one in 7 billion, so you work, day in and day out. Your sleep cycle gets fucked up, you don’t eat anymore, or at least barely. And again i ask you, what would you do?

After doing this for over two years, you simply cannot deal with it anymore, you break down. You don’t know what to do, where to go to make it better. Still, being unable to get to the one person that you love the most, whom you would give the world if you had it, it drives you bad crap crazy.

At last, you’ve suffocated that one person with your need for basic human interaction, so you will get your keys to your apartment back, which have been used once a year, you have christmas presents for that one person laying around which you hoped to gift personally (rather than DHL), because you didn’t know if you were going to see that person before christmas again or not.

When you find that one person that connects you to the world, you become someone different, someone better. When that person is taken from you, what do you become then?