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.
@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 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.
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.
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).
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
g.to_a.sort_by(&.[1].size).map(&.[0])
Crystal strings are immutable so yeah, Crystal string manipulation will always be a bit more boring than in Ruby.
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.
Hi, I am not a Crystal user, but I am a long-time contributor to the Rosetta code site.
I’ve only just seen this thread and would like to take the opportunity of welcoming you all
I would just like to point out that it would be best if you could bring out what makes crystal a good language to program in when writing examples rather than plain translations of Ruby, as that is more the aim of the site. It is good to have large numbers of language examples too, but the aim of the site is for people to compare solutions and sometimes direct language translations look out of place.
I ran all three tasks on my system: i7-6700HQ, 3.5GHz
For catalan numbers here are typical times:
user system total real
catalan_direct 0.000026 0.000052 0.000078 ( 0.000074)
catalan_rec1 0.139766 0.001143 0.140909 ( 0.141418)
catalan_rec2 0.000003 0.000000 0.000003 ( 0.000003)
Also, maybe you weren’t aware, Crystal has a product method, so it’s more idiomatic to do:
def factorial(n : BigInt) : BigInt
#(1..n).reduce(1.to_big_i) { |acc, i| acc * i }
(1..n).product(1.to_big_i)
end
On my system though, anagrams threw an error (0.33)
Unhandled exception: HTTP::Client::Response#body_io cannot be nil (NilAssertionError)
from ???
from /home/jzakiya/crystal/share/crystal/src/hash.cr:896:11 in '__crystal_main'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:106:5 in 'main'
from __libc_start_main
from ../sysdeps/x86_64/start.S:122:0 in '_start'
from ???