[Tutor] Multifunction mapping

dman dman@dman.ddts.net
Tue, 30 Apr 2002 11:46:19 -0500


--hoZxPH4CaxYzWscb
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Apr 30, 2002 at 11:23:46AM -0500, Jordan, Jay wrote:
| how would it be possible to multi finction a map statement.

You only use 1 function.  The complexity of that function is up to
you.

| Consider the following.=20
|=20
| I have a sequence of characters  (a,b,c,d,e,f,g,h,i)
| I want to convert the whole list to their ascii codes (ord)
| (61,62,63,64,65,66,67,68,69)

That's not right.  'a' =3D> 97.  'A' =3D> 65.

| I want to zfill them all to three digits
| (061,062,063,064,065,066,067,068,069)
|
| and then concatenate the whole list into a single string
| (061062063064065066067068069)

reduce(
    # here's the concatenate function
    lambda s1 , s2 : s1+s2 ,
    map(
        # here's the formatting function
        lambda ch : "%3.3d" % ord(ch) ,
        # here's the data, a sequence of characters
        ('A','B','C','D','E','F','G','H','I')
    )
)


It would also work like this :

reduce(
    lambda s1 , s2 : s1+s2 ,
    map(
        lambda ch : "%3.3d" % ord(ch) ,
        'ABCDEFGHI'
    )
)


| Can that be done with a single map statement?

Yep.

| Python is such an elegant language for doing big complicated things
| with just a very few statements so

It is.  This example works a lot like LISP or Scheme due to the
functional-style of processing the list.

-D

--=20

All a man's ways seem innocent to him,
but motives are weighed by the Lord.
        Proverbs 16:2
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--hoZxPH4CaxYzWscb
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjzOylsACgkQO8l8XBKTpRSm/ACdE7vt3QRtR4QJImv4LScCVAr8
Y6cAnRuFtdeX4Y41/Suk4Kb7gK7/u9Re
=sZR2
-----END PGP SIGNATURE-----

--hoZxPH4CaxYzWscb--