Improve Crystal's profile on Rosetta Code

Hello. Thank you!

I updated the Crystal code for catalan numbers with the product method ;) and the anagrams code using response.body.

Yes, anagrams works fine now. :+1:

Addition:
FYI, you can also just use one factorial method, which improves performance.

def factorial(n)
  (1..n).product(1.to_big_i)
end

                     user     system      total        real
catalan_direct   0.000000   0.000062   0.000062 (  0.000057)
catalan_rec1     0.090235   0.000000   0.090235 (  0.090260)
catalan_rec2     0.000003   0.000000   0.000003 (  0.000002)
1 Like

Mon 2020/4/6
Weird numbers
https://rosettacode.org/wiki/Weird_numbers#Crystal

@jzakiya, coulld you please run the benchmark on your laptop, please

Rep-string
http://rosettacode.org/wiki/Rep-string#Crystal

1 Like
The first 25 weird numbers are: 70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310 
  0.031349   0.007468   0.038817 (  0.030550)

Addition:
It’s more idiomatic to write short if statements as one-liners, eg:

if i != j
  divs2 << j            div2 << j if i !=j
end
2 Likes

Mon 2020/4/6

I’ve listed (currently 44) all the Crystal Rosetta Code examples posted here. These have been updated as necessary, to work with versions >= 0.31 which changed from doing / to // for integer math, etc. As newer versions come out, they should continue to be updated to remain current, so having them in a list like this makes it easier to do that.

I think it would be a great idea for the website to have a page that lists all the Rosetta Code examples (including those not posted here) in one place, so users (potential, new, et al) can see tangible examples of how Crystal is used to do real things. I’m even willing to maintain such a page, if no one else is.

What do you think? :thinking:

9 billion names of God the integer (Tu 2020/3/26)
https://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Crystal

Ackermann function (Tu 2020/3/26)
https://rosettacode.org/wiki/Ackermann_function#Crystal

AKS test for primes (Fri 2019/3/29)
https://rosettacode.org/wiki/AKS_test_for_primes#Crystal

Anagrams (Mon 2020/4/6)
https://rosettacode.org/wiki/Anagrams#Crystal

Anti-primes (Mon 2020/4/6)
https://rosettacode.org/wiki/Anti-primes#Crystal

Averages/Root Mean Square (Fri 2019/3/29)
https://rosettacode.org/wiki/Averages/Root_mean_square#Crystal

Benford’s law (Fri 2020/3/13)
https://rosettacode.org/wiki/Benford’s_law#Crystal

Bernoulli numbers (F 2019/4/26)
https://rosettacode.org/wiki/Bernoulli_numbers#Crystal

Best shuffle (Sun 2019/4/28)
https://rosettacode.org/wiki/Best_shuffle#Crystal

Binary digits (F 2019/4/26)
https://rosettacode.org/wiki/Binary_digits#Crystal

Binary search (Wed 2019/5/22)
https://rosettacode.org/wiki/Binary_search#Crystal

Catalan numbers (Mon 2020/4/6)
https://rosettacode.org/wiki/Catalan_numbers#Crystal

Combinations and permutations (Tu 2019/4/16)
https://rosettacode.org/wiki/Combinations_and_permutations#Crystal

Combinations with repetitions (Tu 2019/4/16)
https://rosettacode.org/wiki/Combinations_with_repetitions#Crystal

Fast Fourier Transform (Fri 2019/3/29)
https://rosettacode.org/wiki/Fast_Fourier_transform#Crystal

Farey sequence (Sat 2019/3/23).
https://rosettacode.org/wiki/Farey_sequence#Crystal

Gamma function (Th 2019/5/9)
https://rosettacode.org/wiki/Gamma_function#Crystal

Generate random chess position (Fri 2019/5/3)
https://rosettacode.org/wiki/Generate_random_chess_position#Crystal

Harshad or Niven series (Mon 2019/4/1)
http://rosettacode.org/wiki/Harshad_or_Niven_series#Crystal

Haversine formula (Mon 2019/4/1)
https://rosettacode.org/wiki/Haversine_formula#Crystal

Hickerson series of almost integers (Tu 2019/4/16)
https://rosettacode.org/wiki/Hickerson_series_of_almost_integers#Crystal

Jaro distance (Mon 2019/4/22)
https://rosettacode.org/wiki/Jaro_distance#Crystal

Jortsort (Tu 2019/4/23)
https://rosettacode.org/wiki/JortSort#Crystal

Josephus problem (Tu 2019/4/23)
https://rosettacode.org/wiki/Josephus_problem#Crystal

Julia Set (Fri 2019/3/29)
https://rosettacode.org/wiki/Julia_set#Crystal

Leonardo Numbers (Mon 2019/4/1)
https://rosettacode.org/wiki/Leonardo_numbers#Crystal

Levenshtein distance (Mon 2019/4/1)
https://rosettacode.org/wiki/Levenshtein_distance#Crystal

Lucas-Lehmer test (Sat 2020/4/4)
https://rosettacode.org/wiki/Lucas-Lehmer_test#Crystal

Luhn test of credit card numbers (W 2020/4/1)
https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Crystal

Lychrel numbers (Sat 2020/4/4)
https://rosettacode.org/wiki/Lychrel_numbers#Crystal

Miller-Rabin primality test (Wed 2019/3/19)
https://rosettacode.org/wiki/Miller–Rabin_primality_test#Crystal

Modular exponentiation (Sun 2020/3/29)
https://rosettacode.org/wiki/Modular_exponentiation#Crystal

Modular inverse (Fri 2020/3/6)
https://rosettacode.org/wiki/Modular_inverse#Crystal

Monte Carl methods (Sat 2020/4/4)
https://rosettacode.org/wiki/Monte_Carlo_methods#Crystal

N’th (Mon 2019/3/25)
https://rosettacode.org/wiki/N’th#Crystal

Pythagorean quadruples (Fri 2019/5/3)
https://rosettacode.org/wiki/Pythagorean_quadruples#Crystal

Pythagorean triples (Fri 2019/5/3)
https://rosettacode.org/wiki/Pythagorean_triples#Crystal

Quick Select (Tu 2020/3/26)
https://rosettacode.org/wiki/Quickselect_algorithm#Crystal

Roots of Unity (Fri 2019/3/29)
https://rosettacode.org/wiki/Roots_of_unity#Crystal

Runge Kata (Fri 2019/3/29)
https://rosettacode.org/wiki/Runge-Kutta_method#Crystal

Sierpinski carpet (Tu 2019/4/23)
https://rosettacode.org/wiki/Sierpinski_carpet#Crystal

Sum of a series (Th 2019/4/19)
https://rosettacode.org/wiki/Sum_of_a_series#Crystal

Sum multiples of 3 and 5 (Th 2019/4/19)
https://rosettacode.org/wiki/Sum_multiples_of_3_and_5#Crystal

Weird numbers (Mon 2020/4/6)
https://rosettacode.org/wiki/Weird_numbers#Crystal

5 Likes

I think it’s a great idea.

I could help maintaining that page, too

3 Likes

FASTA format (Fri 2020/4/11)

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

3 Likes

Hi @orangeSi, welcome to the club!

May I make some suggestions, and give you some tips, on getting your code to work.

I copied your code and ran it and it didn’t compile/run because of some coding errors, and structural deficiencies. These can be easily corrected though.

First a quick Tip.
Replace the markup as below to get keyword color highlighting Rosetta Code has for Ruby, to make the code look prettier.

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

Now for the code.

A person should be able to copy the shown source code, put it in a file and run it, and get the output shown for the example. There’s one clear code formatting error in the example, an unconnected trailing } in the last line, which creates a compiling syntax error.

A structural code deficiency is it doesn’t provide the required test input strings. A means to provide the strings needs to be added in order to create the required output. You can view the Ruby example to see one way to do this.

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

Finally, to make the code look more idiomatic Crystal, make short if conditions one-liners (not 3), which I mentioned in a few post before this one. Also, it’s more idiomatic to use 2 spaces for indentations, and not tabs, to show code hierarchy.

Always run your code to test it, before submitting it.

So if you put the code in a file, for example called fasta.cr, you should be able to run it and get the required output, as: $ crystal fasta.cr

Once the code is working correctly we can put in on the list as another task done. :grinning:

1 Like

M 2020/4/13

Factors of a Mersenne number
https://rosettacode.org/wiki/Factors_of_a_Mersenne_number#Crystal

Thanks~ I changed it to ruby so that crystal code have the highlight now ~

1 Like

Wed 2020/4/15

Primality by trial division
https://rosettacode.org/wiki/Primality_by_trial_division#Crystal

Sequence of primes by trial division
https://rosettacode.org/wiki/Sequence_of_primes_by_trial_division#Crystal

Thanks~ I changed to one-line and use 2 spaces instead of tab, and add crystal playground url to header for test the code.

Able to run it in on playground, copied to a file and ran as $ crystal fasta.cr too.

Great job! :grinning:

I will add it to the list. Keepm cumin!

3 Likes

Th 2020/4/16

Fermat numbers
https://rosettacode.org/wiki/Fermat_numbers#Crystal

This is a good example of leveraging the GNU/Linux coreutils library in Crystal efficiently.

2 Likes

Sat 2020/4/18

Happy numbers
https://rosettacode.org/wiki/Happy_numbers#Crystal

This is the 50th task posted in this thread so far!! :tada: :sparkler: :fireworks:

In addition: Number 51

Random numbers
https://rosettacode.org/wiki/Random_numbers#Crystal

5 Likes

Sun 2020/4/19

Cumulative standard deviation
https://rosettacode.org/wiki/Cumulative_standard_deviation#Crystal

I can do this using a class|object but not the Ruby (et al) version using a lambda. Does Crystal have the equivalent? Is there a (short) direct translation of code below?

def sdaccum
  n, sum, sum2 = 0, 0.0, 0.0
  lambda do |num|
    n += 1
    sum += num
    sum2 += num**2
    #Math.sqrt( (sum2 / n) - (sum / n)**2 )
    Math.sqrt( (sum2 * n - sum**2) / n**2 )
  end
end
   
sd = sdaccum
[2,4,4,4,5,5,7,9].each {|n| print sd.call(n), ", "}
1 Like

I think you could use Proc

https://crystal-lang.org/reference/syntax_and_semantics/literals/proc.html

1 Like

I’ve tried every technique from the docs using procs before giving up and asking for help. I’ve exhausted my limit of ideas, but I’m still curious if Crystal can do it this way too.

https://carc.in/#/r/8x1y

That’s literally just replacing lambda do |num| with ->(num : Int32) do. Makes me wonder what you tried :)

Not that I think this is a pretty solution btw…

1 Like

Hey thanks @jhass, I knew it had to be something simple.

All I had to use was the Crystal Programming Language:
https://crystal-lang.org/reference/
https://crystal-lang.org/reference/syntax_and_semantics/blocks_and_procs.html

There’s nothing there that even mentions/references lambdas or compatibility w/Ruby. In fact, putting lambda in the page search bar returns: No results matching "lambda"

Now that I know the syntax, I can translate more of the Ruby examples that use explicit lambdas, so this was a successful learning exercise for me.

Are there other online references that do a better|more thorough job of explaining (w/examples) how to use Crystal, especially for people coming from primarily Ruby?