[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: repeat function



i came up with
	fn repeat n cmd {
		let (counter =) {
			while {!~ $#counter $n} {
				$cmd
				counter = $counter 1
			}
		}
	}
it has two disadvantages:
	(a) the counter list grows proportional to n, which is inefficient
	(b) an ill-formed number causes infinite recursion
(b) is more serious, so a slight improvement is
	fn repeat n cmd {
		let (counter =) {
			result $counter($n)
			while {!~ $#counter $n} {
				$cmd
				counter = $counter 1
			}
		}
	}
(note the ``useless'' result statement inside the let.) this version
will raise an exception if the number is not legal as a subscript.
that should catch most problems.
as for (a), nothing short of adding arithmetic to the shell would
really address that. people have proposed that for es, but Byron & i
are very hesitant. we know some shells have arithmetic or at least
an expr builtin, but some shells have !-history, too.
paul

AltStyle によって変換されたページ (->オリジナル) /