Idea for joined() builtin

Discussion in 'Python' started by Rhamphoryncus, Aug 16, 2007.

  1. I know similar things have been argued before, but little things (like
    the joined name implying a copy) can make a big difference. That and
    I'm providing a simple implementation that works right now, so you
    don't have to wait for it to ever become a builtin. ;)

    >>> joined([], [[1,2,3], [4,5,6]])

    [1, 2, 3, 4, 5, 6]
    >>> joined(' ', ['hello', 'world'])

    'hello world'

    def joined(sep, iterable):
    if hasattr(sep, 'join'):
    return sep.join(iterable) # string-like interface
    else:
    output = type(sep)() # Hopefully an empty container
    for i in iterable:
    output.extend(i) # list-like interface
    output.extend(sep)
    else:
    if sep:
    del output[-len(sep):]
    return output

    A little commentary on the intended usage. There are three common
    ways to "combine" objects:

    The first is adding numbers together. This may be accomplished with a
    +b or sum([a, b]). All values affect the output, but their
    individuality is lost; you cannot extract the original values[1].

    The second is concatenation. This is usually done with list.extend,
    ''.join(), or with my joined function. It can also be done with a+b,
    but the performance is substantially worse if done repeatedly. All
    values affect the output and most of their individuality is retained;
    given the original lengths of the subsequences, you can extract them
    from the combined form.

    The third is the "or" operation of a set or integer. This is done
    with a|b. The values usually have some effect on the output, but as
    redundant entries are removed, they lose a significant part of their
    individuality.

    The important thing to realize is that all of these different ways of
    "combining" objects has different conceptual behaviour and different
    performance characteristics. Get them confused or attempt to over-
    generalize and you will be bitten.


    [1] Math/crypto has some exceptions. Stop mentally poking holes in my
    argument. :)

    --
    Adam Olsen, aka Rhamphoryncus
    Rhamphoryncus, Aug 16, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rb
    Replies:
    0
    Views:
    317
  2. blu3g85

    recently joined member?

    blu3g85, Apr 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    310
    =?Utf-8?B?VE9KTw==?=
    Apr 4, 2004
  3. Ersin Gençtürk

    how to fill typed dataset with joined queries ?

    Ersin Gençtürk, Jan 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    919
    Eliyahu Goldin
    Jan 19, 2005
  4. Christian Riedel
    Replies:
    0
    Views:
    368
    Christian Riedel
    Sep 12, 2003
  5. bdb112
    Replies:
    2
    Views:
    300
    Chris Torek
    Jul 2, 2011
Loading...

Share This Page