星之一角

What have you found for these years?

2009-03-15

印星星

[begin updated2]

[end]

*

`  3 34534567 345  3`

http://gist.github.com/79112

scala 寫起來的感覺嘛，還滿難形容的。好像該有的都有，

list comprehension 更是囉唆到了極點。

every value is an object;
every function is a value.

self, self, self, self, self, selfish... -_-b

[begin updated1]

[end]

`// Star.scalaclass Star(max: Int){  val mid = max / 2 + 1  def row(n: Int): List[Int] =    for (i <- List.range(mid, max + mid - 2 * (mid - n - 1).abs)) yield i  val result =    for (i <- List.range(0, max)) yield      List.range(0, (mid - i - 1).abs).map{s=>' '} ++ row(i)  override def toString: String =    result.foldRight(""){      (l, s) => "\n" + l.mkString + s    }.drop(1).toString}println(new Star(5))`
`-- Star.hsmodule Main wherestar :: Integer -> Stringstar max = tail \$ to_s rectangle where  rectangle :: [[String]]  rectangle = [spaces i ++ row i | i <- [0..max-1]]  spaces i = map (\x->" ") [0..abs (mid-i-1) - 1]  mid   = max `div` 2 + 1  row :: Integer -> [String]  row n = [ show i | i <- [mid..( max+mid-2 * abs (mid-n-1) ) - 1] ]  to_s  = foldr (\l s -> ("\n" ++ concat l ++ s)) ""main = putStrLn \$ star 5`
`# Star.rbclass Star  attr_reader :max, :mid, :result  def initialize max    @max = max    @mid = max / 2 + 1    @result = (0...max).map{ |i| ([' '] * (mid-i-1).abs) + row(i) }  end  def row n    (mid...max + mid - 2 * (mid - n - 1).abs).to_a  end  def to_s    result.inject(''){ |s, l| "\n" + l.join + s }[1..-1]  endendputs Star.new((ARGV.first || 5).to_i)`
`# Star.pyfrom functools import reduceclass Star:  def __init__(self, max):    self.max = max    self.mid = max // 2 + 1    self.result = [ [' '] * abs(self.mid-i-1) +      list(map(lambda x: str(x), list(self.row(i)))) for i in range(max) ]  def row(self, n):    return range(self.mid, self.max + self.mid - 2 * abs(self.mid - n - 1))  def __str__(self):    return reduce(lambda s,l: "\n"+''.join(l)+str(s), self.result, '')[1:]print(Star(5))`

30 retries:

jaiyalas said...

Lin Jen-Shin (godfat) said...

jaiyalas said...

Lin Jen-Shin (godfat) said...

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Lin Jen-Shin (godfat) said...

1. What's missing in the Scala code?
It runs fine on my computer.

2. I am not familiar with Python,
just translating the algorithm
in the Scala code above to Python.
This is my first pythoning time,
so the result is a little ugly I know,
haha.

I'll try to reproduce your Python code later.

Lin Jen-Shin (godfat) said...

You have to manually translate your space to &nbsp;
I can't help with this, this is blogger's behavior.

Anonymous said...

5 error: 'val' expected but identifier found.
def row(n: Int): List[Int] = for (i <- List.range(mid, max + mid - 2 * (mid - n - 1).abs)) yield i
^
:15 error: illegal start of simple pattern
}
^
:14 error: '<-' expected but '}' found.
}.drop(1).toString
^
three errors found

Anonymous said...
This comment has been removed by a blog administrator.
Lin Jen-Shin (godfat) said...

Hmm... I can't figure out why there are such errors.
Here's my Scala version:

godfat ~/p/t/star> scala -version
Scala code runner version 2.7.3.final -- Copyright 2002-2009, LAMP/EPFL

Perhaps you are running older Scala?

Lin Jen-Shin (godfat) said...

You can tell me how to reverse a list with copy in a straightforward way.

Anonymous said...

scala -version
Scala code runner version unknown version -- (c) 2002-2006 LAMP/EPFL

Anonymous said...

why would I? It seems everyone is expert

Anonymous said...

the code
http://gist.github.com/79391

run with python 2.x

Lin Jen-Shin (godfat) said...

Glad to know you have a world full of experts, congratulation!

And what I wish to archive,
is to precess lists and print final result,
not printing along the way.

It's easier to read a lot now.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

to precess lists and print final result, not printing along the way.

1. put the join in your client code for printing only, not in the closure

2. return result instead of return ''.join(result)

Lin Jen-Shin (godfat) said...

Thanks for your tips, I've updated blog post and gist.

update reverse list

Now I can remove copy and stupid reverse method,
it's much much better now.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

the code is worse as my assembly code

Lin Jen-Shin (godfat) said...

It's the algorithm in all Star programs above.
I'm simply translating it to all PL.
In Haskell (the most clean case), it's:

to_s = foldr (\l s -> ("\n" ++ foldr (\i s -> i ++ s) s l)) ""

It's straightforward in functional programming.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Lin Jen-Shin (godfat) said...

I've never said that.
I said it's just straightforward,
and it just jumped out from my brain.
I am not trying to write a perfect Star program.

But you remind me that there's concat in Haskel.

to_s = foldr (\l s -> ("\n" ++ concat l ++ s)) ""

I am thinking how to port this to Python.
Below didn't concat the last line in result.

reduce(lambda s,l: "\n" + ''.join(l) + s.__str__(), self.result[::-1])[1:]

Anonymous said...

besides, str(i) is much better than i.__str__()

Lin Jen-Shin (godfat) said...

And you are really really impolite.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Lin Jen-Shin (godfat) said...

I start to think talking to you are wasting my time, so comments closed.