Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Build a system that even a fool can use and only a fool will want to use it.


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

SubjectAuthor
o An "adapter", superset of an iteratorfedor tryfanau

1
An "adapter", superset of an iterator

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: fedor.tr...@gmail.com (fedor tryfanau)
Newsgroups: comp.lang.python
Subject: An "adapter", superset of an iterator
Date: Wed, 3 May 2023 12:45:36 +0300
Lines: 93
Message-ID: <mailman.19.1683131042.13552.python-list@python.org>
References: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de qz2DeDyWmv0jAybJ1Haj3Q38tLVlLX9K38AfIwrbQAuw==
Return-Path: <fedor.tryfanau@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=qzlC4YzW;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.032
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; '(which': 0.04; 'argument':
0.04; 'def': 0.04; 'filter': 0.07; 'python.': 0.08; '*not*': 0.09;
"list's": 0.09; 'readable': 0.09; 'skip:_ 20': 0.09; 'skip:x 10':
0.09; '(excluding': 0.16; '(it': 0.16; '3.10': 0.16; 'accessed':
0.16; 'formally': 0.16; 'lambda': 0.16; 'solves': 0.16; 'third-
party': 0.16; 'tuple': 0.16; 'python': 0.16; 'solve': 0.19;
'libraries': 0.19; 'to:addr:python-list': 0.20; "i've": 0.22;
'exception': 0.22; 'returns': 0.22; 'skip:_ 10': 0.22; 'version':
0.23; '(and': 0.25; 'programming': 0.25; 'zip': 0.26; 'function':
0.27; 'done': 0.28; 'wrong': 0.28; 'code,': 0.31; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'release':
0.34; 'able': 0.34; "didn't": 0.34; 'skip:" 20': 0.34;
'received:google.com': 0.34; 'one.': 0.35; 'from:addr:gmail.com':
0.35; 'functions': 0.36; 'those': 0.36; 'really': 0.37; 'using':
0.37; "it's": 0.37; 'received:209.85': 0.37; 'class': 0.37;
'this.': 0.37; 'way': 0.38; 'could': 0.38; '8bit%:14': 0.38;
'received:209': 0.39; 'valid': 0.39; 'list': 0.39; 'use': 0.39;
'received:209.85.222': 0.39; 'should': 0.40; 'skip:h 10': 0.61;
'to:': 0.62; 'skip:i 20': 0.62; 'gives': 0.62; 'skip:b 10': 0.63;
'8bit%:9': 0.64; 'range': 0.64; 'key': 0.64; 'skip:r 20': 0.64;
'accessing': 0.64; 'imagine': 0.64; 'introducing': 0.64;
'benefit': 0.65; 'tool': 0.65; '8bit%:7': 0.67; 'items': 0.68;
'order.': 0.69; 'perfectly': 0.69; 'sequence': 0.69; 'type:':
0.69; 'too.': 0.70; '8bit%:6': 0.71; 'competitive': 0.74; '3rd':
0.81; '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=1683107148; x=1685699148;
h=to:subject:message-id:date:from:mime-version:from:to:cc:subject
:date:message-id:reply-to;
bh=ZKRnmACm+o+d/vL3YgSuy+NQbeU+XLx5c3xP6klhoc8=;
b=qzlC4YzWaqcXhlir2gHAr5uh6R2Tce9mKFS5UrNn78XrdvoeMI2EcicZ5TmjELHk5S
BE5+7oJdte/VeIqG/JwkNEGif+5Q35lk8IM4w981zs/MsqLT0nFEScIYleVhSgnpM6GP
F1mweSzVVqeAebCbdwVnVl2Kc0MRgLYA2ex8/QHbl9QiueC0GMyI4fWbv0fWi7rOfRTJ
ygdjFzK5eN2vMrQWnOujFfoi0xKyX3u0gZJlQIWuBC9E4y5hf5QcdsGvlbBPVcFEoZON
yb/kdDtGLDTSlkWUAtFBIPHggLWGeBA5FPp/CwiGe7EeLwectvoMC7AHXBAhyjnT0L+2
llkA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20221208; t=1683107148; x=1685699148;
h=to:subject:message-id:date:from:mime-version:x-gm-message-state
:from:to:cc:subject:date:message-id:reply-to;
bh=ZKRnmACm+o+d/vL3YgSuy+NQbeU+XLx5c3xP6klhoc8=;
b=LPg/bR20fh29S9BQeBUtVy4h7QZW5WDAjWR53uW1yvvd0aNtZDxfTgV3lrgI12BTOW
obNTCX1Bc+OdvhCqFZusVBEqa6837V0sEQodbQWYQ64r88iiHkdMC3nejY/Zn28qQJsV
05Ai25N9/cIQno0BmXGEQcrpxxE5LQIy+u8NeDjFfgENpe4JkfrajG64VaZHcFhTe6Fd
MBIGPFQvsN2cM+C0OC62PdfIt06xT12yvxirvkmaNEXIQigWZ0kJtFMf0xjT/MT4Do0s
2uIDTWHfLoPwi6uVEqZhsWOSDRRLu8XyqMitU+V8fRkA9/pxu/lvZu6+P9zZ2lMMRzx9
Mr7w==
X-Gm-Message-State: AC+VfDxgAN6r+PdOfqfLltpwzsFi8n6zs2inYeGVmSgHXMKsdiG7SFPY
4M9tmjWD9t/DUu63Tf6P1ZdcN+BXaa2k/FwTsKa+EXA9tLI=
X-Google-Smtp-Source: ACHHUZ79osEK5yYxDJzlZBFqLZhQt/cAzuTyhJF5l0d5HZNtjyf//o1BxeXYwPS2jc3qYfloy5Y3PI5trTFRF41ObwE=
X-Received: by 2002:a05:6214:519d:b0:5ef:5245:df77 with SMTP id
kl29-20020a056214519d00b005ef5245df77mr8790333qvb.35.1683107147926; Wed, 03
May 2023 02:45:47 -0700 (PDT)
X-Mailman-Approved-At: Wed, 03 May 2023 12:24:00 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CA+n10uirbqmn21yRG9haY_Va=PiBpOnTJC3ugK3TtrZ9K5ThFQ@mail.gmail.com>
 by: fedor tryfanau - Wed, 3 May 2023 09:45 UTC

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.
```
#possible implementation
m=map(lambda x:x+1,[1,2,3,4,5])
#lambda is not called
print(m[3])# gives 5 by calling the lambda on list's 3rd element, which is 4
#simplified implementation
class map:
def __init__(self,f,a):
self.f=f
self.a=a
def __getitem__(self,idx):
return self.f(self.a[idx])
def __len__(self):
return len(self.a)
```
enumerate should really return an adapter
```
#simplified implementation
class enumerate:
def __init__(self,a):
self.a = a
def __getitem__(self,idx):
return idx,self.a[idx]
def __len__(self):
return len(self.a)
```
reversed should really return an adapter
```
#simplified implementation
class reversed:
def __init__(self,a):
self.a = a
self.length=len(a)
def __getitem__(self,idx):
return self.a[self.length-idx-1]
def __len__(self):
return self.length
```
zip should really return an adapter
range should really return an adapter
filter should *not* return an adapter

All of those functions return an adapter and take in an adapter. But some
(excluding "reversed") should be able take an iterator and return an
iterator.
So the way I imagine a possible release version to work is that
"reversed(enumerate(a))" works if a is an adapter and throws an exception
if not

Perhaps there should be even an adapter comprehension:
```
Add1 = (a+1 for a)
#is equivalent to
Add1 = lambda s: map((lambda a: a+1),s)
transformed = Add1([1,2,3])
print(transformed[1])# should be 3
```

This adapter feature also allows you to not use a key argument "key=" for
certain functions (which could be non-existing). For example bisect.bisect
functions before 3.10 didn't have a key= argument. Some third-party
libraries could do this with it's functions too.

(Subject to change)


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

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor