One thing I forgot to point out in my previous response is that the
example you cite, in particular, is crippled by the small size of our
vocabulary (to date). Had we the space to discuss concatmap,
the following implementation would have been available:
seq_from_spec( \(@site1, @site2) )
->seq_concatmap( sub { seq_zip(map seq(split//), @_) } )
->seq_foreach( sub { $counts{"@_"}++ } );
This implementation is much clearer (assuming you know
concatmap).
Concatmap combines concat and
map; it maps a sequence to a sequence of sequences, whose
values are then concatenated to yield the output sequence. For example:
sub upto { 1 .. $_[0] }
sub supto { seq(upto(@_)); }
seq(1..3) -> seq_map(\&upto) -> enumerate;
# 0 => 1
# 1 => 1 2
# 2 => 1 2 3
seq(1..3) -> seq_map(\&supto) -> seq_concat -> enumerate;
# 0 => 1
# 1 => 1
# 2 => 2
# 3 => 1
# 4 => 2
# 5 => 3
seq(1..3) -> seq_concatmap(\&supto) -> enumerate;
# 0 => 1
# 1 => 1
# 2 => 2
# 3 => 1
# 4 => 2
# 5 => 3
Cheers, Tom
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|