unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Doing something different for the last element of a sequence (partition)?
@ 2020-11-16 10:28 Tim Landscheidt
  2020-11-16 11:10 ` 2QdxY4RzWzUUiLuE
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Landscheidt @ 2020-11-16 10:28 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

I have a list of URLs (all-urls) and I want to launch a
Firefox window for the first x URLs, then wait y seconds,
then launch another one, etc., and then prompt the user if
the source of URLs can be deleted (provided that everything
went smoothly).

If I do:

| (seq-doseq (firefox-urls (seq-partition (seq-sort all-urls #'string<) x))
|   (apply 'call-process "firefox" nil "*firefox*" nil firefox-urls)
|   (sleep-for y))

that works fine, except that it also waits for y seconds af-
ter the last launch.

Looking at seq's toolbox, it seems to me that the only way
to avoid that last sleep would be:

- seq-sort the URLs alphabetically,
- then seq-partition,
- then seq-reverse,
- then use seq-map-indexed to add the indexes to the subse-
  quences in a kind of Schwartzian transform, marking the
  logically last partition with the index 0,
- then seq-reverse,
- then seq-doseq and sleep only if the index is not 0.

Ugh.  Is there something I missed?  Some form of mapconcat
that accepts a function as separator?

Tim




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Doing something different for the last element of a sequence (partition)?
  2020-11-16 10:28 Doing something different for the last element of a sequence (partition)? Tim Landscheidt
@ 2020-11-16 11:10 ` 2QdxY4RzWzUUiLuE
  2020-11-16 11:40   ` Tim Landscheidt
  0 siblings, 1 reply; 3+ messages in thread
From: 2QdxY4RzWzUUiLuE @ 2020-11-16 11:10 UTC (permalink / raw)
  To: help-gnu-emacs

On 2020-11-16 at 10:28:52 +0000,
Tim Landscheidt <tim@tim-landscheidt.de> wrote:

> Hi,
> 
> I have a list of URLs (all-urls) and I want to launch a
> Firefox window for the first x URLs, then wait y seconds,
> then launch another one, etc., and then prompt the user if
> the source of URLs can be deleted (provided that everything
> went smoothly).
> 
> If I do:
> 
> | (seq-doseq (firefox-urls (seq-partition (seq-sort all-urls #'string<) x))
> |   (apply 'call-process "firefox" nil "*firefox*" nil firefox-urls)
> |   (sleep-for y))
> 
> that works fine, except that it also waits for y seconds af-
> ter the last launch.

Or handle the first one as the special case (completely untested):

    (cl-do (((firefox-urls (seq-partition (seq-sort all-urls #'string<) x))
             (delay 0 y)))
      (sleep-for delay)
      (apply 'call-process "firefox" nil "*firefox*" nil firefox-urls))



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Doing something different for the last element of a sequence (partition)?
  2020-11-16 11:10 ` 2QdxY4RzWzUUiLuE
@ 2020-11-16 11:40   ` Tim Landscheidt
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Landscheidt @ 2020-11-16 11:40 UTC (permalink / raw)
  To: help-gnu-emacs

2QdxY4RzWzUUiLuE@potatochowder.com wrote:

>> I have a list of URLs (all-urls) and I want to launch a
>> Firefox window for the first x URLs, then wait y seconds,
>> then launch another one, etc., and then prompt the user if
>> the source of URLs can be deleted (provided that everything
>> went smoothly).

>> If I do:

>> | (seq-doseq (firefox-urls (seq-partition (seq-sort all-urls #'string<) x))
>> |   (apply 'call-process "firefox" nil "*firefox*" nil firefox-urls)
>> |   (sleep-for y))

>> that works fine, except that it also waits for y seconds af-
>> ter the last launch.

> Or handle the first one as the special case (completely untested):

>     (cl-do (((firefox-urls (seq-partition (seq-sort all-urls #'string<) x))
>              (delay 0 y)))
>       (sleep-for delay)
>       (apply 'call-process "firefox" nil "*firefox*" nil firefox-urls))

Inspiration!  I initially looked at seq-reduce, but dismiss-
ed it because I was focussing on the /last/ partition.  But
it is rather simple:

| (let
|     ((all-urls (mapcar 'number-to-string (number-sequence 1 25))))
|   (message "all-urls = %S" all-urls)
|   (seq-reduce
|    (lambda (not-first-call firefox-urls)
|      (if not-first-call
|          (message "Would sleep"))
|      (message "firefox-urls = %S" firefox-urls)
|      t)  ;; Set not-first-call for subsequent calls.
|    (seq-partition (seq-sort #'string< all-urls) 3)
|    nil))

Thanks!

Tim




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-11-16 11:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-16 10:28 Doing something different for the last element of a sequence (partition)? Tim Landscheidt
2020-11-16 11:10 ` 2QdxY4RzWzUUiLuE
2020-11-16 11:40   ` Tim Landscheidt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).