An easy way to increase Crystal’s profile on the site, and in the coding world overall, is for people to provide the missing examples. The easiest way to do this is to just translate the Ruby version in each category, as a start.
Doing this would not only up Crystal’s profile, it would also be a good exercise for people (newbies, et al) looking to do something tangible with Crystal. This will surely benefit Crystal, by being more visible for more categories, and acting as coding tutorials too.
The instructions for adding new code is on the above site. It’s may be initially confusing at first (it was for me), but once you do it once it makes sense (it could be made much simpler though).
Submitting Rosetta Code examples works on multiple levels, for different types of people.
If you are a new (to Crystal) programmer, it’s a concrete way to learn the nuances of Crystal as compared to other languages. For the Farey sequence I learned (unfortunately) the Ruby example cannot be used. So I used the Lua version, and then went to Wikipedia to see where that implementation came from. So I learned a lot (on multiple levels) about what doesn’t work in Crystal and some math. In both cases I consider that a win-win.
The key point of the exercises is to start using Language X as a tutorial for the language. Like learning any subject, you start with the basics. If you already know the language, yes, it might seem boring (like taking Algebra 101 over again), but the examples are definitely helpful for people wanting to learn syntax, structure, and vocabulary for some language.
In fact for Crystal, the first thing to do is to translate the Ruby version, just to see the level of compatibility. I think people might be vexed to see how much Crystal’s typing system effects how much you have to do to get a direct Ruby translation to work.
Ultimately though, I hope it’s not arguable that having more Crystal examples is better, than not.
You can actually omit the lines after {{out}} if you don’t submit output. You just put in the code for whatever task you’re doing, then click Edit at the top of the page for the task, and insert the templated code in the alphabetically correct place in the list (usually before the D entry). It’s really fairly simple. All I did was see how other examples were done and copied them. Here’s the whole templated code for the Ackermann Function.
=={{header|Crystal}}==
{{trans|Ruby}}
<lang crystal>def ack(m, n)
if m == 0
n + 1
elsif n == 0
ack(m-1, 1)
else
ack(m-1, ack(m, n-1))
end
end
#Example:
(0..3).each do |m|
puts (0..6).map { |n| ack(m, n) }.join(' ')
end
</lang>
{{out}}
<pre>
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 5 7 9 11 13 15
5 13 29 61 125 253 509
</pre>
Once you submit the code in the correct place in the task list, you can preview it, and when satisfied that it’s correct, then you Save it, and supply a comment in the Summary field at the bottom. I usually put something like Added Crystal translation of Ruby version, if that was the case. This is logged into the history system for the site.
I had started translating the Ruby FFT example a couple of days ago, and hadn’t figured out exactly how to do it that way in Crystal, and went on and did some others first. So I went back today, and someone had beat me to it, and posted code for it, but there were a couple of problems with it. First, it wouldn’t compile as written (needed require "complex"), and take as input the example input vector. Second, it didn’t have an output statement and results.
However, the approach the person took was very similar to mine, and fixing the input vector issue to take the example array was simple. So I simplified it more, and added an output statement and results, and reposted it.
One thing I’ve noticed when using Complex numbers, sometimes the output results aren’t exact for whole number answers, e.g 1.0 is ouput as 0.99999999999. I know this is inherit in roundoff errors using floating point, but it would be very nice if internally Crystal would set a better limit parameter, or whatever. In Ruby I created my own custom cos and sin functions in my Roots gem to always give the correct whole number values for angles of 0, Pi/2, Pi, etc. I just made sure for these given angles I forced the result to be the correct integer.
Actually for this last one someone had beat me again from when I first looked at it, but I learned two (2) things from submitting translations of the Ruby versions (which I wasn’t going to waste after doing).
Crystal has a lenvenshtein module already (who knew? why? )
In Ruby you can do: arry.each_slice(2) do |a, b| .... use a and b
in Crystal must do: arry.each_slice(2) do |pair| a, b = pair ...
Levenshtein is there for the compiler, for “did you mean?”. But its API is not the best one. We should probably move it to the Crystal namespace and hide it.
About automatic slicing in Ruby (that is, do |a, b|): you can do it in Crystal too but only with Tuple. If it’s an Array, or anything that’s indexable, you can use do |(a, b)|, which explicitly unpacks it (works with tuples too).
Hey thanks!. I tried – do |(a,b)| – in Levenshtein example, it works, and changed the Rosetta code to show both versions. This isn’t documented for arrays though (at least I didn’t find it, and I looked hard).
Also, I’m learning so many Ruby->Crystal idiom changes it would be a nice to have a document on the website to point to specific list and describe them. I have found at least 8-10 now that I can list alone. It would have helped me if I had that list to work from.
I found this below, but it appears to be inactive.