On 02/23/2011 01:57 PM, Andrei Alexandrescu wrote:
On 2/23/11 5:10 AM, Jacob Carlborg wrote:
On 2011-02-23 03:28, bearophile wrote:
This is a Scala implementation of a function that prints the carpet:


def nextCarpet(carpet: List[String]): List[String] = (
carpet.map(x => x + x + x) :::
carpet.map(x => x + x.replace('#', ' ') + x) :::
carpet.map(x => x + x + x))

def sierpinskiCarpets(n: Int) =
(Iterator.iterate(List("#"))(nextCarpet) drop n next) foreach println

Again Scala shines with its beautiful lambdas compared to Ds ugly string
version.

But then

Iterator.iterate(List("#"))(nextCarpet) drop n next) foreach println

a sort of a stuttering Yoda evokes.

lol :-)

It just requires being used to stack-based-like lingo; meaning each step is written in chrono-logical order, from left to right. Well, actually, the 'List("#")' part looks like a useless hack, and 'Iterator.iterate' is somewhat verbose. I'd prefere:
    (sequence(["#"])(nextCarpet) drop n next) foreach println

Denis
--
_________________
vita es estrany
spir.wikidot.com

Reply via email to