Map range: Difference between revisions

615 bytes added ,  4 years ago
Added Wren
No edit summary
(Added Wren)
Line 2,847:
8 -> -0.2
9 -> -0.1</pre>
 
=={{header|Wren}}==
<lang ecmascript>var mapRange = Fn.new { |a, b, s| b.from + (s - a.from) * (b.to - b.from) / (a.to - a.from) }
 
var rset = Fn.new { |m, n|
var s = "%(n)"
var c = s.count
return (m > c) ? " " * (m - c) + s : s
}
 
var a = 0..10
var b = -1..0
for (s in a) {
var t = mapRange.call(a, b, s)
var f = (t >= 0) ? " " : ""
System.print("%(rset.call(2, s)) maps to %(f)%(t)")
}</lang>
 
{{out}}
<pre>
0 maps to -1
1 maps to -0.9
2 maps to -0.8
3 maps to -0.7
4 maps to -0.6
5 maps to -0.5
6 maps to -0.4
7 maps to -0.3
8 maps to -0.2
9 maps to -0.1
10 maps to 0
</pre>
 
=={{header|XPL0}}==
9,488

edits