[Tutor] Strange join syntax

dman dman@dman.ddts.net
Fri, 26 Apr 2002 15:33:38 -0500


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

On Fri, Apr 26, 2002 at 08:03:59PM +0200, Scot Stevenson wrote:
| Hello Remco,=20
|=20
| While talking about list reverse, you gave an example that included
|=20
| > drow =3D ''.join(wordlist)

Consider the following python-ish java-ish pseudo-ish code.


interface Joiner :
    public abstract object join( self , sequence )


# this is the built-in string type
class string( Joiner ) :

    # note: untested, but might work, and is _not_ efficient
    def join( self , sequence ) :
        res =3D str( sequence[0] )
        for item in sequence[1:] :
            res +=3D self + str( item )
        return res


The '' at the beginning is a string object, the empty string.  Strings
are "joiners" and know how to join a sequence of items together.  Thus
you can pass a list of things to a string object's join() method and
it joins them together (returning a string).


I agree that it looks odd at first, but once you get used to the fact
that literals (strings, lists, etc) are still objects, and once the
"joiner" interface seems reasonable, then the whole thing makes
perfect sense and is nicely compact.

-D

--=20

How great is the love the Father has lavished on us,
that we should be called children of God!
        1 John 3:1=20
=20
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg


--i7F3eY7HS/tUJxUd
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

iEYEARECAAYFAjzJuaIACgkQO8l8XBKTpRTNbACfYefY0Q9ff94x7N3m/wC9d6xF
upEAoLe9oqs7G7hrHfto4LKAywGMn110
=wPuq
-----END PGP SIGNATURE-----

--i7F3eY7HS/tUJxUd--