Thursday, June 23, 2011

...Change No.1

Ah-ha, here we are! Welcome again.
As you can see, you can find all my "old" posts here. I hope you like this new home.
 Stay tuned.

Wednesday, June 22, 2011

10 Smalltalk One Liners to Impress Your Friends

Some days ago, Antonio posted a list of 10 Ruby one-liners. The 1-liners saga started with

  Scala,

    then

      CoffeeScript,

       Haskell,

         Clojure,

           Python,

             Groovy

Very good, everyone wants to impress his friends, perhaps She could impress your friends more. Anyway, the grandaddy of programming languages, Smalltalk, is alive even if its voice is weak.

So, let's start

Multiply each item in a list by 2
(1 to: 10) collect:  [:each | each * 2]


Sum a list of numbers
(1 to: 1000) inject: 0 into: [:sum :each | sum + each]


Verify if tokens exist in a string
words := {'smalltalk'. 'akka'. 'play framework'. 'sbt'. 'typesafe'}.
tweet := 'This is an example tweet talking about smalltalk and sbt'.
words anySatisfy: [:each | tweet includesSubString: each]

but Collection>>detect: is more speaking and you can have the first occurrence back

words := {'smalltalk'. 'akka'. 'play framework'. 'sbt'. 'typesafe'}.
tweet := 'This is an example tweet talking about smalltalk and sbt'.
words detect: [:each |tweet includesSubString: each]


Reading a file
(FileStream fileNamed:'test.txt') contents.
Wonderful.


Happy Birthday
Transcript show:'Happy Birthday to You\Happy Birthday to You\Happy Birthday dear Davide\Happy Birthday to You' withCRs
Less geeky than the Scala ex., but more simple and communicative ;-)


Filter a list of numbers
#(49 58 76 82 88 90) groupedBy: [:n| n > 60] 


Fetch and parse an XML web service
XMLDOMParser parseDocumentFrom:(HTTPSocket httpGet:'search.twitter.com/search.atom?&q=smalltalk')



Find minimum (or maximum) in a list
#(49 58 76 123 82 88 90 -3) max
#(49 58 76 123 82 88 90 -3) min


Parallel Processing
Hmm, there isn't an easy way, you should use a specific VM such as:
Polycephaly or RoarVM, but I have no direct experience on them


Sieve of Eratosthenes
sieve := Array new: aNumber withAll: true. 
sieve at: 1 put: false. 
2 to: aNumber do: [:i | (sieve at: i) ifTrue: [2*i to: aNumber by: i do: [:k | sieve at: k put: false] ] 

That's a snippet I posted some years ago. It's not a one liner, but like Antonio I think it's readable.


First Bonus! - Size of running objects
Array allInstances size 
You can see, it's easy to find all instances of an object and calculate their total size.


Second Bonus! - Fast code cleanup
SystemNavigation new allUnsentMessages
Calling allUnsentMessages you can find all methods none call. Sigh, they are very very sad, nobody is interested in them, you should clean up your code :-)

See you next time!

Tuesday, June 21, 2011

You are an innovator

Are you doing the very same things, every day in your life? And you feel you are stuck like a fly in a spider's web.

Probably you are not alone!

But if you are so obsessed to find a way to get rid off them, so, they'll call you innovator.

Reuse, Reduce, Recycle...again

Ok fine, it was an intense week folks, the team struggled with a lot of legacy code (arghh) . Anyway, do you remember Up With People, Reuse Reduce Recycle about two posts ago? Good, I'm going to tell you a story:
Let's suppose you want to go to New York City (are you living there? Ok, stop reading this post, go out, jog in central park, eat something at Bubba Gump etc. etc. etc.).
Let's move on, you are not living in NYC, not in Brooklyn, Queens... but far from Manhattan and you are looking for a flight from your hometown to The Big Apple, so you browse some flight search engines.
One more hint: you are not someone who likes to socialize, to chit-chat, to talk about the weather, you are not human you are... a programmer! Are you living in NYC and you are human? Stop reading this post, go out, jog in...
As you are a programmer, you want to know some of the internalities of flights and how data is transferred from server to client. Finally during your adventure as a novice Sherlock you find at 85%... hmm at 91%... hmm at 97.49% an: XML.

When I find an XML, I always think to myself: I could merrily develop with my favorite language (Smalltalk you guess), why should I face with XML, the Bernie Madoff of languages, something that robs semantic from riches (Smalltalk) to put it in the trash?

In this context you can have something like:

<route>
      <flights>
            <flight>
                  <flightNumber>123456</flightNumber>
                  <departure>Milan MXP</departure>
                  <departureTime>10:30 AM 20110101</departureTime>
                  <arrival>New York City JFK</arrival>
                  <arrivalTime>12:30 PM 20110101</arrivalTime>
            </flight>
          .
          .
      </flights>
</route>

That annoying structure is multiplied for thousands of flights and I don't want to mention: fares, meals, taxes, types of airplanes and so on. You can easily imagine how this xml can be cumbersome.
What can be useful is a mechanism to take a root object (Route) on the server, packing it with all its attributes, transferring it on a client and restoring it there. Many languages support this mechanism: it's called serialization, in Smalltalk you can use a special object: SmartRefStream to easily serialize any object tree.
Ok... ok... I can hear your objections: "XML is a serialization format", "XML provides interoperability" I know, but SmartRefStream is there, smiling at you, there is no need to invent another fashionable format if I can use an effective one, we don't need interoperability, we need operability! We want to use and reuse it, we want to reduce the complexity, not to introduce a new format. But you know I'm not human, I'm a programmer, but not a programmer, I'm a smalltalker.
See you folks.

Is Obama a software guy?

Last week, with the end of Osama Bin Laden, President Obama ended a tangent started ten years ago. It was an expensive digression, but now, in some way, it's a full stop. I don't know what is the involvement of Mr.President in software, even if his campaign had some contributions by agile people, but we, as software developers, should learn from that lesson: to commit implementing a feature and delivering it. It's not a software specific issue, it concerns every professional that wants to be reliable: to promise and to keep the promise.

Reuse, Reduce, Recycle

A long time ago I hosted some guys of Up With People for about a week, they arranged a lovely show in my hometown dancing, singing... we had a lot of fun.
I mind a song named "Reuse, Reduce, Recycle" actually I can't remember the lyrics, but at that time, the song, favoured by a nice riff, stuck in my mind for a while.

I'm not here to advertise on how to be green, but to reflect on software building.
How many times did you think that to implement an HTML page was only a matters of minutes? Unfortunately, you spent one very single day fighting with CSSes and browsers and when you thought you were done, a colleague showed you that increasing fonts the layout was compromised. Arrrgggg!
Or... you are a JS warrior and you want to use that very nice feature of JQuery to impress the customer that is behind you looking at the screen of your pc. Unfortunately the version of JQuery you have in production is a couple of years old and there is no trace of that nice feature you love (of course, a multitude of other services are using that old, but running version of JQuery... and you want to change it? Brrrrr).

That's a standard scenario, but let the UWP guys give us a hand.
How? Next time folks :-)

My Truth, Your Truth

From time to time I see some buzz on "my truth", "your truth", "their truth". Language evolves, but in some cases, meanings are deliberately distorted to undermine the power of words. It does not exist my truth or your truth only my opinion or your opinion.


That's my opinion.

You are wonderful!

Because you are the reader of my blog ;-)