[Pharo-project] One Liners to Impress Your Friends

Peter Hugosson-Miller oldmanlink at gmail.com
Thu Oct 6 11:06:24 EDT 2011


On Thu, Oct 6, 2011 at 4:58 PM, Levente Uzonyi <leves at elte.hu> wrote:

> On Thu, 6 Oct 2011, Sven Van Caekenberghe wrote:
>
>  After reading these two post
>>
>>        10 CoffeeScript One Liners to Impress Your Friends
>>
>>                http://ricardo.cc/2011/06/02/**
>> 10-CoffeeScript-One-Liners-to-**Impress-Your-Friends.html<http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html>
>>
>>        10 Scala One Liners to Impress Your Friends
>>
>>                http://solog.co/scala/10-**scala-one-liners-to-impress-**
>> your-friends.html<http://solog.co/scala/10-scala-one-liners-to-impress-your-friends.html>
>>
>> I could not help but think why people get so excited about new languages
>> while Smalltalk can and has been doing this for decades. So here are
>> Smalltalk versions of their code (not that the examples are all that
>> impressive):
>>
>> "--- 1. Multiple Each Item in a List by 2 ---"
>>
>>        (1 to: 10) collect: [ :each | each * 2 ]
>>
>> "--- 2. Sum a List of Numbers ---"
>>
>>        (1 to: 1000) inject: 0 into: [ :sum :each | sum + each ]
>>
>>        (1 to: 1000) fold: [ :x :y | x + y ]
>>
>>        (1 to: 1000) reduce: [ :x :y | x + y ]
>>
>>        (1 to: 1000) sum
>>
>> "--- 3. Verify if Exists in a String ---"
>>
>>        | wordList tweet |
>>        wordList := #( 'Smalltalk' 'Object-Oriented' 'REST' 'TDD' 'Play
>> Framework' ).
>>        tweet := 'This is an example tweet talking about Smalltalk and
>> TDD'.
>>        wordList anySatisfy: [ :each | tweet includesSubString: each ]
>>
>> "--- 4. Read in a File ---"
>>
>>        (FileStream fileNamed: 'data.txt') contentsOfEntireFile
>>
>> "--- 5. Happy Birthday ---"
>>
>>        1 to: 4 do: [ :each | Transcript crShow: 'Happy Birthday ', (each =
>> 3 ifTrue: [ 'dear Mr. President' ] ifFalse: [ 'to You' ]) ]
>>
>> "--- 6. Filter list of numbers ---"
>>
>>        | passed failed |
>>        passed := OrderedCollection new.
>>        failed := OrderedCollection new.
>>        #(49 58 76 82 88 90) do: [ :each | (each > 60 ifTrue: [ passed ]
>> ifFalse: [ failed ] ) add: each ].
>>        { passed. failed }
>>
>>        #(49 58 76 82 88 90) groupedBy: [ :each | each > 60 ]
>>
>> "--- 7. Fetch and Parse an XML or JSON web service ---"
>>
>>        XMLDOMParser parse: (ZnNeoClient new get: '
>> http://search.twitter.com/**search.atom?&q=pharoproject<http://search.twitter.com/search.atom?&q=pharoproject>
>> ')
>>
>>        JSJsonParser parse: (ZnNeoClient new get: '
>> http://search.twitter.com/**search.json?&q=pharoproject<http://search.twitter.com/search.json?&q=pharoproject>
>> ')
>>
>> "--- 8. Find minimum (or maximum) in a List ---"
>>
>>        #(14 35 -7 46 98) fold: [ :x :y | x min: y ]
>>
>>        #(14 35 -7 46 98) min
>>
>>        #(14 35 -7 46 98) fold: [ :x :y | x max: y ]
>>
>>        #(14 35 -7 46 98) max
>>
>> "--- 9. Parallel Processing ---"
>>
>>        "No such standard support, but multithreading is of course
>> available"
>>
>> "--- 10. Sieve of Eratosthenes ---"
>>
>>        "Unreadable code is not a good idea, this is longer but readable"
>>
>>        | primes |
>>        primes := Array new: 100 withAll: true.
>>        primes at: 1 put: false.
>>        2 to: primes size sqrt ceiling do: [ :each |
>>
>
> You mean #sqrtFloor.
>
>
>                 (primes at: each) ifTrue: [
>>                        each * 2 to: primes size by: each do: [
>> :eachMultiple |
>>
>
> Eratostheneses would start from each * each.
>
>
>                                 primes at: eachMultiple put: false ] ] ].
>>        OrderedCollection streamContents: [ :stream |
>>
>
> Streaming into an OrderedCollection? Come on...
>
> Here's a version optimized for readibility & loc:
>
> Array streamContents: [ :primeStream |
>        | sieve |
>        sieve := Array new: 100 withAll: true.
>        2 to: sieve size do: [ :each |
>                (sieve at: each) ifTrue: [
>                        primeStream nextPut: each.
>                        each * each to: sieve size by: each do: [
> :eachMultiple |
>                                sieve at: eachMultiple put: false ] ] ] ].
>
>
> Levente



That, my friend, is a thing of beauty :-)

-- 
Cheers,
Peter


>                primes doWithIndex: [ :each :index |
>>                        each ifTrue: [ stream nextPut: index ] ] ]
>>
>>
>> Maybe we could turn this into a blog post after some discussion ?
>>
>> To really show off Smalltalk, we might need a couple more impressive
>> examples.
>>
>> Sven
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20111006/5c0d6dbf/attachment-0001.html>


More information about the Pharo-dev mailing list