Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"I prefer to think that God is not dead, just drunk" -- John Huston


devel / comp.lang.python / RE: An "adapter", superset of an iterator

SubjectAuthor
o RE: An "adapter", superset of an iterator<avi.e.gross

1
RE: An "adapter", superset of an iterator

<mailman.25.1683158087.13552.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=27521&group=comp.lang.python#27521

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: An "adapter", superset of an iterator
Date: Wed, 3 May 2023 19:54:42 -0400
Lines: 85
Message-ID: <mailman.25.1683158087.13552.python-list@python.org>
References: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
<536dd246-8029-47dd-1924-859b540b21cc@tompassin.net>
<CAHVvXxRS8jTgBKsfW=vwFtShaV4v_26ZGDuFVQQbsWYkFSai5A@mail.gmail.com>
<005f01d97e1a$a1b28120$e5178360$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de qI17kxybrsj+60jMAml1nAIRmvR7pS8ESlGya1LzxSXQ==
Return-Path: <avi.e.gross@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=odsghSY8;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.024
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'looks': 0.02; 'random':
0.05; '2023': 0.07; 'loop': 0.07; 'modules': 0.07; 'python.':
0.08; '3:47': 0.09; 'calculating': 0.09; 'infinite': 0.09;
'order,': 0.09; 'patches': 0.09; 'readable': 0.09; 'received:108':
0.09; 'skip:z 20': 0.09; 'url:mailman': 0.15; 'memory': 0.15;
'affects': 0.16; 'behaviour': 0.16; 'entirety,': 0.16;
'examination': 0.16; 'fibonacci,': 0.16; 'functions.': 0.16;
'iterable': 0.16; 'itertools': 0.16; 'list()': 0.16; 'numbers?':
0.16; 'odd': 0.16; 'prime': 0.16; 'primes': 0.16; 'something.':
0.16; 'somewhat': 0.16; 'wrote:': 0.16; 'python': 0.16; 'message-
id:@gmail.com': 0.18; 'solve': 0.19; 'to:addr:python-list': 0.20;
'input': 0.21; "i've": 0.22; 'basically': 0.22; 'maybe': 0.22;
'returns': 0.22; 'version': 0.23; 'code': 0.23; 'list,': 0.24;
'anything': 0.25; 'skip:- 10': 0.25; 'url-ip:188.166.95.178/32':
0.25; 'url-ip:188.166.95/24': 0.25; 'python,': 0.25;
'url:listinfo': 0.25; 'programming': 0.25; 'url-ip:188.166/16':
0.25; 'again,': 0.26; 'so.': 0.26; 'wednesday,': 0.26; 'function':
0.27; 'done': 0.28; 'ideas': 0.28; 'requests': 0.28; 'wrong':
0.28; 'it,': 0.29; 'whole': 0.30; 'code,': 0.31; 'am,': 0.31;
'module': 0.31; 'url-ip:188/8': 0.31; 'think': 0.32; 'gotten':
0.32; 'language.': 0.32; 'needed,': 0.32; 'python-list': 0.32;
'structure': 0.32; 'unexpected': 0.32; 'but': 0.32; 'there': 0.33;
'someone': 0.34; 'same': 0.34; 'mean': 0.34; 'requires': 0.34;
'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'item': 0.35; 'from:addr:gmail.com':
0.35; 'request': 0.35; 'functions': 0.36; 'those': 0.36;
'missing': 0.37; 'really': 0.37; 'using': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'others': 0.37; 'though': 0.37; 'could':
0.38; 'received:209': 0.39; 'changes': 0.39; 'added': 0.39;
'adding': 0.39; 'otherwise': 0.39; 'mentioned': 0.39; 'likely':
0.61; 'method': 0.61; 'here.': 0.61; 'above': 0.62; 'from:': 0.62;
'to:': 0.62; 'reasonable': 0.62; 'skip:z 10': 0.62; 'simply':
0.63; 'skip:r 20': 0.64; 'others,': 0.64; 're:': 0.64; 'your':
0.64; 'benefit': 0.65; 'tool': 0.65; 'look': 0.65; 'well': 0.65;
'opportunity': 0.66; 'time.': 0.66; 'numbers': 0.67; 'skip:e 20':
0.67; 'items': 0.68; 'further': 0.69; 'and,': 0.69; 'generator':
0.69; 'perfectly': 0.69; 'sequence': 0.69; 'below': 0.69;
'depending': 0.70; 'raised': 0.70; 'global': 0.73; 'little': 0.73;
'competitive': 0.74; 'features': 0.75; 'delegate': 0.76; 'sent:':
0.78; 'delivered': 0.84; 'control.': 0.84; 'converts': 0.84;
'enters': 0.84; 'factory': 0.84; 'feature,': 0.84; 'oscar': 0.84;
'remainder': 0.84; 'reversed': 0.84; 'savings': 0.84; 'type.':
0.84; 'unused': 0.84; 'weird': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1683158085; x=1685750085;
h=content-language:thread-index:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:from:to:cc:subject:date:message-id:reply-to;
bh=KU6f/W40zVMMebvxqkj58s0NsYr30V3307RNQ2uZlnE=;
b=odsghSY8l1274b8MGalS2KjUu1sJvtBaAXyrljTjlrI37ujO88UhJhO2IkXd9oIGUF
f2rSbBYKIePquTXyOVRHvWcLT+SNUQ6zu4qzFQP+UeBCS6cnM43xM2t3AVhDDVHpNSC8
loFlDl5x3HdqTMlmPtcgRHSDVWFpjzM94KchqyyTSjfpR7JyHSOeqw5ptgafF5IqWf1a
MMpn4smTe4ShnSG7aDN3axUUf/FqxJypVp1NXjY+LP+if3rM+ML+HkfJBYPOACmE/cGa
eD11HAkVfZKiVMAIENQ6SOjZi78d5xfE2hmDcWpQRhJI9Dw48FNEFrmhYj0hrPoLaPlD
hikg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1683158085; x=1685750085;
h=content-language:thread-index:content-transfer-encoding
:mime-version:message-id:date:subject:in-reply-to:references:to:from
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=KU6f/W40zVMMebvxqkj58s0NsYr30V3307RNQ2uZlnE=;
b=abB/rWLkzA9qqwMASMGdapayJb25zjHvU/7byZCtjJf1oQj3walIdkzdjTI2ccSy4l
tayNEQgiR8Mj4MQtg9UEbV8nt45dQCsJLX1I7T1SxZCcarvED4XKOeEjDOIqHX2RLU85
MPO3Xj8IpBub4nAo5JnkNXJQ7+Yp3iK47sxLgn9t3a2BJZdLdXt06ohyVIbnADgg+EN7
1sdq1RDohKnlPGaM5vEvmZWNhjoqiXB7x+xvz6aTEJZXuwfCrLwwLYGQC2ZnwmmqumNW
m+tHH8bbbUjEusMTWRUFyT5AsO0ihqTVDBV58YfdFsppUousLGFHSNz2rsZCz9UvCX5Y
TPjA==
X-Gm-Message-State: AC+VfDwnpF9iIdPf8YmuxnMq2ahq5HINBj22XarENTOBzFO5dn096lae
lUcW9Xqv2lKds4EecVYbpr5rI5Pofy4=
X-Google-Smtp-Source: ACHHUZ4VpcX3N0VlSVO9g0dPGBWMj5/Q1zr23RM8PeHB6XEj1HS5Es4JXBkcuEcAiBTb05ZmxFwnwg==
X-Received: by 2002:a05:622a:409:b0:3e8:e986:b20a with SMTP id
n9-20020a05622a040900b003e8e986b20amr3124481qtx.16.1683158084583;
Wed, 03 May 2023 16:54:44 -0700 (PDT)
In-Reply-To: <CAHVvXxRS8jTgBKsfW=vwFtShaV4v_26ZGDuFVQQbsWYkFSai5A@mail.gmail.com>
X-Mailer: Microsoft Outlook 16.0
Thread-Index: AQIUiYde3Cyi+TTteo/Om14uqhNvjwJgYtNrAPUjXsauuHG/wA==
Content-Language: en-us
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <005f01d97e1a$a1b28120$e5178360$@gmail.com>
X-Mailman-Original-References: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
<536dd246-8029-47dd-1924-859b540b21cc@tompassin.net>
<CAHVvXxRS8jTgBKsfW=vwFtShaV4v_26ZGDuFVQQbsWYkFSai5A@mail.gmail.com>
 by: <avi.e.gross@gmail.com> - Wed, 3 May 2023 23:54 UTC

As others have mentioned features added like this need careful examination
not only at effects but costs.

As I see it, several somewhat different ideas were raised and one of them
strikes me oddly. The whole point of an iterable is to AVOID calculating the
next item till needed. Otherwise, you can just make something like a list.

To talk about random access to an iterable is a tad weird as it would mean
you need to get the first N items and store them and return the Nth item
value as well as maintain the remainder of the unused part of the iterable.
Further requests already in the cache would be gotten from there and any
beyond it would require iterating more and adding more to the cache.

So say my iterator returns the first N primes or just those below 100. What
should be the functionality if you request item 1,000?

As for reversing it, that requires you to basically do list(iterable) and
use it up. What if the iterable is infinite as in all the odd numbers?

If you really want an iterable that return something like prime numbers
below some level in reverse order, that can be done by changing the iterable
to create them going downward and that would be a different iterator. But
how easily could you make some iterators go backward? Fibonacci, maybe so.
Other things perhaps not.

But again, as noted, anything already in a list can be set up as an iterator
that returns one item at a time from that list, including in reverse. There
won't be much savings as the data structure inside would likely be spread
out to take all the memory needed, albeit it may simplify the code to look
like it was being delivered just in time.

As with many things in python, rather than asking for a global solution that
affects many others, sometimes in unexpected ways, it may be more reasonable
to make your own patches to your code and use them in ways you can control.
In the case being discussed, you simply need to create a generator function
that accepts an iterator, converts it to a list in entirety, reverses the
list (or deals with it from the end) and enters a loop where it yields one
value at a time till done. This should work with all kinds of iterators and
return what looks like an iterator without any changes to the language.

Of course, I am likely to be missing something. And, certainly, there may
already be modules doing things like the above or the opportunity for
someone to create a module such as the itertools module with nifty little
functions including factory functions.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Oscar Benjamin
Sent: Wednesday, May 3, 2023 3:47 PM
To: python-list@python.org
Subject: Re: An "adapter", superset of an iterator

On Wed, 3 May 2023 at 18:52, Thomas Passin <list1@tompassin.net> wrote:
>
> On 5/3/2023 5:45 AM, fedor tryfanau wrote:
> > I've been using python as a tool to solve competitive programming
problems
> > for a while now and I've noticed a feature, python would benefit from
> > having.
> > Consider "reversed(enumerate(a))". This is a perfectly readable code,
> > except it's wrong in the current version of python. That's because
> > enumerate returns an iterator, but reversed can take only a sequence
type.
>
> Depending on what you want to give and receive, enumerate(reversed(a))
> will do the job here. Otherwise list() or tuple() can achieve some of
> the same things.

I don't think that is equivalent to the intended behaviour:

reversed(enumerate(a)) # zip(reversed(range(len(a))), reversed(a))
enumerate(reversed(a)) # zip(range(len(a)), reversed(a))

In principle for a sequence input enumerate(a) could be something that
behaves like a sequence and therefore could be reiterated or reversed
etc. The enumerate(a).__reversed__ method could then delegate to
a.__reversed__ and a.__len__ if they exist. This could be confusing
though because the possible behaviour of enumerate(a) would be
different depending on the type of a.

--
Oscar
--
https://mail.python.org/mailman/listinfo/python-list


devel / comp.lang.python / RE: An "adapter", superset of an iterator

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor