MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Can you do something like yield in Mathematica?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101949] Re: Can you do something like yield in Mathematica?
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Fri, 24 Jul 2009 06:11:31 -0400 (EDT)
  • References: <h4955f$q91$1@smc.vnet.net>

Hi,

> In Python, the yield statement is a convenient way to create
> generators. see http://ttsiodras.googlepages.com/yield.html. Does
> Mathematica have that, too?

No it doesn't, and I think it hasn't been missed. You should understand
that Mathematica in many ways works very different than python. The
cases where you would need something like a generator are rare and if
the need arises, one can create something that behaves like one without
a special construct. The following is a very simple example of what I
think would be an analogon to a very simple generator:

rangegenerator[min_, max_] := Module[{genfun, count = min},
  genfun[] := If[count > max, Null, count++];
  genfun
  ]

nexti = rangegenerator[1, 4]

While[(i = nexti[]) =!= Null,
 Print[i]
 ]

of course one could elaborate on this, but I think as simple as it is it
is probably easier to understand.

Considering the example in http://ttsiodras.googlepages.com/yield.html:
Mathematica of course has Permutations built in :-).

You will usually get much more helpful answers when you explain what you
try to do (or why you think you need a generator/yield). Many people
here have never used python...

hth,

albert


  • Prev by Date: Re: Thoughts on a Wolfram|Alpha package for
  • Next by Date: Re: Thoughts on a Wolfram|Alpha package for
  • Previous by thread: Re: Can you do something like yield in Mathematica?
  • Next by thread: documentation bug?