Perl 6 Small Stuff #20: From anonymous one-line functions to full length MAINs with type and error checking

(CC-SA)
my &idm = -> $s { gather for ^$s -> $y { take map { Int($_ == $y) }, ^$s } };
.join(' ').say for $idm(4);
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
-> $a, $b { say "Equal point: " ~ ( $b - $a == 100 ?? "None" !! $a / (1 - (($b - $a) / 100))) }(32,212);
File: eqpoint.p6
Run: ./eqpoint.p6 32 212
./eqpoint.p6 fahrenheit
....................#!/usr/bin/env perl6multi MAIN( #= Compares the C. scale to any scale you dream up
Real $f, #= freezing point in custom scale
Real $b #= boiling point in custom scale
) {
say "There is no equal point for this scale." and exit if $b - $f == 100;
my $equal-point = $f / (1 - (($b - $f) / 100));
say "The calculated equal point is only theoretical as it is below absolute zero." if $equal-point < -273.15;
say "Equal point: $equal-point";
}
multi MAIN( #= Compares the C. scale to a named temperature scale
Str $scale where { $_ ~~ m:i/^(fahrenheit|kelvin|rankin)$/ } #= Name of scale (Fahrenheit, Kelvin or Rankin)
) {
given $scale.fc {
when "fahrenheit" { MAIN( 32 , 212 ); }
when "kelvin" { MAIN(273.15, 373.15); }
when "rankin" { MAIN(491.67, 671.67); }
}
}
Usage:
eqpoint.p6 <f> <b> -- Compares the Celcius scale to whatever scale you dream up
eqpoint.p6 <scale> -- Compares the Celcius scale to a given temperature scale

<f> freezing point in this scale
<b> boiling point in this scale
<scale> Name of scale (Fahrenheit, Kelvin or Rankin)

Norwegian with many interests. Programming being one of them.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store