<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Naming and Some Common String and Array Patterns</title>
	<atom:link href="http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/</link>
	<description>Who Spilled the Java Beans?</description>
	<lastBuildDate>Wed, 28 Jul 2010 15:32:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Iulian Dogariu</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-26</link>
		<dc:creator>Iulian Dogariu</dc:creator>
		<pubDate>Thu, 09 Oct 2008 10:53:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-26</guid>
		<description>Wow, thanks, Marius! That is one hell of a contribution :-)

I can&#039;t seem to reproduce the problems you had while posting the Perl and Python examples, tho&#039; :-(</description>
		<content:encoded><![CDATA[<p>Wow, thanks, Marius! That is one hell of a contribution <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I can&#8217;t seem to reproduce the problems you had while posting the Perl and Python examples, tho&#8217; <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marius Feraru</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-27</link>
		<dc:creator>Marius Feraru</dc:creator>
		<pubDate>Sun, 28 Sep 2008 21:10:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-27</guid>
		<description>and Python:

$ cat ./yu_li_yan_p126.py
#!/usr/bin/python
import re

fruits     = [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;, &#039;orange&#039;, &#039;plum&#039;]
fruits_str = &#039; - &#039;.join(fruits)
months     = [&#039;Jan&#039;, &#039;Feb&#039;, &#039;Mar&#039;, &#039;Apr&#039;, &#039;May&#039;, &#039;Jun&#039;]
days       = [  31,    28,    31,    30,    31,    30 ]
numbers    = range(1, 7)
aoa = [ &#039;one&#039;, [ &#039;two&#039;, &#039;three&#039; ], &#039;four&#039;, [ &#039;five&#039;, [ &#039;six&#039;, &#039;seven&#039; ] ] ]

def show(label,object):
    print &#039;%10s: %s&#039; % (label, object)

# ok, recursion in python seems to suck big time,
# so here&#039;s a fucked up iterative version ;-/
def flatten(arr):
    i = 0
    while i &lt; len(arr):
        while isinstance(arr[i], (list, tuple)):
            if not arr[i]:
                arr.pop(i)
                if not len(arr):
                    break
            else:
                arr[i:i+1] = list(arr[i])
        i += 1
    return arr

show(&#039;join&#039;, fruits_str)
show(&#039;split&#039;, fruits_str.split(&#039; - &#039;))
show(&#039;zip&#039;, zip(months, days, numbers))
show(&#039;flatten&#039;, flatten(aoa))
show(&#039;map&#039;, map(lambda x: x**2, numbers))
show(&#039;reduce&#039;, reduce(lambda x,y: x+y, numbers))
show(&#039;select&#039;, filter(lambda x: re.compile(&quot;^[aeiou]&quot;, re.I).match(x), fruits))
show(&#039;reject&#039;, filter(lambda x: not re.compile(&quot;^[aeiou]&quot;, re.I).match(x), fruits))

$ ./yu_li_yan_p126.py
      join: apple - banana - cherry - orange - plum
     split: [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;, &#039;orange&#039;, &#039;plum&#039;]
       zip: [(&#039;Jan&#039;, 31, 1), (&#039;Feb&#039;, 28, 2), (&#039;Mar&#039;, 31, 3), (&#039;Apr&#039;, 30, 4), (&#039;May&#039;, 31, 5), (&#039;Jun&#039;, 30, 6)]
   flatten: [&#039;one&#039;, &#039;two&#039;, &#039;three&#039;, &#039;four&#039;, &#039;five&#039;, &#039;six&#039;, &#039;seven&#039;]
       map: [1, 4, 9, 16, 25, 36]
    reduce: 21
    select: [&#039;apple&#039;, &#039;orange&#039;]
    reject: [&#039;banana&#039;, &#039;cherry&#039;, &#039;plum&#039;]</description>
		<content:encoded><![CDATA[<p>and Python:</p>
<p>$ cat ./yu_li_yan_p126.py<br />
#!/usr/bin/python<br />
import re</p>
<p>fruits     = ['apple', 'banana', 'cherry', 'orange', 'plum']<br />
fruits_str = &#8216; &#8211; &#8216;.join(fruits)<br />
months     = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']<br />
days       = [  31,    28,    31,    30,    31,    30 ]<br />
numbers    = range(1, 7)<br />
aoa = [ 'one', [ 'two', 'three' ], &#8216;four&#8217;, [ 'five', [ 'six', 'seven' ] ] ]</p>
<p>def show(label,object):<br />
    print &#8216;%10s: %s&#8217; % (label, object)</p>
<p># ok, recursion in python seems to suck big time,<br />
# so here&#8217;s a fucked up iterative version ;-/<br />
def flatten(arr):<br />
    i = 0<br />
    while i &lt; len(arr):<br />
        while isinstance(arr[i], (list, tuple)):<br />
            if not arr[i]:<br />
                arr.pop(i)<br />
                if not len(arr):<br />
                    break<br />
            else:<br />
                arr[i:i+1] = list(arr[i])<br />
        i += 1<br />
    return arr</p>
<p>show(&#8216;join&#8217;, fruits_str)<br />
show(&#8217;split&#8217;, fruits_str.split(&#8216; &#8211; &#8216;))<br />
show(&#8216;zip&#8217;, zip(months, days, numbers))<br />
show(&#8216;flatten&#8217;, flatten(aoa))<br />
show(&#8216;map&#8217;, map(lambda x: x**2, numbers))<br />
show(&#8216;reduce&#8217;, reduce(lambda x,y: x+y, numbers))<br />
show(&#8217;select&#8217;, filter(lambda x: re.compile(&#8220;^[aeiou]&#8220;, re.I).match(x), fruits))<br />
show(&#8216;reject&#8217;, filter(lambda x: not re.compile(&#8220;^[aeiou]&#8220;, re.I).match(x), fruits))</p>
<p>$ ./yu_li_yan_p126.py<br />
      join: apple &#8211; banana &#8211; cherry &#8211; orange &#8211; plum<br />
     split: ['apple', 'banana', 'cherry', 'orange', 'plum']<br />
       zip: [('Jan', 31, 1), ('Feb', 28, 2), ('Mar', 31, 3), ('Apr', 30, 4), ('May', 31, 5), ('Jun', 30, 6)]<br />
   flatten: ['one', 'two', 'three', 'four', 'five', 'six', 'seven']<br />
       map: [1, 4, 9, 16, 25, 36]<br />
    reduce: 21<br />
    select: ['apple', 'orange']<br />
    reject: ['banana', 'cherry', 'plum']</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marius Feraru</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-29</link>
		<dc:creator>Marius Feraru</dc:creator>
		<pubDate>Sun, 28 Sep 2008 21:08:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-29</guid>
		<description>and again, the same bullshit happened. :(

once again, Perl only:

$ cat ./yu_li_yan_p126.pl
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper ();
use List::Util qw(max reduce);
use vars qw($a $b);

my @fruits     = qw(apple banana cherry orange plum);
my $fruits_str = join &#039; - &#039;, @fruits;
my @months     = qw(Jan Feb Mar Apr May Jun);
my @days       = (   31, 28, 31, 30, 31, 30);
my @numbers    = ( 1 .. 6 );
my $aoa
    = [ &#039;one&#039;, [ &#039;two&#039;, &#039;three&#039; ], &#039;four&#039;, [ &#039;five&#039;, [ &#039;six&#039;, &#039;seven&#039; ] ] ];

sub show {
    printf &quot;%10s: %s\n&quot;,
        shift,
        Data::Dumper-&gt;new( [ \@_ ] )-&gt;Indent(0)-&gt;Terse(1)-&gt;Dump;
}

sub flatten {    # it&#039;s your job to flatten other kinds of refs ;-)
    return map { ref $_ ? flatten(@$_) : $_ } @_;
}

sub zip {
    map { my $i = $_; [ map { $_-&gt;[$i] } @_ ] } 0 .. max map { $#{$_} } @_;
}

show(&#039;join&#039;, $fruits_str);
show(&#039;split&#039;, split / - /, $fruits_str);
show(&#039;zip&#039;, zip(\@months, \@days, \@numbers));
show(&#039;flatten&#039;, flatten($aoa));
show(&#039;map&#039;, map { $_**2 } @numbers);
show(&#039;reduce&#039;, reduce { $a + $b } @numbers);
show(&#039;select&#039;, grep { /^[aeiou]/i } @fruits);
show(&#039;reject&#039;, grep { !/^[aeiou]/i } @fruits);

$ ./yu_li_yan_p126.pl
      join: [&#039;apple - banana - cherry - orange - plum&#039;]
     split: [&#039;apple&#039;,&#039;banana&#039;,&#039;cherry&#039;,&#039;orange&#039;,&#039;plum&#039;]
       zip: [[&#039;Jan&#039;,31,1],[&#039;Feb&#039;,28,2],[&#039;Mar&#039;,31,3],[&#039;Apr&#039;,30,4],[&#039;May&#039;,31,5],[&#039;Jun&#039;,30,6]]
   flatten: [&#039;one&#039;,&#039;two&#039;,&#039;three&#039;,&#039;four&#039;,&#039;five&#039;,&#039;six&#039;,&#039;seven&#039;]
       map: [1,4,9,16,25,36]
    reduce: [21]
    select: [&#039;apple&#039;,&#039;orange&#039;]
    reject: [&#039;banana&#039;,&#039;cherry&#039;,&#039;plum&#039;]</description>
		<content:encoded><![CDATA[<p>and again, the same bullshit happened. <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>once again, Perl only:</p>
<p>$ cat ./yu_li_yan_p126.pl<br />
#!/usr/bin/perl<br />
use warnings;<br />
use strict;<br />
use Data::Dumper ();<br />
use List::Util qw(max reduce);<br />
use vars qw($a $b);</p>
<p>my @fruits     = qw(apple banana cherry orange plum);<br />
my $fruits_str = join &#8216; &#8211; &#8216;, @fruits;<br />
my @months     = qw(Jan Feb Mar Apr May Jun);<br />
my @days       = (   31, 28, 31, 30, 31, 30);<br />
my @numbers    = ( 1 .. 6 );<br />
my $aoa<br />
    = [ 'one', [ 'two', 'three' ], &#8216;four&#8217;, [ 'five', [ 'six', 'seven' ] ] ];</p>
<p>sub show {<br />
    printf &#8220;%10s: %s\n&#8221;,<br />
        shift,<br />
        Data::Dumper-&gt;new( [ \@_ ] )-&gt;Indent(0)-&gt;Terse(1)-&gt;Dump;<br />
}</p>
<p>sub flatten {    # it&#8217;s your job to flatten other kinds of refs <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
    return map { ref $_ ? flatten(@$_) : $_ } @_;<br />
}</p>
<p>sub zip {<br />
    map { my $i = $_; [ map { $_-&gt;[$i] } @_ ] } 0 .. max map { $#{$_} } @_;<br />
}</p>
<p>show(&#8216;join&#8217;, $fruits_str);<br />
show(&#8217;split&#8217;, split / &#8211; /, $fruits_str);<br />
show(&#8216;zip&#8217;, zip(\@months, \@days, \@numbers));<br />
show(&#8216;flatten&#8217;, flatten($aoa));<br />
show(&#8216;map&#8217;, map { $_**2 } @numbers);<br />
show(&#8216;reduce&#8217;, reduce { $a + $b } @numbers);<br />
show(&#8217;select&#8217;, grep { /^[aeiou]/i } @fruits);<br />
show(&#8216;reject&#8217;, grep { !/^[aeiou]/i } @fruits);</p>
<p>$ ./yu_li_yan_p126.pl<br />
      join: ['apple - banana - cherry - orange - plum']<br />
     split: ['apple','banana','cherry','orange','plum']<br />
       zip: [['Jan',31,1],['Feb',28,2],['Mar',31,3],['Apr',30,4],['May',31,5],['Jun',30,6]]<br />
   flatten: ['one','two','three','four','five','six','seven']<br />
       map: [1,4,9,16,25,36]<br />
    reduce: [21]<br />
    select: ['apple','orange']<br />
    reject: ['banana','cherry','plum']</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marius Feraru</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-28</link>
		<dc:creator>Marius Feraru</dc:creator>
		<pubDate>Sun, 28 Sep 2008 21:06:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-28</guid>
		<description>Bollocks, looks like Python and Perl sections were zipped somehow :))
After checking my cache, I can say I submitted the right thing. :(

&lt;em&gt;(removed by Iulian, same problem occurred :-/)&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>Bollocks, looks like Python and Perl sections were zipped somehow <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )<br />
After checking my cache, I can say I submitted the right thing. <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p><em>(removed by Iulian, same problem occurred :-/)</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marius Feraru</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-34</link>
		<dc:creator>Marius Feraru</dc:creator>
		<pubDate>Sun, 28 Sep 2008 20:58:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-34</guid>
		<description>Hehe, ok, as much as I like the D&amp;D analogy, I have to note that we&#039;re talking about different things though, as /birth names/ are a given that the others *have to* accept as such (ok, even if you nick them), while these concepts don&#039;t. For instance, I doubt there&#039;s anyone else besides those from Ruby communities who would think that &quot;inject&quot; is the same thing as &quot;reduce&quot; ;-)

++ IIRC PHP&#039;s functions are not as flexible as everyone else&#039;s (and looking at array_combine&#039;s spec seems to prove it).

++ Python has a different opinion about &quot;zipping&quot;, truncating the list of tuples to the length of the shortest argument.

It&#039;s a boring Sunday, so let&#039;s give your readers some more examples (using your use cases)...

$ cat ./yu_li_yan_p126.rb
#!/usr/bin/ruby

fruits     = %w(apple banana cherry orange plum);
fruits_str = fruits.join(&#039; - &#039;);
months     = %w(Jan Feb Mar Apr May Jun);
days       = [   31, 28, 31, 30, 31, 30];
numbers    = (1..6).to_a;
aoa = [ &#039;one&#039;, [ &#039;two&#039;, &#039;three&#039; ], &#039;four&#039;, [ &#039;five&#039;, [ &#039;six&#039;, &#039;seven&#039; ] ] ];

def show(label, object)
    printf &quot;%10s: %s\n&quot;, label, object.inspect
end

show(&#039;join&#039;, fruits_str);
show(&#039;split&#039;, fruits_str.split(&#039; - &#039;));
show(&#039;zip&#039;, months.zip(days, numbers));
show(&#039;flatten&#039;, aoa.flatten);
show(&#039;map&#039;, numbers.map {&#124;x&#124; x**2});
#show(&#039;reduce&#039;, numbers.reduce(:+)); # needs v1.8.7 ;-)
show(&#039;reduce&#039;, numbers.inject {&#124;x,y&#124; x+y});
#show(&#039;select&#039;, fruits.select {&#124;x&#124; x =~ /^[aeiou]/i});
show(&#039;select&#039;, fruits.grep(/^[aeiou]/i));
#show(&#039;reject&#039;, fruits.reject {&#124;x&#124; x =~ /^[aeiou]/i});
show(&#039;reject&#039;, fruits.grep(/^[^aeiou]/i));

$ ./yu_li_yan_p126.rb
      join: &quot;apple - banana - cherry - orange - plum&quot;
     split: [&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;, &quot;orange&quot;, &quot;plum&quot;]
       zip: [[&quot;Jan&quot;, 31, 1], [&quot;Feb&quot;, 28, 2], [&quot;Mar&quot;, 31, 3], [&quot;Apr&quot;, 30, 4], [&quot;May&quot;, 31, 5], [&quot;Jun&quot;, 30, 6]]
   flatten: [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;]
       map: [1, 4, 9, 16, 25, 36]
    reduce: 21
    select: [&quot;apple&quot;, &quot;orange&quot;]
    reject: [&quot;banana&quot;, &quot;cherry&quot;, &quot;plum&quot;]

$ cat ./yu_li_yan_p126.py
#!/usr/bin/python
import re

fruits     = [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;, &#039;orange&#039;, &#039;plum&#039;]
fruits_str = &#039; - &#039;.join(fruits)
months     = [&#039;Jan&#039;, &#039;Feb&#039;, &#039;Mar&#039;, &#039;Apr&#039;, &#039;May&#039;, &#039;Jun&#039;]
days       = [  31,    28,    31,    30,    31,    30 ]
numbers    = range(1, 7)
aoa = [ &#039;one&#039;, [ &#039;two&#039;, &#039;three&#039; ], &#039;four&#039;, [ &#039;five&#039;, [ &#039;six&#039;, &#039;seven&#039; ] ] ]

def show(label,object):
    print &#039;%10s: %s&#039; % (label, object)

# ok, recursion in python seems to suck big time,
# so here&#039;s a fucked up iterative version ;-/
def flatten(arr):
    i = 0
    while i new( [ \@_ ] )-&gt;Indent(0)-&gt;Terse(1)-&gt;Dump;
}

sub flatten {    # it&#039;s your job to flatten other kinds of refs ;-)
    return map { ref $_ ? flatten(@$_) : $_ } @_;
}

sub zip {
    map { my $i = $_; [ map { $_-&gt;[$i] } @_ ] } 0 .. max map { $#{$_} } @_;
}

show(&#039;join&#039;, $fruits_str);
show(&#039;split&#039;, split / - /, $fruits_str);
show(&#039;zip&#039;, zip(\@months, \@days, \@numbers));
show(&#039;flatten&#039;, flatten($aoa));
show(&#039;map&#039;, map { $_**2 } @numbers);
show(&#039;reduce&#039;, reduce { $a + $b } @numbers);
show(&#039;select&#039;, grep { /^[aeiou]/i } @fruits);
show(&#039;reject&#039;, grep { !/^[aeiou]/i } @fruits);

$ ./yu_li_yan_p126.pl
      join: [&#039;apple - banana - cherry - orange - plum&#039;]
     split: [&#039;apple&#039;,&#039;banana&#039;,&#039;cherry&#039;,&#039;orange&#039;,&#039;plum&#039;]
       zip: [[&#039;Jan&#039;,31,1],[&#039;Feb&#039;,28,2],[&#039;Mar&#039;,31,3],[&#039;Apr&#039;,30,4],[&#039;May&#039;,31,5],[&#039;Jun&#039;,30,6]]
   flatten: [&#039;one&#039;,&#039;two&#039;,&#039;three&#039;,&#039;four&#039;,&#039;five&#039;,&#039;six&#039;,&#039;seven&#039;]
       map: [1,4,9,16,25,36]
    reduce: [21]
    select: [&#039;apple&#039;,&#039;orange&#039;]
    reject: [&#039;banana&#039;,&#039;cherry&#039;,&#039;plum&#039;]

$ cat ./yu_li_yan_p126.js
// use JS v1.8 please, I&#039;m too lazy ;-)
fruits     = [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;, &#039;orange&#039;, &#039;plum&#039;]
fruits_str = fruits.join(&#039; - &#039;)
months     = [&#039;Jan&#039;, &#039;Feb&#039;, &#039;Mar&#039;, &#039;Apr&#039;, &#039;May&#039;, &#039;Jun&#039;]
days       = [   31,    28,    31,    30,    31,    30]
numbers    = [    1,     2,     3,     4,     5,     6]
aoa = [ &#039;one&#039;, [ &#039;two&#039;, &#039;three&#039; ], &#039;four&#039;, [ &#039;five&#039;, [ &#039;six&#039;, &#039;seven&#039; ] ] ]

function inspect(object) {
  if(typeof object == &quot;undefined&quot;) return &#039;undefined&#039;;
  if(object === null) return &#039;null&#039;;
  if(typeof object == &quot;number&quot;) return object
  if(object instanceof Array)
    return &#039;[&#039; + object.map(inspect).join(&#039;, &#039;) + &#039;]&#039;;
  return &quot;&#039;&quot; + object + &quot;&#039;&quot;;
}

function show(label, object) {
  label = new Array(11 - label.length).join(&#039; &#039;) + label + &#039;:&#039;;
  print(label, inspect(object));
}

function flatten(x) {
  var ret = [];
  x.forEach( function(i) {
    ret = ret.concat(i instanceof Array ? flatten(i) : [i]);
  });
  return ret;
}

function zip() {
  var arrays = Array.prototype.slice.apply(arguments, [0]);
  var max = Math.max.apply( null, arrays.map(function(x) x.length) );
  var ret = [];
  for(var i=0; i &lt; max; i++)
    ret.push(arrays.map(function(x) x[i]));
  return ret;
}

show(&#039;join&#039;, fruits_str)
show(&#039;split&#039;, fruits_str.split(&#039; - &#039;))
show(&#039;zip&#039;, zip(months, days, numbers));
show(&#039;flatten&#039;, flatten(aoa))
show(&#039;map&#039;, numbers.map(function(x) x*x))
show(&#039;reduce&#039;, numbers.reduce(function(x,y) x+y))
show(&#039;select&#039;, fruits.filter(function(x) x.match(/^[aeiou]/i)))
show(&#039;reject&#039;, fruits.filter(function(x) !x.match(/^[aeiou]/i)))

$ js ./yu_li_yan_p126.js
      join: &#039;apple - banana - cherry - orange - plum&#039;
     split: [&#039;apple&#039;, &#039;banana&#039;, &#039;cherry&#039;, &#039;orange&#039;, &#039;plum&#039;]
       zip: [[&#039;Jan&#039;, 31, 1], [&#039;Feb&#039;, 28, 2], [&#039;Mar&#039;, 31, 3], [&#039;Apr&#039;, 30, 4], [&#039;May&#039;, 31, 5], [&#039;Jun&#039;, 30, 6]]
   flatten: [&#039;one&#039;, &#039;two&#039;, &#039;three&#039;, &#039;four&#039;, &#039;five&#039;, &#039;six&#039;, &#039;seven&#039;]
       map: [1, 4, 9, 16, 25, 36]
    reduce: 21
    select: [&#039;apple&#039;, &#039;orange&#039;]
    reject: [&#039;banana&#039;, &#039;cherry&#039;, &#039;plum&#039;]

Hopefully, your blogging software won&#039;t funk up the above code too much ;-)

Ah, and my apologies to the other languages, I&#039;m too lazy ;-)

cheers</description>
		<content:encoded><![CDATA[<p>Hehe, ok, as much as I like the D&amp;D analogy, I have to note that we&#8217;re talking about different things though, as /birth names/ are a given that the others *have to* accept as such (ok, even if you nick them), while these concepts don&#8217;t. For instance, I doubt there&#8217;s anyone else besides those from Ruby communities who would think that &#8220;inject&#8221; is the same thing as &#8220;reduce&#8221; <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>++ IIRC PHP&#8217;s functions are not as flexible as everyone else&#8217;s (and looking at array_combine&#8217;s spec seems to prove it).</p>
<p>++ Python has a different opinion about &#8220;zipping&#8221;, truncating the list of tuples to the length of the shortest argument.</p>
<p>It&#8217;s a boring Sunday, so let&#8217;s give your readers some more examples (using your use cases)&#8230;</p>
<p>$ cat ./yu_li_yan_p126.rb<br />
#!/usr/bin/ruby</p>
<p>fruits     = %w(apple banana cherry orange plum);<br />
fruits_str = fruits.join(&#8216; &#8211; &#8216;);<br />
months     = %w(Jan Feb Mar Apr May Jun);<br />
days       = [   31, 28, 31, 30, 31, 30];<br />
numbers    = (1..6).to_a;<br />
aoa = [ 'one', [ 'two', 'three' ], &#8216;four&#8217;, [ 'five', [ 'six', 'seven' ] ] ];</p>
<p>def show(label, object)<br />
    printf &#8220;%10s: %s\n&#8221;, label, object.inspect<br />
end</p>
<p>show(&#8216;join&#8217;, fruits_str);<br />
show(&#8217;split&#8217;, fruits_str.split(&#8216; &#8211; &#8216;));<br />
show(&#8216;zip&#8217;, months.zip(days, numbers));<br />
show(&#8216;flatten&#8217;, aoa.flatten);<br />
show(&#8216;map&#8217;, numbers.map {|x| x**2});<br />
#show(&#8216;reduce&#8217;, numbers.reduce(:+)); # needs v1.8.7 <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
show(&#8216;reduce&#8217;, numbers.inject {|x,y| x+y});<br />
#show(&#8217;select&#8217;, fruits.select {|x| x =~ /^[aeiou]/i});<br />
show(&#8217;select&#8217;, fruits.grep(/^[aeiou]/i));<br />
#show(&#8216;reject&#8217;, fruits.reject {|x| x =~ /^[aeiou]/i});<br />
show(&#8216;reject&#8217;, fruits.grep(/^[^aeiou]/i));</p>
<p>$ ./yu_li_yan_p126.rb<br />
      join: &#8220;apple &#8211; banana &#8211; cherry &#8211; orange &#8211; plum&#8221;<br />
     split: ["apple", "banana", "cherry", "orange", "plum"]<br />
       zip: [["Jan", 31, 1], ["Feb", 28, 2], ["Mar", 31, 3], ["Apr", 30, 4], ["May", 31, 5], ["Jun", 30, 6]]<br />
   flatten: ["one", "two", "three", "four", "five", "six", "seven"]<br />
       map: [1, 4, 9, 16, 25, 36]<br />
    reduce: 21<br />
    select: ["apple", "orange"]<br />
    reject: ["banana", "cherry", "plum"]</p>
<p>$ cat ./yu_li_yan_p126.py<br />
#!/usr/bin/python<br />
import re</p>
<p>fruits     = ['apple', 'banana', 'cherry', 'orange', 'plum']<br />
fruits_str = &#8216; &#8211; &#8216;.join(fruits)<br />
months     = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']<br />
days       = [  31,    28,    31,    30,    31,    30 ]<br />
numbers    = range(1, 7)<br />
aoa = [ 'one', [ 'two', 'three' ], &#8216;four&#8217;, [ 'five', [ 'six', 'seven' ] ] ]</p>
<p>def show(label,object):<br />
    print &#8216;%10s: %s&#8217; % (label, object)</p>
<p># ok, recursion in python seems to suck big time,<br />
# so here&#8217;s a fucked up iterative version ;-/<br />
def flatten(arr):<br />
    i = 0<br />
    while i new( [ \@_ ] )-&gt;Indent(0)-&gt;Terse(1)-&gt;Dump;<br />
}</p>
<p>sub flatten {    # it&#8217;s your job to flatten other kinds of refs <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
    return map { ref $_ ? flatten(@$_) : $_ } @_;<br />
}</p>
<p>sub zip {<br />
    map { my $i = $_; [ map { $_-&gt;[$i] } @_ ] } 0 .. max map { $#{$_} } @_;<br />
}</p>
<p>show(&#8216;join&#8217;, $fruits_str);<br />
show(&#8217;split&#8217;, split / &#8211; /, $fruits_str);<br />
show(&#8216;zip&#8217;, zip(\@months, \@days, \@numbers));<br />
show(&#8216;flatten&#8217;, flatten($aoa));<br />
show(&#8216;map&#8217;, map { $_**2 } @numbers);<br />
show(&#8216;reduce&#8217;, reduce { $a + $b } @numbers);<br />
show(&#8217;select&#8217;, grep { /^[aeiou]/i } @fruits);<br />
show(&#8216;reject&#8217;, grep { !/^[aeiou]/i } @fruits);</p>
<p>$ ./yu_li_yan_p126.pl<br />
      join: ['apple - banana - cherry - orange - plum']<br />
     split: ['apple','banana','cherry','orange','plum']<br />
       zip: [['Jan',31,1],['Feb',28,2],['Mar',31,3],['Apr',30,4],['May',31,5],['Jun',30,6]]<br />
   flatten: ['one','two','three','four','five','six','seven']<br />
       map: [1,4,9,16,25,36]<br />
    reduce: [21]<br />
    select: ['apple','orange']<br />
    reject: ['banana','cherry','plum']</p>
<p>$ cat ./yu_li_yan_p126.js<br />
// use JS v1.8 please, I&#8217;m too lazy <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
fruits     = ['apple', 'banana', 'cherry', 'orange', 'plum']<br />
fruits_str = fruits.join(&#8216; &#8211; &#8216;)<br />
months     = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']<br />
days       = [   31,    28,    31,    30,    31,    30]<br />
numbers    = [    1,     2,     3,     4,     5,     6]<br />
aoa = [ 'one', [ 'two', 'three' ], &#8216;four&#8217;, [ 'five', [ 'six', 'seven' ] ] ]</p>
<p>function inspect(object) {<br />
  if(typeof object == &#8220;undefined&#8221;) return &#8216;undefined&#8217;;<br />
  if(object === null) return &#8216;null&#8217;;<br />
  if(typeof object == &#8220;number&#8221;) return object<br />
  if(object instanceof Array)<br />
    return &#8216;[' + object.map(inspect).join(', ') + ']&#8216;;<br />
  return &#8220;&#8216;&#8221; + object + &#8220;&#8216;&#8221;;<br />
}</p>
<p>function show(label, object) {<br />
  label = new Array(11 &#8211; label.length).join(&#8216; &#8216;) + label + &#8216;:&#8217;;<br />
  print(label, inspect(object));<br />
}</p>
<p>function flatten(x) {<br />
  var ret = [];<br />
  x.forEach( function(i) {<br />
    ret = ret.concat(i instanceof Array ? flatten(i) : [i]);<br />
  });<br />
  return ret;<br />
}</p>
<p>function zip() {<br />
  var arrays = Array.prototype.slice.apply(arguments, [0]);<br />
  var max = Math.max.apply( null, arrays.map(function(x) x.length) );<br />
  var ret = [];<br />
  for(var i=0; i &lt; max; i++)<br />
    ret.push(arrays.map(function(x) x[i]));<br />
  return ret;<br />
}</p>
<p>show(&#8216;join&#8217;, fruits_str)<br />
show(&#8217;split&#8217;, fruits_str.split(&#8216; &#8211; &#8216;))<br />
show(&#8216;zip&#8217;, zip(months, days, numbers));<br />
show(&#8216;flatten&#8217;, flatten(aoa))<br />
show(&#8216;map&#8217;, numbers.map(function(x) x*x))<br />
show(&#8216;reduce&#8217;, numbers.reduce(function(x,y) x+y))<br />
show(&#8217;select&#8217;, fruits.filter(function(x) x.match(/^[aeiou]/i)))<br />
show(&#8216;reject&#8217;, fruits.filter(function(x) !x.match(/^[aeiou]/i)))</p>
<p>$ js ./yu_li_yan_p126.js<br />
      join: &#8216;apple &#8211; banana &#8211; cherry &#8211; orange &#8211; plum&#8217;<br />
     split: ['apple', 'banana', 'cherry', 'orange', 'plum']<br />
       zip: [['Jan', 31, 1], ['Feb', 28, 2], ['Mar', 31, 3], ['Apr', 30, 4], ['May', 31, 5], ['Jun', 30, 6]]<br />
   flatten: ['one', 'two', 'three', 'four', 'five', 'six', 'seven']<br />
       map: [1, 4, 9, 16, 25, 36]<br />
    reduce: 21<br />
    select: ['apple', 'orange']<br />
    reject: ['banana', 'cherry', 'plum']</p>
<p>Hopefully, your blogging software won&#8217;t funk up the above code too much <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Ah, and my apologies to the other languages, I&#8217;m too lazy <img src='http://www.iuliandogariu.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alin</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-33</link>
		<dc:creator>Alin</dc:creator>
		<pubDate>Thu, 25 Sep 2008 16:38:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-33</guid>
		<description>A very simple example for *reduce* using Ruby:
&gt;&gt; [1,4,5].inject{&#124;sum,i&#124; sum+i}

&lt;em&gt;More explanations from Iulian: This computes the sum of all the numbers in the array. The thing between curly braces is a &quot;block&quot;, which for all practical purposes is a function. The function has two arguments and gets called for each item of the array. The first param, called &quot;sum&quot;, is the value accumulated so far. That&#039;s why they call it &quot;accumulator&quot; in general. The second param is the current item in the array.&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>A very simple example for *reduce* using Ruby:<br />
&gt;&gt; [1,4,5].inject{|sum,i| sum+i}</p>
<p><em>More explanations from Iulian: This computes the sum of all the numbers in the array. The thing between curly braces is a &#8220;block&#8221;, which for all practical purposes is a function. The function has two arguments and gets called for each item of the array. The first param, called &#8220;sum&#8221;, is the value accumulated so far. That&#8217;s why they call it &#8220;accumulator&#8221; in general. The second param is the current item in the array.</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Iulian Dogariu</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-32</link>
		<dc:creator>Iulian Dogariu</dc:creator>
		<pubDate>Thu, 25 Sep 2008 16:36:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-32</guid>
		<description>Well yes, all these names come from FP indeed. But usually people freak out the moment they hear the words &quot;functional programming&quot;, and won&#039;t continue listening on, to see how useful and intuitive FP really is.</description>
		<content:encoded><![CDATA[<p>Well yes, all these names come from FP indeed. But usually people freak out the moment they hear the words &#8220;functional programming&#8221;, and won&#8217;t continue listening on, to see how useful and intuitive FP really is.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lelela</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-31</link>
		<dc:creator>Lelela</dc:creator>
		<pubDate>Thu, 25 Sep 2008 16:33:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-31</guid>
		<description>Hey, functional programming is very useful regarding vocabulary. I already knew the terms from that class.</description>
		<content:encoded><![CDATA[<p>Hey, functional programming is very useful regarding vocabulary. I already knew the terms from that class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.iuliandogariu.com/2008/09/naming-and-some-common-string-and-array-patterns/comment-page-1/#comment-30</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Thu, 25 Sep 2008 15:24:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.jayomega.net/blog/?p=126#comment-30</guid>
		<description>The Perl way :

&lt;code&gt;
@fruits=(&#039;apples&#039;,&#039;oranges&#039;,&#039;ququxes&#039;,&#039;bananas&#039;);
$&quot; = &#039;-&#039;;
$output = &quot;@fruits&quot;;
&lt;/code&gt;

using join :
&lt;code&gt;
$output = join &#039;-&#039;, @fruits;
&lt;/code&gt;

the same thing with uppercase characters, using map {}
&lt;code&gt;
$output = join &#039;-&#039;, map{ uc } @fruits;
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>The Perl way :</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">@fruits=('apples','oranges','ququxes','bananas');<br />
$&quot; = '-';<br />
$output = &quot;@fruits&quot;;</div></td></tr></tbody></table></div>
<p>using join :</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$output = join '-', @fruits;</div></td></tr></tbody></table></div>
<p>the same thing with uppercase characters, using map {}</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$output = join '-', map{ uc } @fruits;</div></td></tr></tbody></table></div>
]]></content:encoded>
	</item>
</channel>
</rss>
