<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: On Simple Functional Idioms</title>
	<atom:link href="http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/feed/" rel="self" type="application/rss+xml" />
	<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/</link>
	<description>thoughts on Smalltalk and programming in general...</description>
	<pubDate>Fri, 16 May 2008 05:31:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Fabio</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4841</link>
		<dc:creator>Fabio</dc:creator>
		<pubDate>Fri, 24 Aug 2007 20:21:51 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4841</guid>
		<description>Got cut again ... I'll post on the squeak dev mailing list ...</description>
		<content:encoded><![CDATA[<p>Got cut again &#8230; I&#8217;ll post on the squeak dev mailing list &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabio</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4840</link>
		<dc:creator>Fabio</dc:creator>
		<pubDate>Fri, 24 Aug 2007 20:20:51 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4840</guid>
		<description>Sorry Ramon ... I couldn't finish the post there is some problem on the server ... I was explaining why I don't agree with ... “Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic” ... I got cut out buy the server 
:-(
I'll try to post it all again ... Then if I can't do it ... I'll ask my questions on the Squeak mlist

&#62;&#62; Actually, if you’re dying to work with collections so tersely....

I usually do stuff using lots lists, maps, sets,  ... instead of making complex hierarchies ... 
i do  .. filter here, than zip it, make a map, select a few xyz ... check if is in a set ... re-filter , remap dispatch to this and to that ... remap ... ... stuff like that ... 
  
and Smalltalk supports very well the kind of code I like to write ... because of it's collection protocol ... but there is a BETTER way of doing it ... which doesn't conflict at all with object orientation and even smalltalk keyword syntax. That is ... like this guy is doing ... http://www.fscript.org/documentation/OOPAL.pdf ... at least in my humble opinion.

The paper also explains ... what is the problem with this:
&#62;---------
.... just rig up your collection to send any message it doesn’t understand to collect by default, so your example would look like this…

(((({10. 20. 30. 40} add1) add1) add1) add1) add1.
----------
{10. 20. 30. 40} add1 &#124; #add1 
----&lt;I&gt;-------
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.
-----</description>
		<content:encoded><![CDATA[<p>Sorry Ramon &#8230; I couldn&#8217;t finish the post there is some problem on the server &#8230; I was explaining why I don&#8217;t agree with &#8230; “Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic” &#8230; I got cut out buy the server<br />
:-(<br />
I&#8217;ll try to post it all again &#8230; Then if I can&#8217;t do it &#8230; I&#8217;ll ask my questions on the Squeak mlist</p>
<p>&gt;&gt; Actually, if you’re dying to work with collections so tersely&#8230;.</p>
<p>I usually do stuff using lots lists, maps, sets,  &#8230; instead of making complex hierarchies &#8230;<br />
i do  .. filter here, than zip it, make a map, select a few xyz &#8230; check if is in a set &#8230; re-filter , remap dispatch to this and to that &#8230; remap &#8230; &#8230; stuff like that &#8230; </p>
<p>and Smalltalk supports very well the kind of code I like to write &#8230; because of it&#8217;s collection protocol &#8230; but there is a BETTER way of doing it &#8230; which doesn&#8217;t conflict at all with object orientation and even smalltalk keyword syntax. That is &#8230; like this guy is doing &#8230; <a href="http://www.fscript.org/documentation/OOPAL.pdf" rel="nofollow">http://www.fscript.org/documentation/OOPAL.pdf</a> &#8230; at least in my humble opinion.</p>
<p>The paper also explains &#8230; what is the problem with this:<br />
&gt;&#8212;&#8212;&#8212;<br />
&#8230;. just rig up your collection to send any message it doesn’t understand to collect by default, so your example would look like this…</p>
<p>(((({10. 20. 30. 40} add1) add1) add1) add1) add1.<br />
&#8212;&#8212;&#8212;-<br />
{10. 20. 30. 40} add1 | #add1<br />
&#8212;-<i>&#8212;&#8212;-<br />
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.<br />
&#8212;&#8211;</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4839</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Fri, 24 Aug 2007 20:11:36 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4839</guid>
		<description>It's not a matter of agreement, it's a matter of fact.  I agree that array oriented stuff would be cool, and is better, but the statement "Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic" is a true one.

I've read that paper before, very nice, but it's a proposal to merge the techniques into OO, it's not something that has been done and accepted to the point of being able to call it idiomatic.

I would however, like to see the adoption of such techniques as well.</description>
		<content:encoded><![CDATA[<p>It&#8217;s not a matter of agreement, it&#8217;s a matter of fact.  I agree that array oriented stuff would be cool, and is better, but the statement &#8220;Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic&#8221; is a true one.</p>
<p>I&#8217;ve read that paper before, very nice, but it&#8217;s a proposal to merge the techniques into OO, it&#8217;s not something that has been done and accepted to the point of being able to call it idiomatic.</p>
<p>I would however, like to see the adoption of such techniques as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Mitchell</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4838</link>
		<dc:creator>David Mitchell</dc:creator>
		<pubDate>Fri, 24 Aug 2007 19:50:25 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4838</guid>
		<description>@Fabio

This would make an excellent question for the Squeak-dev mailing list.</description>
		<content:encoded><![CDATA[<p>@Fabio</p>
<p>This would make an excellent question for the Squeak-dev mailing list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabio</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4837</link>
		<dc:creator>Fabio</dc:creator>
		<pubDate>Fri, 24 Aug 2007 19:48:41 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4837</guid>
		<description>(I can't get my full post on ...)

.... continues ....

I don't agree with ...
&#62;&#62;
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.
</description>
		<content:encoded><![CDATA[<p>(I can&#8217;t get my full post on &#8230;)</p>
<p>&#8230;. continues &#8230;.</p>
<p>I don&#8217;t agree with &#8230;<br />
&gt;&gt;<br />
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabio</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4836</link>
		<dc:creator>Fabio</dc:creator>
		<pubDate>Fri, 24 Aug 2007 19:43:28 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4836</guid>
		<description>&#62;&#62;&#62;&#62;&#62; Actually, if you’re dying to work with collections so tersely.... &#62;&#62;&#62;
.... just rig up your collection to send any message it doesn’t understand to collect by default, so your example would look like this…

(((({10. 20. 30. 40} add1) add1) add1) add1) add1.
&#62;&#62;&#62;&#62;
{10. 20. 30. 40} add1 &#124; #add1 
&#62;&#62;&#62;&#62;&#62;
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.
</description>
		<content:encoded><![CDATA[<p>&gt;&gt;&gt;&gt;&gt; Actually, if you’re dying to work with collections so tersely&#8230;. &gt;&gt;&gt;<br />
&#8230;. just rig up your collection to send any message it doesn’t understand to collect by default, so your example would look like this…</p>
<p>(((({10. 20. 30. 40} add1) add1) add1) add1) add1.<br />
&gt;&gt;&gt;&gt;<br />
{10. 20. 30. 40} add1 | #add1<br />
&gt;&gt;&gt;&gt;&gt;<br />
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabio</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4835</link>
		<dc:creator>Fabio</dc:creator>
		<pubDate>Fri, 24 Aug 2007 19:41:45 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4835</guid>
		<description>&#62;&#62; Actually, if you’re dying to work with collections so tersely....

I usually do stuff using lots lists, maps, sets,  ... instead of making complex hierarchies ... 
i do  .. filter here, than zip it, make a map, select a few xyz ... check if is in a set ... re-filter , remap dispatch to this and to that ... remap ... ... stuff like that ... 
  
and Smalltalk supports very well the kind of code I like to write ... because of it's collection protocol ... but there is a BETTER way of doing it ... which doesn't conflict at all with object orientation and even smalltalk keyword syntax. That is ... like this guy is doing ... http://www.fscript.org/documentation/OOPAL.pdf ... at least in my humble opinion.

The paper also explains ... what is the problem with this:
&#62;---------
.... just rig up your collection to send any message it doesn’t understand to collect by default, so your example would look like this…

(((({10. 20. 30. 40} add1) add1) add1) add1) add1.
----------
{10. 20. 30. 40} add1 &#124; #add1 
----&lt;I&gt;-------
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.
-----</description>
		<content:encoded><![CDATA[<p>&gt;&gt; Actually, if you’re dying to work with collections so tersely&#8230;.</p>
<p>I usually do stuff using lots lists, maps, sets,  &#8230; instead of making complex hierarchies &#8230;<br />
i do  .. filter here, than zip it, make a map, select a few xyz &#8230; check if is in a set &#8230; re-filter , remap dispatch to this and to that &#8230; remap &#8230; &#8230; stuff like that &#8230; </p>
<p>and Smalltalk supports very well the kind of code I like to write &#8230; because of it&#8217;s collection protocol &#8230; but there is a BETTER way of doing it &#8230; which doesn&#8217;t conflict at all with object orientation and even smalltalk keyword syntax. That is &#8230; like this guy is doing &#8230; <a href="http://www.fscript.org/documentation/OOPAL.pdf" rel="nofollow">http://www.fscript.org/documentation/OOPAL.pdf</a> &#8230; at least in my humble opinion.</p>
<p>The paper also explains &#8230; what is the problem with this:<br />
&gt;&#8212;&#8212;&#8212;<br />
&#8230;. just rig up your collection to send any message it doesn’t understand to collect by default, so your example would look like this…</p>
<p>(((({10. 20. 30. 40} add1) add1) add1) add1) add1.<br />
&#8212;&#8212;&#8212;-<br />
{10. 20. 30. 40} add1 | #add1<br />
&#8212;-<i>&#8212;&#8212;-<br />
However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn’t do it.<br />
&#8212;&#8211;</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4831</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Fri, 24 Aug 2007 18:31:18 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4831</guid>
		<description>Actually, if you're dying to work with collections so tersely, you just rig up your collection to send any message it doesn't understand to collect by default, so your example would look like this...

(((({10. 20. 30. 40} add1) add1) add1) add1) add1.

Perfectly valid Smalltalk.  You could get rid of the parens by defining a pipe on object like so

Object&gt;&gt;&#124; anArg 
	^ self perform: anArg

And then pipe symbols together like so...

{10. 20. 30. 40} add1 &#124; #add1 &#124; #add1 &#124; #add1 &#124; #add1

But you'd have to change the compiler to make the pipe work like ; without the symbols (I think).

However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn't do it.

You can override the default compiler and do whatever you like with the syntax, if you're brave.</description>
		<content:encoded><![CDATA[<p>Actually, if you&#8217;re dying to work with collections so tersely, you just rig up your collection to send any message it doesn&#8217;t understand to collect by default, so your example would look like this&#8230;</p>
<p>(((({10. 20. 30. 40} add1) add1) add1) add1) add1.</p>
<p>Perfectly valid Smalltalk.  You could get rid of the parens by defining a pipe on object like so</p>
<p>Object>>| anArg<br />
	^ self perform: anArg</p>
<p>And then pipe symbols together like so&#8230;</p>
<p>{10. 20. 30. 40} add1 | #add1 | #add1 | #add1 | #add1</p>
<p>But you&#8217;d have to change the compiler to make the pipe work like ; without the symbols (I think).</p>
<p>However, Smalltalk is an object oriented language, not an array oriented one, and none of this stuff would be considered idiomatic so I probably wouldn&#8217;t do it.</p>
<p>You can override the default compiler and do whatever you like with the syntax, if you&#8217;re brave.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fabio</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4824</link>
		<dc:creator>Fabio</dc:creator>
		<pubDate>Fri, 24 Aug 2007 15:01:28 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4824</guid>
		<description>Why smalltalk has no operator/keyword for piping ?

obj collect: [ :x &#124; ...] 
    &#124; filter: [:x &#124; ...] 
    &#124; select: [:x &#124; ...] 

?

but has... the ";" ?

I don't care to send messages to the reciever of the previous message, because I return self when no return value is important for example ....

point x: 10 
       &#124; y: 20
       &#124; z: 30

x returns self, y returns self, z returns self -&#62; which is the updated point.

works exactly as:
point x:10; y:20 ; z:30. 

But you lose PIPES !!!!! Can somebody elaborate on this ?

I don't like your idea of writing helper methods like ...

 employees
    select: [:emp &#124; emp role = #programmer ]
    collect: [:emp &#124; emp salary ]
    inject: 0 into: [:a :b &#124; a max: b ] 

What if I need to inject first, and another case where I have other seletes and injects not just 3 operations. I that is your raccomanded style you would need to write for me quite a lot of helpers ... but at least in smalltalk you CAN do it :-) .. but again I think this is a Smalltalk Issue.

Note that I'm working an a programming language that will have support for multiple dispatch, Array programming,  based heavily on Smalltalk and a Pipe&#38;Filters style...


What about this ?

Smalltalk ...

 employees
    select: [:emp &#124; emp role = #programmer ]
    collect: [:emp &#124; emp salary ]
    inject: 0 into: [:a :b &#124; a max: b ] 

employees $ (employees:role = `programmer) :salary :inject [\a b\ a :max b]

$ = selection ... takes an array on the left and an array of booleans on the right ... returns elements from the first array that have TRUE on the same index of the second array.

array :filter

sends all the objects in array to the filter process yielding an array of filtered objects.

Check F-Script on Google. Array programming rocks on collections. It has ALL smalltalk stuff PLUS message patterns. There is a paper there where they literally smash functional programming/lambas/blocks ... they have convinced mee that array programming is the right way to think about collections. Note that love functional programming. but array programming is better...

Again on smalltalk ...

Stupid example (please replace add1 wich ... YourComplexDifficultLongCollectionOperation)

{10. 20. 30. 40} :add1 :add1 :add1 :add1 :add1
&#62;&#62;{15. 25. 35. 45}

Compare with smalltalk ... 

Please I'm a smalltalk novice correct me If I'm wrong....

((((({10. 20. 30. 40} collect: [ :elem &#124; elem :add1]) collect: [ :elem &#124; elem :add1]) collect: [ :elem &#124; elem :add1]) collect: [ :elem &#124; elem :add1]) collect: [ :elem &#124; elem :add1])

I think that PIPES are more important than the cascade operator as most of the cases where cascading is helpfull have the previous message returning self !!!... there are exceptions but I'd like not to loose pipes for them.

Any comments ?</description>
		<content:encoded><![CDATA[<p>Why smalltalk has no operator/keyword for piping ?</p>
<p>obj collect: [ :x | ...]<br />
    | filter: [:x | ...]<br />
    | select: [:x | ...] </p>
<p>?</p>
<p>but has&#8230; the &#8220;;&#8221; ?</p>
<p>I don&#8217;t care to send messages to the reciever of the previous message, because I return self when no return value is important for example &#8230;.</p>
<p>point x: 10<br />
       | y: 20<br />
       | z: 30</p>
<p>x returns self, y returns self, z returns self -&gt; which is the updated point.</p>
<p>works exactly as:<br />
point x:10; y:20 ; z:30. </p>
<p>But you lose PIPES !!!!! Can somebody elaborate on this ?</p>
<p>I don&#8217;t like your idea of writing helper methods like &#8230;</p>
<p> employees<br />
    select: [:emp | emp role = #programmer ]<br />
    collect: [:emp | emp salary ]<br />
    inject: 0 into: [:a :b | a max: b ] </p>
<p>What if I need to inject first, and another case where I have other seletes and injects not just 3 operations. I that is your raccomanded style you would need to write for me quite a lot of helpers &#8230; but at least in smalltalk you CAN do it :-) .. but again I think this is a Smalltalk Issue.</p>
<p>Note that I&#8217;m working an a programming language that will have support for multiple dispatch, Array programming,  based heavily on Smalltalk and a Pipe&amp;Filters style&#8230;</p>
<p>What about this ?</p>
<p>Smalltalk &#8230;</p>
<p> employees<br />
    select: [:emp | emp role = #programmer ]<br />
    collect: [:emp | emp salary ]<br />
    inject: 0 into: [:a :b | a max: b ] </p>
<p>employees $ (employees:role = `programmer) :salary :inject [\a b\ a :max b]</p>
<p>$ = selection &#8230; takes an array on the left and an array of booleans on the right &#8230; returns elements from the first array that have TRUE on the same index of the second array.</p>
<p>array :filter</p>
<p>sends all the objects in array to the filter process yielding an array of filtered objects.</p>
<p>Check F-Script on Google. Array programming rocks on collections. It has ALL smalltalk stuff PLUS message patterns. There is a paper there where they literally smash functional programming/lambas/blocks &#8230; they have convinced mee that array programming is the right way to think about collections. Note that love functional programming. but array programming is better&#8230;</p>
<p>Again on smalltalk &#8230;</p>
<p>Stupid example (please replace add1 wich &#8230; YourComplexDifficultLongCollectionOperation)</p>
<p>{10. 20. 30. 40} :add1 :add1 :add1 :add1 :add1<br />
&gt;&gt;{15. 25. 35. 45}</p>
<p>Compare with smalltalk &#8230; </p>
<p>Please I&#8217;m a smalltalk novice correct me If I&#8217;m wrong&#8230;.</p>
<p>((((({10. 20. 30. 40} collect: [ :elem | elem :add1]) collect: [ :elem | elem :add1]) collect: [ :elem | elem :add1]) collect: [ :elem | elem :add1]) collect: [ :elem | elem :add1])</p>
<p>I think that PIPES are more important than the cascade operator as most of the cases where cascading is helpfull have the previous message returning self !!!&#8230; there are exceptions but I&#8217;d like not to loose pipes for them.</p>
<p>Any comments ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paolo Bonzini</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4816</link>
		<dc:creator>Paolo Bonzini</dc:creator>
		<pubDate>Fri, 24 Aug 2007 08:37:08 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4816</guid>
		<description>Your welcome, I guess I'll have to release a new prerelease soon because there were some big bugfixes recently.

I hope to port Seaside at the ESUG conference next week...</description>
		<content:encoded><![CDATA[<p>Your welcome, I guess I&#8217;ll have to release a new prerelease soon because there were some big bugfixes recently.</p>
<p>I hope to port Seaside at the ESUG conference next week&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4813</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Fri, 24 Aug 2007 07:57:35 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4813</guid>
		<description>All very interesting ideas, guess I'll have to give GNU Smalltalk a try soon, I hear it's pretty good at scripting so I was going to get to it anyway.</description>
		<content:encoded><![CDATA[<p>All very interesting ideas, guess I&#8217;ll have to give GNU Smalltalk a try soon, I hear it&#8217;s pretty good at scripting so I was going to get to it anyway.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paolo Bonzini</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4812</link>
		<dc:creator>Paolo Bonzini</dc:creator>
		<pubDate>Fri, 24 Aug 2007 07:51:52 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-4812</guid>
		<description>FWIW (I'm a month and something late on commenting) there are two ways to improve the "Smalltalk" way.

First, you could use "Lazy Collections", which provide an implementation of #select:/#collect: that does not create intermediate collections.  Then you would do this without any performance penalty:


programmers := employees select: [:emp &#124; emp role = #programmer ].
salaries := programmers collect: [:emp &#124; emp salary ]
^salaries inject: 0 into: [:a :b &#124; a max: b ]


But since this breaks code in interesting ways, there is another way around it, which is to implement #select: and friends on Streams.  GNU Smalltalk does this for example, so that you can write:


data := employees readStream.
programmers := data select: [:emp &#124; emp role = #programmer ].
salaries := programmers collect: [:emp &#124; emp salary ]
^salaries inject: 0 into: [:a :b &#124; a max: b ]


This creates three different kinds of streams (a ReadStream, a FilteringStream, a CollectingStream) and not even a single temporary collection.  Everything is just hidden behind the factory methods, which are polymorphic with Collection's.

Since in the upcoming version of GNU Smalltalk (3.0, release candidate will be ready in a two/three months I guesstimate) database rows are a subclass of Stream, you get this for free for database access too!</description>
		<content:encoded><![CDATA[<p>FWIW (I&#8217;m a month and something late on commenting) there are two ways to improve the &#8220;Smalltalk&#8221; way.</p>
<p>First, you could use &#8220;Lazy Collections&#8221;, which provide an implementation of #select:/#collect: that does not create intermediate collections.  Then you would do this without any performance penalty:</p>
<p>programmers := employees select: [:emp | emp role = #programmer ].<br />
salaries := programmers collect: [:emp | emp salary ]<br />
^salaries inject: 0 into: [:a :b | a max: b ]</p>
<p>But since this breaks code in interesting ways, there is another way around it, which is to implement #select: and friends on Streams.  GNU Smalltalk does this for example, so that you can write:</p>
<p>data := employees readStream.<br />
programmers := data select: [:emp | emp role = #programmer ].<br />
salaries := programmers collect: [:emp | emp salary ]<br />
^salaries inject: 0 into: [:a :b | a max: b ]</p>
<p>This creates three different kinds of streams (a ReadStream, a FilteringStream, a CollectingStream) and not even a single temporary collection.  Everything is just hidden behind the factory methods, which are polymorphic with Collection&#8217;s.</p>
<p>Since in the upcoming version of GNU Smalltalk (3.0, release candidate will be ready in a two/three months I guesstimate) database rows are a subclass of Stream, you get this for free for database access too!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3293</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Tue, 10 Jul 2007 04:54:35 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3293</guid>
		<description>What a great example Mark.  Database joins and where clauses are a perfect example of where for loops can be replaced by more reusable declarative syntax to greatly simplify the most common use cases.

Databases are also a good example of suffering the same problem, the failure to abstract beyond the primitive operations.  Databases have one primitive operation for viewing data, the "SELECT".  It's the one hammer that must fit all problems.

When working with collections in Smalltalk, we've abstracted beyond that into a handful of commonly used methods starting with #select:, but also #detect:, #detectMax:, #detectMin:, #detectSum:, #reject:, #collect:, #inject:into:, #includes:, #contains:, #do:, etc..

Some languages encourage abstractions and allow you to build upon your previous work, others don't.  Sometimes this is a limitation of the language, sometimes it's a limitation of the culture.

C# is an interesting example where the language has become capable, but the culture is seriously lagging behind.</description>
		<content:encoded><![CDATA[<p>What a great example Mark.  Database joins and where clauses are a perfect example of where for loops can be replaced by more reusable declarative syntax to greatly simplify the most common use cases.</p>
<p>Databases are also a good example of suffering the same problem, the failure to abstract beyond the primitive operations.  Databases have one primitive operation for viewing data, the &#8220;SELECT&#8221;.  It&#8217;s the one hammer that must fit all problems.</p>
<p>When working with collections in Smalltalk, we&#8217;ve abstracted beyond that into a handful of commonly used methods starting with #select:, but also #detect:, #detectMax:, #detectMin:, #detectSum:, #reject:, #collect:, #inject:into:, #includes:, #contains:, #do:, etc..</p>
<p>Some languages encourage abstractions and allow you to build upon your previous work, others don&#8217;t.  Sometimes this is a limitation of the language, sometimes it&#8217;s a limitation of the culture.</p>
<p>C# is an interesting example where the language has become capable, but the culture is seriously lagging behind.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Miller</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3285</link>
		<dc:creator>Mark Miller</dc:creator>
		<pubDate>Tue, 10 Jul 2007 02:13:01 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3285</guid>
		<description>Excellent comments here.

What Ramon is describing reminds me of a time when we were using an API-based database engine more than 10 years ago, at a company where I used to work. The library was lightening fast. We hadn't found another DB engine that could match its speed. The API did not understand the concept of joins, though. So we did them manually. We'd set up one filter for one side of the join, and another for the other, iterate over both of them, using an outer and inner loop (for the one-to-many relationships), and we'd test for a condition to then launch into another set of steps, or perhaps populate a UI. We did this over and over again. It was so repetitive it started to get boring and tiresome. It had the same feel to it as the enumerate, map, filter, accumulate pattern in the article Ramon links to. I think we tried the idea of creating a function to do this work, but the language (C) was not flexible enough to allow it. I suppose we could've littered the code with special-case, one-use functions and passed them in as function pointers. We knew of function pointers. I assume the scheme wouldn't have worked, because we didn't implement this. Quite a few of our iteration loops were customized with various interim logic steps. It was never quite the same loop twice, though as a basic pattern it was very consistent.

Then, on a different project the customer required us to work with an Oracle database, and I learned SQL. What a difference! I could express joins in a succinct, declarative manner, and get the desired result. It made me wonder why we did it the other way for so long.</description>
		<content:encoded><![CDATA[<p>Excellent comments here.</p>
<p>What Ramon is describing reminds me of a time when we were using an API-based database engine more than 10 years ago, at a company where I used to work. The library was lightening fast. We hadn&#8217;t found another DB engine that could match its speed. The API did not understand the concept of joins, though. So we did them manually. We&#8217;d set up one filter for one side of the join, and another for the other, iterate over both of them, using an outer and inner loop (for the one-to-many relationships), and we&#8217;d test for a condition to then launch into another set of steps, or perhaps populate a UI. We did this over and over again. It was so repetitive it started to get boring and tiresome. It had the same feel to it as the enumerate, map, filter, accumulate pattern in the article Ramon links to. I think we tried the idea of creating a function to do this work, but the language (C) was not flexible enough to allow it. I suppose we could&#8217;ve littered the code with special-case, one-use functions and passed them in as function pointers. We knew of function pointers. I assume the scheme wouldn&#8217;t have worked, because we didn&#8217;t implement this. Quite a few of our iteration loops were customized with various interim logic steps. It was never quite the same loop twice, though as a basic pattern it was very consistent.</p>
<p>Then, on a different project the customer required us to work with an Oracle database, and I learned SQL. What a difference! I could express joins in a succinct, declarative manner, and get the desired result. It made me wonder why we did it the other way for so long.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2007-07-09 &#171; Mike Does Tech</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3268</link>
		<dc:creator>links for 2007-07-09 &#171; Mike Does Tech</dc:creator>
		<pubDate>Mon, 09 Jul 2007 13:14:18 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3268</guid>
		<description>[...] On Simple Functional Idioms [...]</description>
		<content:encoded><![CDATA[<p>[...] On Simple Functional Idioms [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Shell</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3241</link>
		<dc:creator>Jeff Shell</dc:creator>
		<pubDate>Mon, 09 Jul 2007 00:53:34 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3241</guid>
		<description>Ach. Thanks for clearing things up. I guess I've just gotten so used to Python and other dynamic languages over the past ten or so years. I forgot that the big static languages made such things so difficult. No wonder my eyes glazed over whenever anyone tried to explain the rationale for Templates in C++.  I hope that I never have to return to such a world.</description>
		<content:encoded><![CDATA[<p>Ach. Thanks for clearing things up. I guess I&#8217;ve just gotten so used to Python and other dynamic languages over the past ten or so years. I forgot that the big static languages made such things so difficult. No wonder my eyes glazed over whenever anyone tried to explain the rationale for Templates in C++.  I hope that I never have to return to such a world.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piers Cawley</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3232</link>
		<dc:creator>Piers Cawley</dc:creator>
		<pubDate>Sun, 08 Jul 2007 06:53:51 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3232</guid>
		<description>I just realised, I meant to point out Adam Turoff's excellent article about this very topic at: http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-with-for-loop.html

So I'll do it now.

And I could have &lt;em&gt;sworn&lt;/em&gt; I'd tried using &#60;pre&#62; before and had the comment rejected. Ho hum.</description>
		<content:encoded><![CDATA[<p>I just realised, I meant to point out Adam Turoff&#8217;s excellent article about this very topic at: <a href="http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-with-for-loop.html" rel="nofollow">http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-with-for-loop.html</a></p>
<p>So I&#8217;ll do it now.</p>
<p>And I could have <em>sworn</em> I&#8217;d tried using &lt;pre&gt; before and had the comment rejected. Ho hum.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Masklinn</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3231</link>
		<dc:creator>Masklinn</dc:creator>
		<pubDate>Sat, 07 Jul 2007 22:08:17 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3231</guid>
		<description>&#62; For all its faults, the Java version does at least only walk through the collection once.

That's an optimization for performance instead of an optimization for clarity (programs should be written for humans to understand, and incidentally for computers to execute). Plus it can be solved by fusion (which the Haskell guys are currently exploring)

&#62; So if you were using Java and wanted map/filter/reduce/inject/whatever, why not take a few minutes and write a module/class/helper with those items in it?

Because there is no possibility to easily inject behavior, in Java or C# you need to creates classes for everything, and at best you can create an anonymous class with a previously defined type, and a very low genericity.

&#62; In basic Smalltalk, Ruby, etc, if I were to nest these like I’m doing in Python, the collection would be iterated for each little case. But with Iterators it only gets run through once, even though I’m nesting a lot of calls.

That's a runtime/optimization issue, it has no relation to the *concept* itself, which is exactly the same in Python, Smalltalk, Ruby, Scheme or Haskell.</description>
		<content:encoded><![CDATA[<p>&gt; For all its faults, the Java version does at least only walk through the collection once.</p>
<p>That&#8217;s an optimization for performance instead of an optimization for clarity (programs should be written for humans to understand, and incidentally for computers to execute). Plus it can be solved by fusion (which the Haskell guys are currently exploring)</p>
<p>&gt; So if you were using Java and wanted map/filter/reduce/inject/whatever, why not take a few minutes and write a module/class/helper with those items in it?</p>
<p>Because there is no possibility to easily inject behavior, in Java or C# you need to creates classes for everything, and at best you can create an anonymous class with a previously defined type, and a very low genericity.</p>
<p>&gt; In basic Smalltalk, Ruby, etc, if I were to nest these like I’m doing in Python, the collection would be iterated for each little case. But with Iterators it only gets run through once, even though I’m nesting a lot of calls.</p>
<p>That&#8217;s a runtime/optimization issue, it has no relation to the *concept* itself, which is exactly the same in Python, Smalltalk, Ruby, Scheme or Haskell.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Popov</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3230</link>
		<dc:creator>Boris Popov</dc:creator>
		<pubDate>Sat, 07 Jul 2007 19:45:12 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3230</guid>
		<description>Well, I personally hate perform:'s since they fall outside of rewrite capabilities, but that's a whole other discussion. Either way, we could come up with a million ways of doing the same thing, but in some cases it is important to have composite patterns that do a single walk even though they tend to be a little less reusable, but such is the case with all composites :)</description>
		<content:encoded><![CDATA[<p>Well, I personally hate perform:&#8217;s since they fall outside of rewrite capabilities, but that&#8217;s a whole other discussion. Either way, we could come up with a million ways of doing the same thing, but in some cases it is important to have composite patterns that do a single walk even though they tend to be a little less reusable, but such is the case with all composites :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3229</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Sat, 07 Jul 2007 19:39:14 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3229</guid>
		<description>Of course, if you had such predicates as isProgrammer on the model, then I'd just call your method like this...

&lt;pre&gt;
employees max: #salary if: #isProgrammer
&lt;/pre&gt;

But this is really just another version of select:detectMax

&lt;pre&gt;
(employees select: #isProgrammer) detectMax: #salary 
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Of course, if you had such predicates as isProgrammer on the model, then I&#8217;d just call your method like this&#8230;</p>
<pre>
employees max: #salary if: #isProgrammer
</pre>
<p>But this is really just another version of select:detectMax</p>
<pre>
(employees select: #isProgrammer) detectMax: #salary
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Popov</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3228</link>
		<dc:creator>Boris Popov</dc:creator>
		<pubDate>Sat, 07 Jul 2007 19:36:54 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3228</guid>
		<description>Of course you could also use that do things like finding the largest even number in a collection,

&lt;pre&gt;
numbers max: [:ea &#124; ea] if: [:ea &#124; ea even]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Of course you could also use that do things like finding the largest even number in a collection,</p>
<pre>
numbers max: [:ea | ea] if: [:ea | ea even]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Popov</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3227</link>
		<dc:creator>Boris Popov</dc:creator>
		<pubDate>Sat, 07 Jul 2007 19:35:32 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3227</guid>
		<description>Well, the real beauty is that you could go nuts with this and simply create things to do,

&lt;pre&gt;
employees max: [:ea &#124; ea salary] if: [:ea &#124; ea isProgrammer]
&lt;/pre&gt;

where,

&lt;pre&gt;
max: fetch if: condition
 ^self
     inject: 0
     into: [:max :ea &#124; max max: ((condition value: ea)
                                                ifTrue: [fetch value: ea]
                                                ifFalse: [max])]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Well, the real beauty is that you could go nuts with this and simply create things to do,</p>
<pre>
employees max: [:ea | ea salary] if: [:ea | ea isProgrammer]
</pre>
<p>where,</p>
<pre>
max: fetch if: condition
 ^self
     inject: 0
     into: [:max :ea | max max: ((condition value: ea)
                                                ifTrue: [fetch value: ea]
                                                ifFalse: [max])]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3226</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Sat, 07 Jul 2007 19:15:23 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3226</guid>
		<description>pre tags of course, fixed.</description>
		<content:encoded><![CDATA[<p>pre tags of course, fixed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piers Cawley</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3225</link>
		<dc:creator>Piers Cawley</dc:creator>
		<pubDate>Sat, 07 Jul 2007 19:14:24 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3225</guid>
		<description>Damn, what's the magic for putting a code block in comments?</description>
		<content:encoded><![CDATA[<p>Damn, what&#8217;s the magic for putting a code block in comments?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piers Cawley</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3224</link>
		<dc:creator>Piers Cawley</dc:creator>
		<pubDate>Sat, 07 Jul 2007 19:12:49 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3224</guid>
		<description>Boris: I actually prefer the higher order select: thenCollect: andInject: into:, which can be implemented as:

&lt;pre&gt;
select: selBlock thenCollect: collBlock andInject: anObject into: injBlock
    ^self inject: anObject 
          into: [:i :rawItem&#124;
              (selBlock value: rawItem)
                  ifTrue: 
                      [injBlock 
                          value: i 
                          value: (collBlock value: rawItem) ]
                  ifFalse: [i]]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Boris: I actually prefer the higher order select: thenCollect: andInject: into:, which can be implemented as:</p>
<pre>
select: selBlock thenCollect: collBlock andInject: anObject into: injBlock
    ^self inject: anObject
          into: [:i :rawItem|
              (selBlock value: rawItem)
                  ifTrue:
                      [injBlock
                          value: i
                          value: (collBlock value: rawItem) ]
                  ifFalse: [i]]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ramon Leon</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3223</link>
		<dc:creator>Ramon Leon</dc:creator>
		<pubDate>Sat, 07 Jul 2007 18:55:27 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3223</guid>
		<description>@jeff, without first class functions, these patterns can't be idiomatic because you're forced to define every little predicate as a separate class making it less work to just write the foreach instead.  &lt;strong&gt;Newer version of C# actually do have first class functions, and 3.0 even has a nice syntax for them&lt;/strong&gt;.  That doesn't help much when the code you maintain is written in 1.1 and you're too busy to justify upgrading.

"If you can write a procedure of any kind that takes an input and returns an output, don’t you then have some kind of higher order function?"

No, that'd be a regular function.  A higher order function takes other functions, not their values as it's inputs or outputs.

As for those worried about multiple iterations of the collection, optimization isn't the point.  The point is composition of existing code in a chain.  A very common chain that occurs over and over in code and could be optimized behind the scenes without cluttering the expression of the solution with imperative constructs.  

Declarative solutions are nicer because they express only the "what" while the imperative solutions get all bogged down in the "how".

The IO solution is interesting, I'd considered that same solution in Smalltalk but left it out because of the Symbol value hack it uses to avoid the block

&lt;pre&gt;
(employees select: [:emp &#124; emp role = #programmer ]) detectMax: #salary
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>@jeff, without first class functions, these patterns can&#8217;t be idiomatic because you&#8217;re forced to define every little predicate as a separate class making it less work to just write the foreach instead.  <strong>Newer version of C# actually do have first class functions, and 3.0 even has a nice syntax for them</strong>.  That doesn&#8217;t help much when the code you maintain is written in 1.1 and you&#8217;re too busy to justify upgrading.</p>
<p>&#8220;If you can write a procedure of any kind that takes an input and returns an output, don’t you then have some kind of higher order function?&#8221;</p>
<p>No, that&#8217;d be a regular function.  A higher order function takes other functions, not their values as it&#8217;s inputs or outputs.</p>
<p>As for those worried about multiple iterations of the collection, optimization isn&#8217;t the point.  The point is composition of existing code in a chain.  A very common chain that occurs over and over in code and could be optimized behind the scenes without cluttering the expression of the solution with imperative constructs.  </p>
<p>Declarative solutions are nicer because they express only the &#8220;what&#8221; while the imperative solutions get all bogged down in the &#8220;how&#8221;.</p>
<p>The IO solution is interesting, I&#8217;d considered that same solution in Smalltalk but left it out because of the Symbol value hack it uses to avoid the block</p>
<pre>
(employees select: [:emp | emp role = #programmer ]) detectMax: #salary
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3221</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Sat, 07 Jul 2007 16:55:18 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3221</guid>
		<description>The io version is most compact:

&lt;pre&gt;
employees select(role == "programmer") max(salary)
&lt;/pre&gt;

See http://www.dekorte.com/blog/blog.cgi?do=item&#38;id=2752</description>
		<content:encoded><![CDATA[<p>The io version is most compact:</p>
<pre>
employees select(role == "programmer") max(salary)
</pre>
<p>See <a href="http://www.dekorte.com/blog/blog.cgi?do=item&amp;id=2752" rel="nofollow">http://www.dekorte.com/blog/blog.cgi?do=item&amp;id=2752</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Boris Popov</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3220</link>
		<dc:creator>Boris Popov</dc:creator>
		<pubDate>Sat, 07 Jul 2007 16:27:47 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3220</guid>
		<description>Piers, here's a single walk, which I personally prefer as well,

&lt;pre&gt;
employees inject: 0 into: [:x :ea &#124;
  x max: (ea isProgrammer
                       ifTrue: [ea salary]
                       ifFalse: [x])]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Piers, here&#8217;s a single walk, which I personally prefer as well,</p>
<pre>
employees inject: 0 into: [:x :ea |
  x max: (ea isProgrammer
                       ifTrue: [ea salary]
                       ifFalse: [x])]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff Shell</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3219</link>
		<dc:creator>Jeff Shell</dc:creator>
		<pubDate>Sat, 07 Jul 2007 13:36:18 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3219</guid>
		<description>Um. Why can't Java and C# support higher order functions or make them idiomatic? I'm not a fan of those languages. Maybe I just still don't get functional programming.

If you can write a procedure of any kind that takes an input and returns an output, don't you then have some kind of higher order function? So if you were using Java and wanted map/filter/reduce/inject/whatever, why not take a few minutes and write a module/class/helper with those items in it?

Even better would be to support Iterators like Python. I've been using Python's 'itertools' module which has lazy versions of map, filter (select), etc..., using Python's simple Iterator's protocol. As a result I've been able to build up complex filter chains by grouping small functions together (is_active, has_dates, is_past). In basic Smalltalk, Ruby, etc, if I were to nest these like I'm doing in Python, the collection would be iterated for each little case. But with Iterators it only gets run through once, even though I'm nesting a lot of calls.

Inside of all of these 'ifilter' calls, and to the outside world, it all looks the same. The input and output is always an iterable. you can just keep stacking them up.

For example, you could do the following (assuming you had the small 'is_...' functions defined):

import itertools, operator
max_salary = max(itertools.imap(operator.attrgetter('salary'), itertools.ifilter(is_programmer, employees)))

Or you can use Python's "generator comprehensions" and do the following without having to put up with map and filter for such a simple use case:

max_salary = max(emp.salary for emp in employees if emp.role is Programmer)

Note that Python's built in 'max()' function can take in either an iterable/sequence, or multiple arguments (ie: max([1,3,5,7,9]) and max(1,3,5,7,9). No need to deal with inject or reduce here, which I must admit is the one fuctional idiom I can do without (writing a function like Python's max() is a lot easier to read in production code. Same thing with sum() - much better than reduce(operator.add, seq) or reduce(lambda a,b: a+b, seq))

I used to love using Prototype.js when I did Javascript because it added the collect, inject, etc methods to most array-like objects. But it didn't apply them to all. So I was constantly wasting cycles converting things to arrays or wasting cycles and memory when nesting maps/filters/reduces because it had to make a new list every time. Then I went to MochiKit, which is heavily influenced by Python. In its base library it has 'map' and 'filter' and other helpers for this style of programming, which I do love to use. But even better - it has an 'Iterators' module that does Python style Iterators and provides a lot of lazy implementations of the same features. By using adapters, it can provide the iterator protocol for 'array-like' objects (some annoying pseudo-collections that JS has that have less methods than the base Array). Check it out as a good example of providing "simple functional idioms" to a C-based for-loop style language like Javascript.

http://www.mochikit.com/doc/html/MochiKit/Iter.html

Using Iterators and Generators in Python (and with MochiKit in Javascript.. and I believe JS 1.7 sports some iterator features natively now), you can get some really powerful composition. By taking in and returning an iterable, which may be lazy, you could build up a large complex filter/query together over time by running the results through more generator functions, and still have it exhaust its primary resource only once. You don't need long methods like 'select:thenCollect:andInject:into' because you can just nest function calls - while still taking advantage of an object oriented language.

Sorry, this is a bit of an exhaustive comment. In summary - I rarely program in Javascript without MochiKit's Python based functional idioms by my side. Any C-based language can do the same. Granted, it's not idiomatic for everyone in that case, but it's still very useful. With a little bit of work, you can write a library that can make the most intolerable language more bearable. And also - I think there are even better idioms like Python's Iterators and Generators which can be used to compose very powerful filters (using these same basic techniques) without running through the collection each time, all by supporting a very very simple iterator protocol.</description>
		<content:encoded><![CDATA[<p>Um. Why can&#8217;t Java and C# support higher order functions or make them idiomatic? I&#8217;m not a fan of those languages. Maybe I just still don&#8217;t get functional programming.</p>
<p>If you can write a procedure of any kind that takes an input and returns an output, don&#8217;t you then have some kind of higher order function? So if you were using Java and wanted map/filter/reduce/inject/whatever, why not take a few minutes and write a module/class/helper with those items in it?</p>
<p>Even better would be to support Iterators like Python. I&#8217;ve been using Python&#8217;s &#8216;itertools&#8217; module which has lazy versions of map, filter (select), etc&#8230;, using Python&#8217;s simple Iterator&#8217;s protocol. As a result I&#8217;ve been able to build up complex filter chains by grouping small functions together (is_active, has_dates, is_past). In basic Smalltalk, Ruby, etc, if I were to nest these like I&#8217;m doing in Python, the collection would be iterated for each little case. But with Iterators it only gets run through once, even though I&#8217;m nesting a lot of calls.</p>
<p>Inside of all of these &#8216;ifilter&#8217; calls, and to the outside world, it all looks the same. The input and output is always an iterable. you can just keep stacking them up.</p>
<p>For example, you could do the following (assuming you had the small &#8216;is_&#8230;&#8217; functions defined):</p>
<p>import itertools, operator<br />
max_salary = max(itertools.imap(operator.attrgetter(&#8217;salary&#8217;), itertools.ifilter(is_programmer, employees)))</p>
<p>Or you can use Python&#8217;s &#8220;generator comprehensions&#8221; and do the following without having to put up with map and filter for such a simple use case:</p>
<p>max_salary = max(emp.salary for emp in employees if emp.role is Programmer)</p>
<p>Note that Python&#8217;s built in &#8216;max()&#8217; function can take in either an iterable/sequence, or multiple arguments (ie: max([1,3,5,7,9]) and max(1,3,5,7,9). No need to deal with inject or reduce here, which I must admit is the one fuctional idiom I can do without (writing a function like Python&#8217;s max() is a lot easier to read in production code. Same thing with sum() - much better than reduce(operator.add, seq) or reduce(lambda a,b: a+b, seq))</p>
<p>I used to love using Prototype.js when I did Javascript because it added the collect, inject, etc methods to most array-like objects. But it didn&#8217;t apply them to all. So I was constantly wasting cycles converting things to arrays or wasting cycles and memory when nesting maps/filters/reduces because it had to make a new list every time. Then I went to MochiKit, which is heavily influenced by Python. In its base library it has &#8216;map&#8217; and &#8216;filter&#8217; and other helpers for this style of programming, which I do love to use. But even better - it has an &#8216;Iterators&#8217; module that does Python style Iterators and provides a lot of lazy implementations of the same features. By using adapters, it can provide the iterator protocol for &#8216;array-like&#8217; objects (some annoying pseudo-collections that JS has that have less methods than the base Array). Check it out as a good example of providing &#8220;simple functional idioms&#8221; to a C-based for-loop style language like Javascript.</p>
<p><a href="http://www.mochikit.com/doc/html/MochiKit/Iter.html" rel="nofollow">http://www.mochikit.com/doc/html/MochiKit/Iter.html</a></p>
<p>Using Iterators and Generators in Python (and with MochiKit in Javascript.. and I believe JS 1.7 sports some iterator features natively now), you can get some really powerful composition. By taking in and returning an iterable, which may be lazy, you could build up a large complex filter/query together over time by running the results through more generator functions, and still have it exhaust its primary resource only once. You don&#8217;t need long methods like &#8217;select:thenCollect:andInject:into&#8217; because you can just nest function calls - while still taking advantage of an object oriented language.</p>
<p>Sorry, this is a bit of an exhaustive comment. In summary - I rarely program in Javascript without MochiKit&#8217;s Python based functional idioms by my side. Any C-based language can do the same. Granted, it&#8217;s not idiomatic for everyone in that case, but it&#8217;s still very useful. With a little bit of work, you can write a library that can make the most intolerable language more bearable. And also - I think there are even better idioms like Python&#8217;s Iterators and Generators which can be used to compose very powerful filters (using these same basic techniques) without running through the collection each time, all by supporting a very very simple iterator protocol.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piers Cawley</title>
		<link>http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3218</link>
		<dc:creator>Piers Cawley</dc:creator>
		<pubDate>Sat, 07 Jul 2007 07:36:14 +0000</pubDate>
		<guid isPermaLink="false">http://onsmalltalk.com/programming/smalltalk/on-simple-functional-idioms/#comment-3218</guid>
		<description>For all its faults, the Java version does at least only walk through the collection once. 

Admittedly, if you were in a mood to optimize, it's possible to write select:thenCollect: and, by extension, select:thenCollect:andInject:into: in a way that only loops once (or possibly twice in the case of fixed size collections). 

The beauty of functional methods like this being that you only have to do the optimization once, and every user benefits without affecting client code complexity. Result.</description>
		<content:encoded><![CDATA[<p>For all its faults, the Java version does at least only walk through the collection once. </p>
<p>Admittedly, if you were in a mood to optimize, it&#8217;s possible to write select:thenCollect: and, by extension, select:thenCollect:andInject:into: in a way that only loops once (or possibly twice in the case of fixed size collections). </p>
<p>The beauty of functional methods like this being that you only have to do the optimization once, and every user benefits without affecting client code complexity. Result.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
