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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Map range step by step in the ReScript 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 ReScript programming language

Source code in the rescript programming language

let map_range = ((a1, a2), (b1, b2), s) => {
  b1 +. ((s -. a1) *. (b2 -. b1) /. (a2 -. a1))
}

Js.log("Mapping [0,10] to [-1,0] at intervals of 1:")

for i in 0 to 10 {
  Js.log("f(" ++ Js.String.make(i) ++ ") = " ++
    Js.String.make(map_range((0.0, 10.0), (-1.0, 0.0), float(i))))
}

<!DOCTYPE html>
<html>
<head>
<title>ReScript: Map_range</title>
<meta charset="UTF-8"/>
<style rel="stylesheet" type="text/css">
body { color:#EEE; background-color:#888; }
</style>
<script>var exports = {};</script>
<script src="./maprange.js"></script>
</head>
<body>

</body>
</html>


  

You may also check:How to resolve the algorithm String case step by step in the Vala programming language
You may also check:How to resolve the algorithm Tau number step by step in the Forth programming language
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the Perl programming language
You may also check:How to resolve the algorithm Terminal control/Coloured text step by step in the Scala programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the Go programming language