[Pharo-project] One Liners to Impress Your Friends
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-Frie... 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
I like the idea. Note that Davide Varvello did a post on his blog about Smalltalk one liners last june. http://varvello.blogspot.com/2011/06/test.html Noury -- http://twitter.com/#!/NouryBouraqadi On 6 oct. 2011, at 11:59, 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-Frie...
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
I didn't see that post, but he's implementing the same examples ;-) I think it would be cool to come up with a nice list and put that on the Pharo website. On 06 Oct 2011, at 14:28, Noury Bouraqadi wrote:
I like the idea.
Note that Davide Varvello did a post on his blog about Smalltalk one liners last june. http://varvello.blogspot.com/2011/06/test.html
Noury -- http://twitter.com/#!/NouryBouraqadi
On 6 oct. 2011, at 11:59, 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-Frie...
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
Sven Van Caekenberghe wrote:
I didn't see that post, but he's implementing the same examples ;-)
I think it would be cool to come up with a nice list and put that on the Pharo website.
Hi Sven, The post is here: http://varvello.blogspot.com/2011/06/test.html http://varvello.blogspot.com/2011/06/test.html and the examples are not really the same, you can also see a couple of bonuses at the bottom of the post. Cheers Davide ---- Blog: http://varvello.blogspot.com http://varvello.blogspot.com -- View this message in context: http://forum.world.st/One-Liners-to-Impress-Your-Friends-tp3877615p3878414.h... Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
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-Frie...
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 |
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
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 Thu, Oct 6, 2011 at 4:58 PM, Levente Uzonyi <leves@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-Frie...>
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
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
participants (5)
-
Davide Varvello -
Levente Uzonyi -
Noury Bouraqadi -
Peter Hugosson-Miller -
Sven Van Caekenberghe