Counting dup

For the Compleat Fan
Locked
m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Counting dup

Post by m i c h a e l »

Lutz,

This isn't really a request so much as sharing an idea: What would you think of adding $idx to dup?

Something like this:

Code: Select all

> (dup (format "%c" $idx) 10)
("" "\001" "\002" "\003" "\004" "\005" "\006" "\007" "\008" "\t")
> _
Of course, we can do it this way:

Code: Select all

> (map (fn (ea) (format "%c" ea)) (sequence 0 9))
("" "\001" "\002" "\003" "\004" "\005" "\006" "\007" "\008" "\t")
Or even:

Code: Select all

> (map (curry format "%c") (sequence 0 9))
("" "\001" "\002" "\003" "\004" "\005" "\006" "\007" "\008" "\t")
But neither of these seems as elegant as the dup solution. What do you think?

m i c h a e l

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

for that specific problem your are citing, the shortest solution would be:

Code: Select all

(map char (sequence 0 9))
but perhaps you were thinking in general of a method to create lists of strings, where each element is related to the previous in some form or the other.

Unfortunately 'dup' evaluates its second argument only once, then duplicates the result. But we could generalize the 'series' function, which already offers most of what we are looking for:

(series <num-start> <func> <num-count>) as currently in:

Code: Select all

(series 1 (fn (x) (* 2 x)) 5) => '(1 2 4 8 16)
to a new syntax pattern where we can start with any expression:

(series <exp-start> <func> <num-count>) as in:

Code: Select all

(series "a" (fn (c) (char (inc (char c)))) 5)  ; in future version

=> ("a" "b" "c" "d" "e")
So func could work not only on numbers but on any data-type in <exp-start>. It turns out a very small change in the existing 'series' function makes this possible and you will see this in the next version.

ps: note that 'series' also includes $idx already so you also will be able to do:

Code: Select all

(series "\000" (fn () (char (+ $idx 1))) 5) ; in future version
=> ("\000" "\001" "\002" "\003" "\004")
in this case fn() simply ingnores the incoming argument and only looks on $idx

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

That's great, Lutz! series is one of those functions I've overlooked in the past, so now maybe I'll start using it more.

m i c h a e l

Locked