comp.lang.ada
 help / color / mirror / Atom feed
From: Paul Rubin <no.email@nospam.invalid>
Subject: Re: Generators/coroutines in future Ada?
Date: Wed, 12 Jul 2017 10:34:32 -0700
Date: 2017-07-12T10:34:32-07:00	[thread overview]
Message-ID: <87k23d4l07.fsf@nightsong.com> (raw)
In-Reply-To: ok2jfj$t2$1@dont-email.me

"J-P. Rosen" <rosen@adalog.fr> writes:
> AFAIU, generators are functions with states, where the returned value at
> one call depends on the state that was left behind by the previous call.
> This can easily be achieved with a function within a package, where the
> package serves to protect and hide the function's state.

It's not always so easy though.  Generators can yield from other
generators, so doing them as functions with states can require the
states to be stacks or collections of stacks.  Doing it with tasks is
more natural, but Ada tasks can be heavyweight, e.g. mapping to Posix
threads.

In Haskell, generators aren't even explicit: every list is automatically
a generator.  Here's an example which would be quite messy in Ada:
a 5-smooth number a/k/a Hamming number is a number with no prime factors
greater than 5.  The first 20 of them are:

  1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36

Problem: find the 1500th Hamming number (answer: 859963392).  This is a
classic Haskell problem solved by merging corecursively generated lists:

    hamming = 1 : m 2 `merge` m 3 `merge` m 5 where
      m k = map (k *) hamming
      xxs@(x:xs) `merge` yys@(y:ys) = case x `compare` y of
        LT -> x : (xs `merge` yys)
        EQ -> x : (xs `merge` ys)
        GT -> y : (xxs `merge` ys)

    main = print (hamming !! 1499)

This prints the answer instantly (0.004 seconds elapsed, mostly in the
operating system).  I remember doing it in C++ and it was a page or so
of ugly code.


  parent reply	other threads:[~2017-07-12 17:34 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-09 20:19 Generators/coroutines in future Ada? Victor Porton
2017-07-09 21:28 ` Dennis Lee Bieber
2017-07-09 23:56   ` Victor Porton
2017-07-10  7:33   ` Dmitry A. Kazakov
2017-07-10 13:38 ` Victor Porton
2017-07-10 17:01   ` Paul Rubin
2017-07-10 21:24     ` Victor Porton
2017-07-12 17:09       ` Paul Rubin
2017-07-12 17:39         ` Victor Porton
2017-07-13  6:35           ` Paul Rubin
2017-07-11  9:42   ` J-P. Rosen
2017-07-11 12:52     ` Victor Porton
2017-07-11 13:01       ` Victor Porton
2017-07-11 13:26         ` J-P. Rosen
2017-07-11 16:04           ` Dennis Lee Bieber
2017-07-11 16:59           ` Dmitry A. Kazakov
2017-07-11 19:52             ` Pascal Obry
2017-07-11 20:18               ` Dmitry A. Kazakov
2017-07-11 18:36           ` Victor Porton
2017-07-11 19:22             ` J-P. Rosen
2017-07-11 20:25               ` Dmitry A. Kazakov
2017-07-11 23:19                 ` Victor Porton
2017-07-12  4:54                   ` J-P. Rosen
2017-07-12 13:07                     ` Victor Porton
2017-07-12 13:38                       ` Dmitry A. Kazakov
2017-07-12  5:35                   ` Randy Brukardt
2017-07-12  7:27                     ` Dmitry A. Kazakov
2017-07-12 22:47                     ` Shark8
2017-07-16 13:11                       ` Robert Eachus
2017-07-17 23:54                       ` Randy Brukardt
2017-07-18  7:38                         ` Dmitry A. Kazakov
2017-07-12  7:19                   ` Dmitry A. Kazakov
2017-07-12  6:35                 ` G.B.
2017-07-12  7:34                   ` Dmitry A. Kazakov
2017-07-12 20:49                     ` G.B.
2017-07-13  8:18                       ` Dmitry A. Kazakov
2017-07-12 17:34           ` Paul Rubin [this message]
2017-07-11 19:27         ` Simon Wright
2017-07-12  5:42     ` darkestkhan
2017-07-12  8:57     ` Maciej Sobczak
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox