The Crystal Programming Language Forum

Improve Crystal's profile on Rosetta Code

Mon 2019/4/22

This was a little tricky to translate. The Ruby version used slick booleans for flow control (which I did both ways to verify). The real problem was having to explicitly initialize the arrays to false (unecessary in Ruby). I also applied the lesson learned from previously using slices, in the output. Man, I’m getting good at this. :smile:

Jaro distance

1 Like

Tu 2019/4/23

Sierpinski carpet

@jzakiya Great work! One small input valuable when translating from Ruby: when doing [a, b].max it’s better to do {a, b}.max. The tuple will be allocated in the stack and things could be optimized probably by llvm much better.

Thanks for the feedback @bcardiff. I changed in [].max|min to {}.max|min.

Tu 2019/4/23


Josephus problem

Another small snippet I notice is when reading ARGV that happens a lot. All of the following lines return the same value.

ARGV.empty? ? 41 : ARGV[0].to_i
ARGV[0]?.try(&.to_i) || 41
ARGV.fetch(0, "41").to_i
ARGV.fetch(0, 41).to_i

Beauty is in the eyes of the beholder :slight_smile:.

Thanks again @bcardiff for these equivalents examples, which I didn’t know about. I think the 2nd one conforms closest to the Ruby snippet spirit.

I hope at some point documentation is created to extract from these code examples these explicit snippets alternatives, and put them in an easily searchable|accessible form.

Only by using a language (program or human), and doing real things with it, will these types of nuances and options be known, which can then be accessed for appropriate use.

I’m not only learning more of real world Crystal, but also Ruby too, doing these problems (which is exactly the point).

Keep the feedback coming. It’s useful to everyone who will ultimately view this thread.

F 2019/4/26

A simple one.

Binary digits

A not so simple one.
Apparently someone posted a Ruby translation just before I posted my Python translation. I was going to do the Ruby version too, but couldn’t figure out how to use Iterators for it. If someone knows how to do (a short version) with iterators please post. I’d like to know, if it’spossible.

Bernoulli numbers

Addition: Sat 2019/4/27
Added Python version 2 Bernoulli number generator translation, which (at least to me) is much simpler and understandable than Ruby|Crystal enumerator inspired versions (and likely faster too).

1 Like

Sun 2019/4/28

String manipulation in Crystal is far less elegant and concise than Ruby.
And I can’t splat a tuple in an output expression (?).

Ruby vs Crystal equivalents

  g is a hash

  k = g.sort_by { |k, v| v.length }.map { |k, v| k }         # Ruby
  k = g.to_a.sort_by { |h| h[1].size }.map { |h| h[0] }      # Crystal
  k = g.to_a.sort_by { |(k, v)| v.size }.map { |(k, v)| k }

  new[p] = letters.slice! i                                  # Ruby
  new = new.sub(p, letters[i]); letters = letters.sub(i, "") # Crystal

%w(abracadabra seesaw elk grrrrrr up a).each do |word|
  # puts "%s, %s, (%d)" % [word, *best_shuffle(word)]       # Ruby
  new, score = best_shuffle(word)                           # Crystal
  puts "%s, %s, (%d)" % [word, new, score]

Crystal (currently) doesn’t have sort_by method for hashes.

Are there better|concise alternative phrases for Crystal?

Best shuffle

Ruby has Enumberale#sort and Enumerable#sort_by which first convert the Enumerable to an array, so there’s a hidden performance penalty. Crystal makes it more explicit by having you write to_a. I think that’s good.

For the hash thing you can do:

g.to_a.sort_by { |(k, v)| v.size }.map { |(k, v)| v }
# or

Crystal strings are immutable so yeah, Crystal string manipulation will always be a bit more boring than in Ruby.

F 2019/5/3

A key thing to remember using Arrays|Hashes in Crystal, you have to initialize all the container values because you can’t do control flow on nil|false|true like in Ruby with uninitialized values.

Generate random chess position

Pythagorean quadruples

Pythagorean triples

Th 2019/5/9

Gamma function

M 2019/5/13
Added C# translation of Lanczos method.

W 2019/5/22

Binary search