Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The memory management on the PowerPC can be used to frighten small children. -- Linus Torvalds


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

SubjectAuthor
* Re: An "adapter", superset of an iteratorChris Angelico
`- Re: An "adapter", superset of an iteratorGreg Ewing

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

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: An "adapter", superset of an iterator
Date: Thu, 4 May 2023 07:29:14 +1000
Lines: 48
Message-ID: <mailman.22.1683149367.13552.python-list@python.org>
References: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
<CAPTjJmra48rPc0b-==WeibDfRTPbbuzPgqy_OzKoDJQgQ_2zrg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de hZohiaWN5cHKTnX2maBI3g0RW5Ryqx6yPJU1H3lxdsrA==
Return-Path: <rosuav@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=Uq8JCtj5;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.048
X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; '2023': 0.07; 'open.':
0.07; 'python.': 0.08; 'general,': 0.09; 'readable': 0.09; '(it':
0.16; 'accessed': 0.16; 'chrisa': 0.16; 'formally': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'received:209.85.218': 0.16; 'solves': 0.16; 'tuple': 0.16;
'wrote:': 0.16; 'python': 0.16; 'probably': 0.17; 'solve': 0.19;
'thu,': 0.19; 'to:addr:python-list': 0.20; "i've": 0.22; 'first,':
0.22; 'returns': 0.22; 'version': 0.23; '(and': 0.25;
'programming': 0.25; 'function': 0.27; 'done': 0.28; 'example,':
0.28; 'wrong': 0.28; 'code,': 0.31; 'elements': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'able':
0.34; 'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'one.': 0.35; 'from:addr:gmail.com':
0.35; 'really': 0.37; 'using': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'this.': 0.37; 'received:209': 0.39;
'quite': 0.39; 'otherwise': 0.39; 'valid': 0.39; 'list': 0.39;
'use': 0.39; 'should': 0.40; 'to:': 0.62; 'skip:r 20': 0.64;
'about.': 0.64; 'accessing': 0.64; 'introducing': 0.64; 'benefit':
0.65; 'tool': 0.65; 'exactly': 0.68; 'items': 0.68; 'order': 0.69;
'order.': 0.69; 'perfectly': 0.69; 'sequence': 0.69; 'type:':
0.69; 'competitive': 0.74; 'left': 0.83; 'converted': 0.84;
'feature,': 0.84; 'reversed': 0.84; 'type.': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20221208; t=1683149366; x=1685741366;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=vkSayX98AvWG29EgV9wxhqkiFOIoDzwFW4WAs1ZUB3Q=;
b=Uq8JCtj55kxRJg/B9z65Qy+TyPuuWEyYbC7a6bxx/QfoRSN7mgCjVsX19y3b6FtTLP
Ye2ywgJ+JRfQv6LNqHmVC7lcdXXXnjVqTC9HooBiinGuAJ1PIf81YkKGVPq5VkT04DAf
hwwPdLaPs8XQAHQs8z54p+e8/06Y7LrPDfOI+h69DiYFrksuP68rgyEBGdniKIq3tVL4
zcQpPTxlNprM+UDFGcdD/794qnoDQ5d80D3ib6nO/0n81Yj5hMQD10zUFdgUgYldJUNs
le+fN+w5HJFwLeBcxqhjCRnR7QZEZCgxo2faPfpGLl70BFzMsP+zg7SaDrnFBU+TIIjc
2bjA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1683149366; x=1685741366;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=vkSayX98AvWG29EgV9wxhqkiFOIoDzwFW4WAs1ZUB3Q=;
b=iHY7ZNtiB+XxqMllgRUyPG+1c8BXW6JBCEazhj7UhTjtyO1TSbROpJRyfJTTk5L85r
UhDnnvW/BE/VTxiiTOu4Q+BU306HaQA2zdj1jYRA3knj1c6J+X3OPgjShsGaFNBjlDhs
gqy4ERVzwJeNS8AzGl9mjnF9MdikglIjxA/icCVHGog92nbNSTVxpHj0KImQ2bNjjkAk
yIfbTltVPRt1oIvTc7gphCq1m/FAE0LRm/iSWQrMRKETiQe6HYl3UHLqUeUW3Ovs85RP
R3YopbT8xziqAIWW0uS1ubwHtnCl9/hadrT5FsO1Bfm7R2rbtYzRuDqpVkvYmXlo46i/
hKtQ==
X-Gm-Message-State: AC+VfDwlZHujoegPqhrQGfOO2fj1ngwHo2ompj7UVrQGVdfvD+VsSfnt
IyOuOfTLybqXtCmh7AazOI7FEbvuvqwaG8FCIbAkL6SK
X-Google-Smtp-Source: ACHHUZ7Miaa40KyUAMl5Elw+ZqeiwON8saa5g0Jk99KK9H2gqL0M2reo1DV+coQfjGcBWmJ8WY4orQuz/JQnuHcjm2Q=
X-Received: by 2002:a17:906:ef09:b0:961:8fcd:53b3 with SMTP id
f9-20020a170906ef0900b009618fcd53b3mr4911692ejs.10.1683149365707; Wed, 03 May
2023 14:29:25 -0700 (PDT)
In-Reply-To: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
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: <CAPTjJmra48rPc0b-==WeibDfRTPbbuzPgqy_OzKoDJQgQ_2zrg@mail.gmail.com>
X-Mailman-Original-References: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
 by: Chris Angelico - Wed, 3 May 2023 21:29 UTC

On Thu, 4 May 2023 at 02:25, fedor tryfanau <fedor.tryfanau@gmail.com> 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.
>
> The feature I am describing (and proposing) solves this.
> Introducing an adapter type: this is an iterator, but it's items can be
> accessed out of order.
> More formally it has to:
> 1. Have __getitem__ to allow access by index
> 2. Have __len__
> 3. Be immutable
> (It is a lot like the sequence protocol)
>
> An adapter can be converted to an iterator by accessing it from 0 to
> len(adapter). Which is done by iter(). (or by __iter__, I don't know which
> implementation would be "right")
> ```
> iter(a)
> #is equivalent to
> (a[i] for i in range(len(a)))
> ```
> For example any tuple is a valid adapter and any list can be easily
> converted to one.
>
> Built-in adapter-generators:
> "map" function should really return an adapter.

The trouble with that is that map() already accepts any iterator. So
you're asking for map to be able to return an iterator if given an
iterator, or an adapter if given an adapter. That makes it quite
complicated to use and reason about.

In general, it's probably safest to just coalesce things to list when
you need to:

reversed(list(enumerate(a)))

because otherwise there are many MANY questions left open. For
example, in what order are the elements of a retrieved? with
reversed(enumerate(a)) it's not clear, but if you force to list first,
it is (and you know exactly when they're accessed too).

ChrisA

Re: An "adapter", superset of an iterator

<kbgatvFa3qkU1@mid.individual.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: An "adapter", superset of an iterator
Date: Thu, 4 May 2023 12:01:01 +1200
Lines: 20
Message-ID: <kbgatvFa3qkU1@mid.individual.net>
References: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
<CAPTjJmra48rPc0b-==WeibDfRTPbbuzPgqy_OzKoDJQgQ_2zrg@mail.gmail.com>
<mailman.22.1683149367.13552.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net XhBaijhm7tJbezpF/htVoQIWBexqrRyOrhIyoLrAIdEE8F70dx
Cancel-Lock: sha1:hLEi9TDgUfkYoWkyBW3Rl24O2Po=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Content-Language: en-US
In-Reply-To: <mailman.22.1683149367.13552.python-list@python.org>
 by: Greg Ewing - Thu, 4 May 2023 00:01 UTC

On 4/05/23 9:29 am, Chris Angelico wrote:
> So
> you're asking for map to be able to return an iterator if given an
> iterator, or an adapter if given an adapter. That makes it quite
> complicated to use and reason about.

Also a bit slower, since it would need to inspect its argument
and decide what to do with it. Currently it can just get on
with its job and rely on duck typing to do the right thing.

Maybe there could be a parallel set of functions "enumerated",
"mapped", etc. that take sequences and return sequence views.

Although that naming convention would suggest that reversed()
itself should return a sequence view rather than an iterator.
That would require restricting it to working on sequences,
which would be an incompatible change.

--
Greg

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor