Map range: Difference between revisions

added Ursala
m (→‎{{header|C}}: move incorrect-template above <lang>)
(added Ursala)
Line 505:
10 -> 0.00
</pre>
 
=={{header|Ursala}}==
The function <code>f</code> is defined using pattern matching and substitution, taking a pair of pairs of interval endpoints and a number as parameters, and returning a number.
<lang Ursala>#import flo
 
f((("a1","a2"),("b1","b2")),"s") = plus("b1",div(minus("s","a1"),minus("a2","a1")))
 
#cast %eL
 
test = f* ((0.,10.),(-1.,0.))-* ari11/0. 10.</lang>
output:
<pre><
-1.000000e+00,
-9.000000e-01,
-8.000000e-01,
-7.000000e-01,
-6.000000e-01,
-5.000000e-01,
-4.000000e-01,
-3.000000e-01,
-2.000000e-01,
-1.000000e-01,
0.000000e+00></pre>
A more idiomatic way is to define f as a second order function
<lang Ursala>f(("a1","a2"),("b1","b2")) "s" = ...</lang>
with the same right hand side as above, so that it takes a pair of intervals and returns a function mapping numbers in one interval to numbers in the other.
 
An even more idiomatic way is to use the standard library function <code>plin</code>, which takes an arbitrarily long list of interval endpoints and returns a piecewise linear interpolation function.
Anonymous user