Improve Crystal's profile on Rosetta Code

There are a lot of code categories on Rosetta code with no Crystal examples.

https://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Crystal

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).

10 Likes

Previously added Miller-Rabin primality test.

https://rosettacode.org/wiki/Miller–Rabin_primality_test#Crystal

Just added Farey sequence (2019/3/23).

https://rosettacode.org/wiki/Farey_sequence#Crystal

3 Likes

I;ve added several entries and it is somawhat funny (similar to adventofcode, without tests and easier, but at least you aren’t just throwing your time away).
https://rosettacode.org/wiki/Special:Contributions/Kipar

But in many cases you can just copy Ruby solution and fix syntax errors - this is boring and doesn’t seems to be the right way.

2 Likes

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.

1 Like

Added N'th (2019/3/25).

https://rosettacode.org/wiki/N'th#Crystal

1 Like

Added 3 more low hanging fruit examples.

Quick Select
https://rosettacode.org/wiki/Quickselect_algorithm#Crystal

Ackermann Function
https://rosettacode.org/wiki/Ackermann_function#Crystal

9 billion names of God the integer
https://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Crystal

1 Like

To help people create submissions I created a template for rosettacode, with the most basic one shown below.

=={{header|Crystal}}==
<lang ruby>


</lang>

{{out}}
<pre>

</pre>

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.

4 Likes

Here are 5 more off the undone list (2019/3/29).

AKS test for primes
https://rosettacode.org/wiki/AKS_test_for_primes#Crystal

Averages/Root Mean Square
https://rosettacode.org/wiki/Averages/Root_mean_square#Crystal

Julia Set
https://rosettacode.org/wiki/Julia_set#Crystal

Roots of Unity
https://rosettacode.org/wiki/Roots_of_unity#Crystal

Runge Kata
https://rosettacode.org/wiki/Runge-Kutta_method#Crystal

2 Likes

Using SDL to generate an actual image would be awesome for the Julia Set example

3 Likes

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.

Fast Fourier Transform
https://rosettacode.org/wiki/Fast_Fourier_transform#Crystal

2 Likes

Or stumpy_png

2 Likes

EDIT: Also added Harshad|Niven series for the haul of the day.

OK, 4 more (2019/4/1), no kidding (April 1st).

Harshad or Niven series
http://rosettacode.org/wiki/Harshad_or_Niven_series#Crystal

Haversine formula
https://rosettacode.org/wiki/Haversine_formula#Crystal

Leonardo Numbers
https://rosettacode.org/wiki/Leonardo_numbers#Crystal

Levenshtein distance
https://rosettacode.org/wiki/Levenshtein_distance#Crystal

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).

  1. Crystal has a lenvenshtein module already (who knew? why? :smile:)
  2. 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 ...

Interesting, here’s the code in the module.

3 Likes

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.

2 Likes

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).

3 Likes

Levenhstein is super useful for shards that want to provide did you mean to enhance their libraries. If you don’t want it in core maybe a shard?

I’d also be happy to make a PR to clean up the API so it’s a bit easier to use but I think it’s also fine as is.

Here’s some example code where I’m using it: https://github.com/luckyframework/lucky_flow/commit/fbdd46357b99f8178c1be12fc3808690b4eddcdd#diff-fbfbcfa95f1d598fd3d333e4ffcdf08eR70

2 Likes

Yeah, maybe Levenshtein is generic enough right now and it’s just missing some docs.

1 Like

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.

1 Like

There is https://github.com/crystal-lang/crystal/wiki/Crystal-for-Rubyists But it’s by no means exhaustive.

2 Likes

Tu 2019/4/16

Combinations and permutations
https://rosettacode.org/wiki/Combinations_and_permutations#Crystal

Combinations with repetitions
https://rosettacode.org/wiki/Combinations_with_repetitions#Crystal

Couldn’t (currently) get the same precision as Ruby, et al. See Ruby version for comparison.

Hickerson series of almost integers
https://rosettacode.org/wiki/Hickerson_series_of_almost_integers#Crystal

https://rosettacode.org/wiki/Hickerson_series_of_almost_integers#Ruby

2 Likes

Sum of a series
https://rosettacode.org/wiki/Sum_of_a_series#Crystal

Sum multiples of 3 and 5
https://rosettacode.org/wiki/Sum_multiples_of_3_and_5#Crystal

2 Likes