Range of n lines from stdin
Marco Leise
Marco.Leise at gmx.de
Fri Dec 27 07:19:15 PST 2013
Am Fri, 27 Dec 2013 14:26:59 +0000
schrieb "Ivan Kazmenko" <gassa at mail.ru>:
> Quick question.
>
> (1) I can do
> n.iota.map!(_ => readln)
> to get the next n lines from stdin.
>
> (2) However, when I do
> readln.repeat(n)
> it looks clearer but works differently: preserves front and reads
> only one line.
>
> (3) In the particular case of readln, we can substitute it with
> stdin.byLine.take(n)
> but the question remains for other impure functions.
>
> So, what I ask for is some non-caching repeat for functions with
> side effects. More idiomatic than (1). Is there something like
> that in Phobos? Is it an OK style to have an impure function in
> an UFCS chain?
>
> If repeat could know whether its first argument is pure, it could
> then enable or disable front caching depending on purity... no
> way currently?
repeat() is only meant to repeat the same first element over
and over. I think it would be wrong if it changed its value
during iteration. A wrapper struct could be more ideomatic:
FuncRange!readln.take(n)
> Ivan Kazmenko.
--
Marco
More information about the Digitalmars-d-learn
mailing list