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 10 Scala One Liners to Impress Your Friends 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') JSJsonParser parse: (ZnNeoClient new get: '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 | (primes at: each) ifTrue: [ each * 2 to: primes size by: each do: [ :eachMultiple | primes at: eachMultiple put: false ] ] ]. OrderedCollection streamContents: [ :stream | 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 |
On 06 Oct 2011, at 16:58, Levente Uzonyi wrote: > 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 ] ] ] ]. That is indeed a cooler version. Thanks for the feedback, Levente ! Sven |
Free forum by Nabble | Edit this page |