Tuesday, August 23, 2011 2:15 PM | rahel luethy | 0 comment(s)
Generating matrix-shaped test data is easy in any spreadsheet application: Enter a few cell values and use the fill handle to complete all others. But firing up a spreadsheet somehow feels lame. This Scala one-liner can do the same: println(Array.fill(7, 3)("X").map(_.mkString("\t")).mkString("\n"))
Is it readable? No. Is it handy? Yes!
Friday, April 22, 2011 10:21 PM | rahel luethy | 2 comment(s)
Just a quick one this time (it's easter holiday and the weather is simply too beautiful to waste time in front of a computer). Anyway, have a look at this constructor call: MigrosEasterEgg(GREEN, BLUE)
Unless you're Swiss and just know that I'm referring to the "Extra" variant, wouldn't it be a lot less ambiguous to write val aMigrosEgg = MigrosEasterEgg(dotColor = GREEN, bgColor = BLUE) As of Scala 2.8 you can name your arguments. You can also leave out those which have default values, shuffle their order — or you can simply have another one of those little suckers: val notAMigrosEgg = aMigrosEgg.copy(dotColor = PINK) Happy easter everyone!
Thursday, April 14, 2011 8:43 PM | rahel luethy | 3 comment(s)
As promised, here is the second post in my haphazardly thrown together Scala series. Over the past few days I have read some chapters in Odersky's Programming in Scala, and most importantly, I have written my first ~100 lines of code (a basic Minesweeper app with a simple Swing UI). It's been a lot of fun, and I am still very enthusiastic about many new concepts & constructs. When coding, the biggest challenge is to not fall back to imperative Java style, but really use the functional concepts wherever applicable. I am probably still not radical enough, but for the time being, here is my take on revealing non-mined cells: In summary: 1 usage of => (and even a recursion ;-)).
Friday, April 08, 2011 9:01 AM | rahel luethy | 2 comment(s)
This article finally did it: "Guardian.co.uk Switching from Java to Scala" — it made me jump head first into the Scala newbie pool. Of course I wanted to learn Scala for quite some time already, but I somehow never managed to get my act together. Life as a Java programmer had gotten harder over the last few years. Not that I don't enjoy my job anymore, to the contrary, but reading about all these highly dynamic youngsters hacking together their Python/Ruby/Groovy wizardy at the speed of light has made me feel even older than I already am. Old-fashionedness at its finest: I absolutely can't relate to all this enthusiasm for dynamic languages. Ok, I admit that some script-fu will certainly help getting this nasty little problem solved quickly, but how the heck will a huge Rails project be maintainable over 10+ years? Or do we still have to wait longer for time to tell that it will fail? Do these people write tests for each and every thing that otherwise would be checked by a compiler? Isn't this a rather high price for a little duck-typing? I am obviously being cynical, but luckily I am not the only one in doubt. Some great brains also think that throwing away the type system is not the best compromise to make in order to get rid of Java's (admittedly extensive) boilerplate. In this excellent talk, Bill Venners speaks about his preference for typed languages. He mentions "deterministic refactoring" as the main benefit, which he explains like this: I don't like types because I think they proof my program correct [...] but because they can proof changes that I make to my programs are correct Coming back to Scala: It seems to combine the best of all worlds — strictly typed, functional & object-oriented, radically concise, and just close enough to the Java world for old-timers to transition gradually ;-) To keep my new Scala heart beating, I will start posting weekly Scala gems. Let me start with the example given in the above interview — just to illustrate what "no boilerplate" and "concise" really mean: Java: public class HowdyClass {
private String name;
public HowdyClass(String name) {
this.name = name;
}
public String sayHowdy() {
return "Howdy " + name;
}
}and in Scala:
class HowdyClass(name: String) {
def sayHowdy = "Howdy " + name
}The second example is my personal highlight after roughly two hours of playing around with the interpreter: Initializing a two-dimensional grid with indexed cells:
class Cell(x: Int, y: Int)
Seq.tabulate(3, 9)(new Cell(_, _))
This is actually almost too concise (or at least as a functional noob it took me quite some time to understand it). Here's a verbose equivalent which is a bit more self explanatory:
Seq.tabulate(3, 9)((x: Int, y: Int) => new Cell(x, y))
And now go get it and have a lot of fun!
Thursday, March 17, 2011 8:27 AM | rahel luethy | 0 comment(s)
Unless you are a proud owner of a Photoshop license (or a not-so-proud owner of a PS crack), GIMP is probably your best bet when it comes to professional image editing software. It works on all platforms, the OS X version can be downloaded here. GIMP uses a multi-window layout. On the Mac, it requires Apple's X11 window environment (which explains, why your menu bar will say "X11" once you launch GIMP). Unfortunately, X11 has a very annoying behavior by default: If you click on an inactive window, the click will just focus that window. Say you want to select a different brush from the "Toolbox" window, this requires two clicks: One to activate the window, another to select the brush — for me, this made GIMP unusable on the Mac at first. Luckily, I stumbled across a hidden X11 preference which does the trick: 
After this is ticked, GIMP & X11 cooperate just as you would expect them to!
Tuesday, March 08, 2011 9:37 PM | rahel luethy | 3 comment(s)
(Original uf Schwiizerdütsch wiiter unde) Based on a recent Reddit AmA (Ask Me Anything) by a 4-year-old, I asked my kids some of the top-ranked questions — here are Filipa's (5) and Juan's (3) answers:
Compared to one year ago: What do you know now that you wish you knew back then?
Jumping from a rolling scooter. No, tying my shoes.
Ferrari.
What's your favorite color?
Yellow.
Red.
How many roads must a man walk down before you call him a man?
A boy: 3, a man: 6.
20.
How do earthquakes happen?
If big plates, bigger than countries, below the soil and below road gravel, if these plates collide, then that causes quakes.
Like this: 'Mäh, mäh, galagalä'.
What is best in life?
Having a lot of money.
DJ Bobo.
If you had a whole day to do whatever you wanted, anywhere in the world, how would you spend it?
Going to Africa, checking out what kind of people there are and how they speak.
Going to Africa. Poop!
What's the scariest thing ever?
A lion.
A jaguar.
What's your favorite thing to do when you're outside?
Rocking on the swing.
Playing in the sandbox.
What's the first thing you think of when you wake up?
That it's morning now and that there will be breakfast.
That I want to have breakfast.
Do you have any tips for talking to boys/girls?
Telling them what stupid things one could do.
Babachalachala.
What's a question you would like me to ask you?
Where do dwarfs come from?
Pencil.
And what would be your answer?
From the mountains, from caves.
Lamp.
What do you think is the favorite thing for adults to do?
Drinking coffee and having a chat.
Working.
Do you remember being born?
Yes.
Yes, I do.
Why does it become dark at night? Where does the sun go?
The sun is on the other side of earth. It's becoming dark because it is no longer here.
—
What do you like more, Jonas or Ice Cream?
Ice Cream. That's a funny question...
Ice Cream. Yummy!
What is the biggest number you can think of?
199 Billions. Is this bigger than 10 Trillions?
2. No, behind-a-thousand.
What would make the world a better place?
Less bad air and more lichen.
Africa.
What is the meaning of life?
This question is too difficult for me.
Computer.
What would you do with a million dollars?
Saving for when I don't have enough food anymore.
Buying dessert.
Schwiizertütsch: Basierend uf emne chürzleche Reddit AmA ("Frog mi irgend öppis") wo e 4-Jährige gmacht het, hani es paar vo de Frooge mine Chind gstellt — do sind d'Antworte vo de Filipa (5) und em Juan (3):
Vergliche mit vor emne Johr: Was chasch du jetzt, und s'wär guet gsii, wenn du's scho frühner hätsch chönne?
Vom fahrende Trotti abspringe. Nei, Schueh binde.
Ferrari.
Was isch dini Lieblingsfarb?
Gäl.
Rot.
Wie vil Stroosse muess a Maa abelaufe, bevor me seit er isch a Maa und nümm e Bueb?
E Bueb: 3, e Maa: 6.
20.
Wie git's Ärdbebe?
Wenn grossi Platte, grösser als Länder, unter de Erde und unter em Chiis vo de Stroosse zäme tätsche, denn git's Ärdbebe.
Eso: Mäh, mäh, galagalä.
Was isch s'Besche im Läbe?
Dass me vil Gäld het.
DJ Bobo.
Wenn du e ganze Tag chönntsch mach was du wetsch, irgendwo uf de Wält, was würdsch mache?
Mol uf Afrika go luege was es dört für Mensche git, und wie sie det reede.
Uf Afrika. Gaggi!
Was isch s'Gfürchigschte wo's git?
E Leu.
E Jaguar
Was machsch am liebschte, wenn du dusse bisch?
Riitiseile.
Sändele.
Was isch s'erschte wo du am Morge dänksch, wenn du ufwachsch?
Dass jetzt Morge isch und dass es jetzt Zmorge git.
Zmorge ässe.
Häsch irgend e Tipp, was me met Buebe (Meitli) chönnt rede?
Säge was mer für blöde Seich wet mache.
Babachalachala.
Was wär e cooli Froog won ich dir chönnt stelle?
Wohär d'Zwärge chömme.
Bleistift.
Und was wär dini Antwort?
Vo de Bärge. Us Höhline.
Lampe.
Was dänksch, was die Erwachsene am Liebschte mache?
Kaffi trinke und mitenand schwätze.
Schaffe.
Chasch du dich dra erinnere, gebore z'wärde?
Jo.
Jo, chan ich.
Warum wird's in dr Nacht dunkel, und wo goht denn d'Sunne aane?
D'Sunne isch denn uf de andere Siite vo de Erde. Und s'wird dunkel, wil d'Sunne denn nümm döte isch.
—
Was hesch lieber, de Jonas, oder Glacé?
Glacé. Das isch denn e luschtigi Froog...
Glacé. Mega fein.
Was isch die gröschti Zahl wo du kennsch?
199 Milliarde. Isch das grösser als 10 Trilliarde?
2. Nei, Hindertuusig.
Was würd d'Wält besser mache?
Nid so vil blödi Luft und meh Flächte.
Afrika.
Was isch de Sinn vom Läbe?
Die Froog isch für mich e chli z'schwierig.
Computer.
Was würsch mit ere Million Franke mache?
Chli spaare für weni nümm gnueg Esse hät.
Dessert chaufe.
Monday, February 14, 2011 8:01 PM | rahel luethy | 0 comment(s)
These bars are motivated by my love for oatmeal & dates. The recipe is inspired by allrecipes.com, but is way easier to prepare, uses whole-grain flour, and does not include extra sugar:
180 g chopped dates
120 ml water
120 g quick-cooking oats
75 g butter
2 g salt
Mix all in non-sticking sauce pan, bring to boil, and cook until slightly thickened (stirring constantly).
If you go for healthy snacks, there is no need for extra sugar (dates are quite sweet already), otherwise just add maple syrup or brown sugar to your liking.
Remove pan from heat and finish the dough by adding:
1 egg white
125 g whole grain flour
2 g baking soda
Mix well, pat mixture into a baking dish, and bake at 180° for 20-25 minutes (or until lightly browned). Cool on a wire rack. Cut into bars.
Thursday, January 27, 2011 1:24 PM | rahel luethy | 0 comment(s)
From Wikipedia: "AppleScript was designed to be used as an accessible end-user scripting language, offering users an intelligent mechanism to control applications, and to access and modify data and documents." Yet another scripting language — but honestly, the fact that it is built into Mac OS (no installation) and was initially designed to automate repetitive tasks, makes the getting started quite easy & fun. Here's my problem (and how I managed to solve it with AppleScript in about 30 minutes): As you might know by now, I am a happy Quicksilver user and I sometimes take it as a challenge to do as much of my programming & office work via keyboard as possible. One task I regularly do throughout the day, is creating "Task" entries ;-) I do this via the "Mail" application, which eventually adds its entries to Exchange, so they are accessible from any machine/device. Long story short, adding tasks should be simple via Quicksilver, given that it can cope with textual input and pass it on to any AppleScript. Here's the todo.scpt that I came up with (aka structured procrastination):
Or in other words:
To try it out yourself, just store the script in ~/Library/Application Support/Quicksilver/Actions and invoke it as usual:
- Invoke Quicksilver and press period to go to text entry mode
- Enter your task (e.g. "Remember the milk")
- Hit tab and start typing "todo" until the script action shows
- Hit Enter and watch your task being created
I am confident that you can come up with even better AppleScript usages to waste the rest of your day ;-)
Wednesday, December 22, 2010 8:32 AM | rahel luethy | 2 comment(s)
After almost 10 years of professional software development, I had my share of Editor war, and I tend to avoid discussions around it as much as possible. My recent switch to the Mac however forced me to switch to a new editor (wouldn't have happend to neither emacs nor vi folks, would it?), and since the pre-installed TextEdit is really not an option, I spent 50 bucks on a TextMate license. Here are 3 reasons why I think it was totally worth it: Shallow Learning Curve I vividly remember my first encounter with vi: After my simple keystrokes totally messed up the file, I couldn't even figure out how to close the editor. Today, after way too many introductory tutorials, I know just enough to edit a line, save, and exit without further damage, but I still try to avoid vi like the plague. TextMate is just the opposite: It pretends to be a simple editor that just works, while actually it is very "extensible" and extremely "powerful" (my favorite word in editor-war-lingo). Its richness comes in customizable bundles, macros & commands (all extensively documented, so I won't repeat the basics here). What makes the learning curve shallow, is that you can conquer one after the other, just when they come in handy. To illustrate that further: I installed TextMate about 2 months ago, fired it up maybe 20 times, didn't read more than 2 pages of tutorials, but can already list two features that facilitate my programming life:
Command Execution The first great feature is running shell commands from the editor, and inserting results straight into the document: Type cal, press Ctrl-R (Text » Execute Line Inserting Result), and TextMate will insert this month's calendar in textual form. A simple one-liner can generate output for which you would otherwise need to fire up a terminal or even a spreadsheet application: ruby -e "3.times { puts rand }". Multi-Line Editing Just yesterday I had to reformat column-based test data (available in a spreadsheet) to pass it into the Java Arrays.asList(T... a) method. Essentially, I had to reformat values like these
into a comma separated array of strings like these My bash/awk/sed knowledge is really poor, so one simple way would have been a Find/Replace with regular expressions (to match the line beginning/ending). With TextMate's column editing support this is even simpler: Select the column, press Alt/Option (Edit » Change to Column Selection), and watch how the cursor now spans across all selected lines. In this mode, every keystroke is reflected in each line and it is thus easy to insert the quotes and comma at the beginning and end. Joining the string literals to one line is then a simple matter of another shortcut (Ctrl-Shift-J or Bundes » Text » Join Line With Next). Obviously, editing multiple lines is slightly more complicated when the lines have various length, but with Text » Edit Each Line in Selection this is a piece of cake as well.
Tuesday, November 23, 2010 2:42 PM | rahel luethy | 2 comment(s)
In contrast to standard Windows keyboards, the Mac one does not have a dedicated PrtScrn key. Taking a screenshot is thus a little bit trickier on the Mac, but the utility behind the scenes is also quite a bit smarter than its Windows companion. For the matter of completeness, the Command-Control-Shift-3 combination is the equivalent to the Windows key: it captures the complete screen and copies the PNG image to the clipboard. The service that processes this command is called SystemUIServer and it understands quite a few more shortcuts, all just modifications of the standard behavior. I spare you the details, but here are some goodies to play around with: Cross-hair Cursor As a side-effect of the Command-Shift-4 variant (screenshot of selected area) the mouse cursor changes to a cross-hair variant which displays the pixel-based screen coordinates. This is really handy during UI development, especially when combined with magic mouse zooming. Capturing Individual Windows While the cross-hair cursor is visible, pressing the Space bar will turn it into a little camera cursor which then allows capturing only the focused window. This is quite similar to the Alt-PrtScrn combination on Windows, except that you can actually move the (blue) selection at your will and only trigger the shot once you're ready. Preview/Terminal All commands mentioned so far either put the captured image to clipboard or save it straight to your Desktop. If you regularly want to annotate or otherwise edit your screenshots before saving them, you may prefer to trigger them from the Preview application (File » Take Screen Shot). Finally, there's also a command-line utility called screencapture, but honestly, I've never used it so far.
|