Map range: Difference between revisions

Content deleted Content added
→‎{{header|PicoLisp}}: Marked incorrect as task asks for a generic function of two input ranges and a value.
Rdm (talk | contribs)
m J: add comments
Line 77: Line 77:
'b1 b2'=.n
'b1 b2'=.n
b1+((y-a1)*b2-b1)%a2-a1
b1+((y-a1)*b2-b1)%a2-a1
)
)</lang>
NB. this version defers all calculations to runtime, but mirrors exactly the task formulation</lang>


Or
Or
Line 85: Line 86:
'b1 b2'=.n
'b1 b2'=.n
b1 + ((b2-b1)%a2-a1) * -&a1
b1 + ((b2-b1)%a2-a1) * -&a1
)
)</lang>
NB. this version precomputes the scaling ratio</lang>


Example use:
Example use:
Line 94: Line 96:
or
or


<lang j> adjust=:2 4 maprange 5 11
<lang j> adjust=:2 4 maprange 5 11 NB. save the derived function as a named entity
adjust 2.718282 3 3.141592
adjust 2.718282 3 3.141592
7.15485 8 8.42478</lang>
7.15485 8 8.42478</lang>