How to resolve the algorithm Map range step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Map range step by step in the Raku programming language

Table of Contents

Problem Statement

Given two ranges:   where:

Write a function/subroutine/... that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use this function to map values from the range   [0, 10]   to the range   [-1, 0].

Show additional idiomatic ways of performing the mapping, using tools available to the language.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Map range step by step in the Raku programming language

Source code in the raku programming language

sub getmapper(Range $a, Range  $b) {
  my ($a1, $a2) = $a.bounds;
  my ($b1, $b2) = $b.bounds;
  return -> $s { $b1 + (($s-$a1) * ($b2-$b1) / ($a2-$a1)) }
}

my &mapper = getmapper(0 .. 10, -1 .. 0);
for ^11 -> $x {say "$x maps to &mapper($x)"}


  

You may also check:How to resolve the algorithm URL parser step by step in the Raku programming language
You may also check:How to resolve the algorithm Chinese remainder theorem step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the ERRE programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the DCL programming language
You may also check:How to resolve the algorithm String length step by step in the Yorick programming language