# Improve Crystal's profile on Rosetta Code

**URL:** <https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513>\
**Category:** Community\
**Created:** [March 4, 2019, 10:27pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513 "2019-03-04T22:27:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 4, 2019, 10:27pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/1 "2019-03-04T22:27:18Z")

</div>

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

[https://rosettacode.org/wiki/Reports:Tasks\_not\_implemented\_in\_Crystal](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).

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 23, 2019, 4:10am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/2 "2019-03-23T04:10:26Z")

</div>

Previously added `Miller-Rabin primality test`.

[https://rosettacode.org/wiki/Miller–Rabin\_primality\_test#Crystal](https://rosettacode.org/wiki/Miller%E2%80%93Rabin_primality_test#Crystal)

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

[https://rosettacode.org/wiki/Farey\_sequence#Crystal](https://rosettacode.org/wiki/Farey_sequence#Crystal)

---

<div class="post-metadata">

**Author:** ![konovod](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/konovod/32/229_2.png) [@konovod](https://forum.crystal-lang.org/u/konovod)\
**Post date:** [March 23, 2019, 11:14am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/3 "2019-03-23T11:14:32Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 23, 2019, 3:20pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/4 "2019-03-23T15:20:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 25, 2019, 10:18pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/5 "2019-03-25T22:18:52Z")

</div>

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

[https://rosettacode.org/wiki/N'th#Crystal](https://rosettacode.org/wiki/N%27th#Crystal)

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 26, 2019, 4:55pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/6 "2019-03-26T16:55:48Z")

</div>

Added 3 more low hanging fruit examples.

`Quick Select`  
[https://rosettacode.org/wiki/Quickselect\_algorithm#Crystal](https://rosettacode.org/wiki/Quickselect_algorithm#Crystal)

`Ackermann Function`  
[https://rosettacode.org/wiki/Ackermann\_function#Crystal](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](https://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Crystal)

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 26, 2019, 5:27pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/7 "2019-03-26T17:27:14Z")

</div>

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

```auto
=={{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`.

```auto
=={{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.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 29, 2019, 5:03pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/8 "2019-03-29T17:03:39Z")

</div>

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

`AKS test for primes`  
[https://rosettacode.org/wiki/AKS\_test\_for\_primes#Crystal](https://rosettacode.org/wiki/AKS_test_for_primes#Crystal)

`Averages/Root Mean Square`  
[https://rosettacode.org/wiki/Averages/Root\_mean\_square#Crystal](https://rosettacode.org/wiki/Averages/Root_mean_square#Crystal)

`Julia Set`  
[https://rosettacode.org/wiki/Julia\_set#Crystal](https://rosettacode.org/wiki/Julia_set#Crystal)

`Roots of Unity`  
[https://rosettacode.org/wiki/Roots\_of\_unity#Crystal](https://rosettacode.org/wiki/Roots_of_unity#Crystal)

`Runge Kata`  
[https://rosettacode.org/wiki/Runge-Kutta\_method#Crystal](https://rosettacode.org/wiki/Runge-Kutta_method#Crystal)

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [March 29, 2019, 7:57pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/9 "2019-03-29T19:57:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [March 29, 2019, 8:54pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/10 "2019-03-29T20:54:34Z")

</div>

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](https://rosettacode.org/wiki/Fast_Fourier_transform#Crystal)

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [March 29, 2019, 9:11pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/11 "2019-03-29T21:11:08Z")

</div>

> [@bcardiff](#):
>
> Using SDL to generate an actual image would be awesome for the Julia Set example

Or [stumpy\_png](https://github.com/stumpycr/stumpy_png)

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [April 1, 2019, 8:42pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/12 "2019-04-01T20:42:22Z")

</div>

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](http://rosettacode.org/wiki/Harshad_or_Niven_series#Crystal)

`Haversine formula`  
[https://rosettacode.org/wiki/Haversine\_formula#Crystal](https://rosettacode.org/wiki/Haversine_formula#Crystal)

`Leonardo Numbers`  
[https://rosettacode.org/wiki/Leonardo\_numbers#Crystal](https://rosettacode.org/wiki/Leonardo_numbers#Crystal)

`Levenshtein distance`  
[https://rosettacode.org/wiki/Levenshtein\_distance#Crystal](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? 😄)
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.

> <https://github.com/crystal-lang/crystal/blob/d81c32c072849cd64c07ef9afa50bc66153fcb66/src/levenshtein.cr#L2>

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [April 2, 2019, 12:59am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/13 "2019-04-02T00:59:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [April 2, 2019, 1:03am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/14 "2019-04-02T01:03:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/paulcsmith/32/301_2.png) [@paulcsmith](https://forum.crystal-lang.org/u/paulcsmith)\
**Post date:** [April 2, 2019, 1:19am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/15 "2019-04-02T01:19:47Z")

</div>

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](https://github.com/luckyframework/lucky_flow/commit/fbdd46357b99f8178c1be12fc3808690b4eddcdd#diff-fbfbcfa95f1d598fd3d333e4ffcdf08eR70)

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [April 2, 2019, 1:40am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/16 "2019-04-02T01:40:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [April 2, 2019, 3:21am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/17 "2019-04-02T03:21:21Z")

</div>

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.

> **[marksiemers/ruby-to-crystal](https://github.com/marksiemers/ruby-to-crystal)**
>
> Examples of Ruby code in Crystal and benchmarks to compare - marksiemers/ruby-to-crystal

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [April 2, 2019, 6:58am UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/18 "2019-04-02T06:58:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [April 16, 2019, 3:59pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/19 "2019-04-16T15:59:31Z")

</div>

Tu 2019/4/16

`Combinations and permutations`  
[https://rosettacode.org/wiki/Combinations\_and\_permutations#Crystal](https://rosettacode.org/wiki/Combinations_and_permutations#Crystal)

`Combinations with repetitions`  
[https://rosettacode.org/wiki/Combinations\_with\_repetitions#Crystal](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#Crystal)

[https://rosettacode.org/wiki/Hickerson\_series\_of\_almost\_integers#Ruby](https://rosettacode.org/wiki/Hickerson_series_of_almost_integers#Ruby)

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [April 20, 2019, 6:16pm UTC](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513/20 "2019-04-20T18:16:49Z")

</div>

`Sum of a series`  
[https://rosettacode.org/wiki/Sum\_of\_a\_series#Crystal](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](https://rosettacode.org/wiki/Sum_multiples_of_3_and_5#Crystal)

[Next page](https://forum.crystal-lang.org/t/improve-crystals-profile-on-rosetta-code/513.md?page=2)
