Skip to content

Map range

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Ghul.Pipes
map_range(
(a1, a2): (double, double),
(b1, b2): (double, double),
s: double
) -> double =>
b1 + (s - a1) * (b2 - b1) / (a2 - a1)
(0::10)
|> map(s =>
(s, mapped = map_range((0.0D, 10.0D), (-1.0D, 0.0D), cast(s))))
|> each(((s, mapped)) => write_line("{s} maps to {mapped:F1}"))