In lista infinita?

A

andrea

Ho notato che i generatori anche se infiniti non si lamentano se usati
in modo potenzialmente "pericoloso".
Dato:
m = (sum(xrange(1,x)) for x in count(1))

10 in m -> True
ma se gli passo un valore che non c'è naturalmente loopa all'infinito
perché non ha modo di sapere se prima o poi troverà il numero.

Ora assumendo una sequenza non decrescente con questa funzioncina si può
effettivamente avere una "pigrizia" a là haskell.

--8<---------------cut here---------------start------------->8---
def in_inf(gen, el):
x = dropwhile(lambda x: x < el, gen)
if x.next() == el:
return True
else:
return False
--8<---------------cut here---------------end--------------->8---

Che dite può andare?
Magari si potrebbe passare la funzione di controllo come argomento per
generalizzarla.
Altri miglioramenti/utilizzi trasversali?
 
A

Andreas Waldenburger

Ho notato che i generatori anche se infiniti non si lamentano se usati
in modo potenzialmente "pericoloso".
[...]
Altri miglioramenti/utilizzi trasversali?

Maybe. But I'm sure it.comp.lang.python might help you better. And from
the looks of it, you seem to have started a similar thread there
(called "Generatori infiniti").

Generally, you'll fare better with English (even broken English will be
fine.) in this group. It's just nicer for everyone if they can
understand all messages and not feel left out.

Anyway, good luck with your question. I didn't understand a whole lot
of it, but I guess you want to test for membership in an infinite
list/generator. Yes, possible in principle, but obviously eats memory
for larger generators. Also, consumes the generators, which is also a
factor.

/W
 
A

Andrea Crotti

Maybe. But I'm sure it.comp.lang.python might help you better. And from
the looks of it, you seem to have started a similar thread there
(called "Generatori infiniti").

Generally, you'll fare better with English (even broken English will be
fine.) in this group. It's just nicer for everyone if they can
understand all messages and not feel left out.

Anyway, good luck with your question. I didn't understand a whole lot
of it, but I guess you want to test for membership in an infinite
list/generator. Yes, possible in principle, but obviously eats memory
for larger generators. Also, consumes the generators, which is also a
factor.

I'm sorry of course it was a mistake, I wanted to post on it.comp. but
I had this other group open...

Anyway it was not really a question, that code was just something to
check if an element is a member of a generator.
Is always possible to write
a in x()
but if it's not in it doesn't terminate, passing the right function
(in that case it was just increasing sequence) will do the trick..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top