Improve Crystal's profile on Rosetta Code

Hey @wrq, welcome to the club.
Sorry for not acknowledging earlier. Was busy.

A few things:

  • Can you add day and date to your post, so I can log chronologically
  • Change <lang crystal> to <lang ruby> to get pretty code highlighting
  • Run code against the task dataset provided at top of page and post output

Otherwise, keepm cumn. :smiley:

Tu 2020/10/6

I did the Ruby version first to debug the algorithm I used, then translated to Crystal.

N-smooth numbers

2 Likes

Just found this. I’ll work on one.

1 Like

The first two for 2021!

Sun 2021/1/17

Real constants and functions
https://rosettacode.org/wiki/Real_constants_and_functions#Crystal

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

1 Like

Mon 2021/3/8

Population count
https://rosettacode.org/wiki/Population_count#Crystal

Tu 2021/3/9

Zeckendorf number representation
https://rosettacode.org/wiki/Zeckendorf_number_representation#Crystal

Here are the current lists of tasks with|out Crystal examples.

Tasks with Crystal examples.
https://rosettacode.org/wiki/Category:Crystal

Tasks without Crystal examples.
https://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_Crystal

Sat 2021/7/24

Median
https://rosettacode.org/wiki/Averages/Median#Crystal

1 Like

Mon 2021/7/26

Digital Root
https://rosettacode.org/wiki/Digital_root#Crystal

I’d actually already implemented this a while back, but it hadn’t occurred to me to put it on Rosetta Code.

3 Likes

Wed 2021/11/17

With thanks to @HertzDevil and @e4pjacob. :slightly_smiling_face:

Pathological floating point problems
https://rosettacode.org/wiki/Pathological_floating_point_problems#Crystal

1 Like

Th 2021/11/18

Additive primes
https://rosettacode.org/wiki/Additive_primes#Crystal

Two identical strings
https://rosettacode.org/wiki/Two_identical_strings#Crystal

1 Like

Th 2022/2/3

First one of 2022! :sparkler:

Summation of primes
https://rosettacode.org/wiki/Summation_of_primes#Crystal

4 Likes

The last line could also be

puts "The sum of all primes below 2 million is #{(0i64..2000000i64).count { |n| prime? n }}.

I think that shows off Crystal’s readability slightly better and also saves an intermediate array (from Range#select).

I’m happy to make that edit, but I didn’t want to undercut your much larger contribution.

1 Like

This gives 148934, which is the number of primes, not all their sums.

2 Likes

:man_facepalming: So it does. In which case I think your example is better (due to readability) than a solution which removes the intermediate array.

Yes, I was going for coding efficiency not performance, because my priority was to show ease of programming versus a pedantic (more cryptic) faster version.

I have a completely working Crystal version of my primes-utils rubygem
(primes-utils | RubyGems.org | your community gem host),
but I haven’t made a shard of it yet. Then in Crystal (like Ruby) you can just do:

2000000.primes.sum
To get the primes count up to 2000000 would just be: 2000000.primescnt

2 Likes

For what it’s worth, I’ve added a Hello world/Graphical entry: Hello world/Graphical - Rosetta Code

I copied the code.

# HelloWorldGraphical.cr
require "crsfml"

window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "Hello world/Graphical")
font = SF::Font.from_file("DejaVuSerif-Bold.ttf")

text = SF::Text.new
text.font = font

text.string = "Goodbye, world!"
text.character_size = 24

text.color = SF::Color::Black

while window.open?
  while event = window.poll_event
    if event.is_a? SF::Event::Closed
      window.close
    end
  end

  window.clear(SF::Color::White)

  window.draw(text)

  window.display
end

and installed the libraries on my KDE Linux laptop and ran it, and get this error when running it.

➜  Hello World Graphical crystal run HelloWorldGraphical.cr
Failed to load font "DejaVuSerif-Bold.ttf" (failed to create the font face)
Unhandled exception: Font.load_from_file failed (SF::InitError)
  from lib/crsfml/src/graphics/obj.cr:3737:9 in 'from_file'
  from HelloWorldGraphical.cr:4:1 in '__crystal_main'
  from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:129:5 in 'main_user_code'
  from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:115:7 in 'main'
  from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:141:3 in 'main'
  from /lib64/libc.so.6 in '??'
  from /lib64/libc.so.6 in '__libc_start_main'
  from ../sysdeps/x86_64/start.S:117 in '_start'
  from ???

A small blank window quickly forms, then disappears, with the error message following.

It looks like you didn’t have a font file in the code directory itself. CrSFML doesn’t load fonts from root, it only loads font files that are within the project folder.

This is probably not a good Crystal example for Rosetta Code, because people should be able to run each example using the native implementation|tools of the given language.

Here, I had to install the libraries for crsfml from the repos of my Linux system (which luckily it had), but it’s still missing font libraries for it to run. None of these are native to Crystal, and people unfamiliar with Crystal may think something was wrong|bad with it when they can’t get this to run.

A better alternative is to implement it with standard shards that are (hopefully mostly) OS independent, so the procedure to install|run the code is the same|similar across platforms.

As an example, take a look at the Ruby example for this.
It gives multiple examples using different libraries to choose from, depending on your platform.

But the most important thing is to provide code that works, to show Crystal’s capabilities to others.

1 Like

The only other possibility I can think of that’s actively developed is the GTK4 binding. If there are graphic output shards that are standard then I don’t know what they are.

If you’re missing the font itself, the DejaVu Serif fonts can be downloaded here: DejaVu Serif Font Free by DejaVu Fonts » Font Squirrel

Place DejaVuSerif-Bold.ttf in the same location as the main.cr file