digitalmars.D - From APL

"bearophile" <bearophileHUGS lycos.com> writes:
```The paper touches only a small subset of what's needed to write
modern program, it's mostly about array operations and related
matters. It compares some parts of the old Fortran 88 with parts
of the dead APL language. The author seems fond of APL, and
several things written in the paper are unfair:

"Fortran 88 Arrays - Paper Clips and Rubber Bands" (2001), by
Robert Bernecky:
http://www.snakeisland.com/fortran8.htm

Some of the ideas of APL are widely used in modern functional
languages. D contains array operations and Phobos contains some
higher order operations similar to some APL verbs and adverbs.

------------------

Section 2.3 is about Scan operations, that are like reduce or
fold, but keep all the intermediate results too:

+\ of 3 1 2 4

is 3 4 6 10

Some lazy scans are present in the Haskell Prelude too (and in
Mathematica) (the Prelude contains functions and constants that
are loaded on default):
http://zvon.org/other/haskell/Outputprelude/scanl_f.html

I think scans are not present in Phobos. Maybe one or two are
worth adding.

------------------

Section 2.5 suggests to generalize the dot product:

If the + and * of the Fortran 77 DO loops for inner product are
replaced by other functions, a whole family of interesting inner
products appear,<

Some examples of usage (in J language):

Associative search                   *./..=y
Inverted associative                 x+./..~:y
Minima of residues for primes        x<./..|y
Transitive closure step on Booleans  y+./..*.y
Minima of maxima                     x<./..>.y

Maybe a higher order function named "dot" (that takes two
callables) is worth adding to Phobos. But I am not sure.

------------------

Section 2.6 reminds us that a transpose() is a commonly useful
function. A transpose is handy to have in Phobos.

Bye,
bearophile
```
Sep 20 2012
"Simen Kjaeraas" <simen.kjaras gmail.com> writes:
```------------QLwcgRpqxl91mosI3qOrko
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 7bit

On Thu, 20 Sep 2012 13:55:48 +0200, bearophile <bearophileHUGS lycos.com>
wrote:

Section 2.3 is about Scan operations, that are like reduce or fold, but
keep all the intermediate results too:

+\ of 3 1 2 4

is 3 4 6 10

Some lazy scans are present in the Haskell Prelude too (and in
Mathematica) (the Prelude contains functions and constants that are
loaded on default):
http://zvon.org/other/haskell/Outputprelude/scanl_f.html

I think scans are not present in Phobos. Maybe one or two are worth
adding.

See attached. Mayhaps I should post this as a pull request?

------------------

Section 2.5 suggests to generalize the dot product:

If the + and * of the Fortran 77 DO loops for inner product are
replaced by other functions, a whole family of interesting inner
products appear,<

Some examples of usage (in J language):

Associative search                   *./..=y
Inverted associative                 x+./..~:y
Minima of residues for primes        x<./..|y
Transitive closure step on Booleans  y+./..*.y
Minima of maxima                     x<./..>.y

Maybe a higher order function named "dot" (that takes two callables) is
worth adding to Phobos. But I am not sure.

So that's basically mapReduce, right? We've got that, just not as succinct.

--
Simen
------------QLwcgRpqxl91mosI3qOrko
Content-Disposition: attachment; filename=scan.d
Content-Type: application/octet-stream; name="scan.d"
Content-Transfer-Encoding: Base64

aW1wb3J0IHN0ZC5hbGdvcml0aG07CmltcG9ydCBzdGQucmFuZ2U7CmltcG9ydCBz
dGQudHJhaXRzOwppbXBvcnQgc3RkLmZ1bmN0aW9uYWw7CmltcG9ydCBzdGQudHlw
ZXR1cGxlOwoKdGVtcGxhdGUgc2NhbihhbGlhcyBmdW4pCnsKICAgIGF1dG8gc2Nh
bihULCBSYW5nZSkoVCBzdGFydCwgUmFuZ2UgcikgaWYgKGlzSW5wdXRSYW5nZSEo
VW5xdWFsIVJhbmdlKSkKICAgIHsKICAgICAgICBhbGlhcyBiaW5hcnlGdW4hZnVu
IF9mdW47CiAgICAgICAgCiAgICAgICAgc3RydWN0IFJlc3VsdAogICAgICAgIHsK
ICAgICAgICAgICAgYWxpYXMgVW5xdWFsIVJhbmdlIFI7CiAgICAgICAgICAgIGFs
aWFzIHR5cGVvZihfZnVuKFQuaW5pdCwgLkVsZW1lbnRUeXBlIVIuaW5pdCkpIEVs
ZW1lbnRUeXBlOwogICAgICAgICAgICBSIF9pbnB1dDsKICAgICAgICAgICAgRWxl
bWVudFR5cGUgX2Zyb250OwogICAgICAgICAgICAKICAgICAgICAgICAgdGhpcyhU
IHN0YXJ0LCBSIGlucHV0KQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBf
ZnJvbnQgPSBzdGFydDsKICAgICAgICAgICAgICAgIF9pbnB1dCA9IGlucHV0Owog
ICAgICAgICAgICB9CgogICAgICAgICAgICBzdGF0aWMgaWYgKGlzSW5maW5pdGUh
UikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgLy8gUHJvcGFnYXRlIGlu
ZmluaXRlLW5lc3MuCiAgICAgICAgICAgICAgICBlbnVtIGJvb2wgZW1wdHkgPSBm
YWxzZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAg
IHsKICAgICAgICAgICAgICAgIGJvb2wgX2VtcHR5ID0gZmFsc2U7CiAgICAgICAg
ICAgICAgICBAcHJvcGVydHkgYm9vbCBlbXB0eSgpCiAgICAgICAgICAgICAgICB7
CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIF9lbXB0eTsKICAgICAgICAgICAg
ICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICAgICAgdm9pZCBwb3BGcm9udCgp
CiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHN0YXRpYyBpZiAoIWlzSW5m
aW5pdGUhUikgewogICAgICAgICAgICAgICAgICAgIGlmIChfaW5wdXQuZW1wdHkp
IHsKICAgICAgICAgICAgICAgICAgICAgICAgX2VtcHR5ID0gdHJ1ZTsKICAgICAg
ICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAg
ICBpZiAoIWVtcHR5KSB7CiAgICAgICAgICAgICAgICAgICAgX2Zyb250ID0gX2Z1
bihfZnJvbnQsIF9pbnB1dC5mcm9udCk7CiAgICAgICAgICAgICAgICAgICAgX2lu
cHV0LnBvcEZyb250KCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0K
ICAgICAgICAgICAgCiAgICAgICAgICAgIEBwcm9wZXJ0eSBhdXRvIHJlZiBmcm9u
dCgpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHJldHVybiBfZnJvbnQ7
CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIHN0YXRpYyBp
ZiAoaGFzTGVuZ3RoIVIgfHwgaXNTb21lU3RyaW5nIVIpCiAgICAgICAgICAgIHsK
ICAgICAgICAgICAgICAgIEBwcm9wZXJ0eSBhdXRvIGxlbmd0aCgpCiAgICAgICAg
ICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIF9pbnB1dC5sZW5n
dGg7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgYWxpYXMgbGVu
Z3RoIG9wRG9sbGFyOwogICAgICAgICAgICB9CgogICAgICAgICAgICBzdGF0aWMg
aWYgKGlzRm9yd2FyZFJhbmdlIVIpCiAgICAgICAgICAgICAgICBAcHJvcGVydHkg
YXV0byBzYXZlKCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAg
ICBhdXRvIHJlc3VsdCA9IHRoaXM7CiAgICAgICAgICAgICAgICAgICAgcmVzdWx0
Ll9pbnB1dCA9IHJlc3VsdC5faW5wdXQuc2F2ZTsKICAgICAgICAgICAgICAgICAg
ICByZXR1cm4gcmVzdWx0OwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KCiAg
ICAgICAgcmV0dXJuIFJlc3VsdChzdGFydCwgcik7CiAgICB9Cn0KCnVuaXR0ZXN0
IHsKICAgIGF1dG8gYXJyID0gc2NhbiEoKGEsYik9PmErYikoMCwgWzEsMiwzLDRd
KTsKICAgIAogICAgYXNzZXJ0KGVxdWFsKGFyciwgWzAsMSwzLDYsMTBdKSk7CiAg
ICAKICAgIGltcG9ydCBzdGQuc3RkaW87CiAgICAKICAgIHdyaXRlbG4oIHNjYW4h
ImErYiIoMCwgY2FzdChpbnRbXSlbXSkgKTsKfQoKdm9pZCBtYWluKCApIHsKCn0=

------------QLwcgRpqxl91mosI3qOrko--
```
Sep 21 2012