Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32704
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Haskell -> Python |
| Date | 2012-11-03 16:29 +0000 |
| Message-ID | <XnsA100A7C85E1E5duncanbooth@127.0.0.1> (permalink) |
| References | <b19e3922-d86f-426f-afb8-1f75b793f87b@googlegroups.com> <mailman.3222.1351892486.27098.python-list@python.org> |
Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Nov 2, 2012 at 1:19 PM, <foster63@gmail.com> wrote:
>> Is there anything anyone could recommend to make it more "Pythonic"
>> or more functional. It looks clumsy next to the Haskell.
>
> def options(heaps):
> for i, heap in enumerate(heaps):
> head = heaps[:i]
> tail = heaps[i+1:]
> yield from (head + [x] + tail for x in range(heap))
>
> "yield from" is Python 3.3 syntax. If you're not using Python 3.3,
> then that line could be replaced by:
>
> for x in range(heap):
> yield head + [x] + tail
>
> Cheers,
> Ian
An alternative that is closer to foster63's original but still more
"Pythonic" for some definitions of those words.
def options(heaps):
if not heaps: return []
head, *tail = heaps
for h in range(head):
yield [h]+tail
for t in options(tail):
yield [head]+t
For a more 'functional' version there is also the Python 3.3 variant:
def options(heaps):
if not heaps: return []
head, *tail = heaps
yield from ([h]+tail for h in range(head))
yield from ([head]+t for t in options(tail))
--
Duncan Booth http://kupuguy.blogspot.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Haskell -> Python foster63@gmail.com - 2012-11-02 12:19 -0700
Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 15:56 -0400
Re: Haskell -> Python Simon Foster <simon.foster@inbox.com> - 2012-11-02 20:09 +0000
Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 15:40 -0600
Re: Haskell -> Python Duncan Booth <duncan.booth@invalid.invalid> - 2012-11-03 16:29 +0000
Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 15:46 -0600
Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 18:24 -0400
Re: Haskell -> Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-02 16:27 -0600
Re: Haskell -> Python Dave Angel <d@davea.name> - 2012-11-02 22:03 -0400
Re: Haskell -> Python aahz@pythoncraft.com (Aahz) - 2012-11-03 22:16 -0700
csiph-web