Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
2
Task/Map-range/00-META.yaml
Normal file
2
Task/Map-range/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Map_range
|
||||
21
Task/Map-range/00-TASK.txt
Normal file
21
Task/Map-range/00-TASK.txt
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
Given two [[wp:Interval (mathematics)|ranges]]:
|
||||
:::* <big><math>[a_1,a_2]</math></big> and
|
||||
:::* <big><math>[b_1,b_2]</math></big>;
|
||||
:::* then a value <big><math>s</math></big> in range <big><math>[a_1,a_2]</math></big>
|
||||
:::* is linearly mapped to a value <big><math>t</math></big> in range <big><math>[b_1,b_2]</math>
|
||||
</big> where:
|
||||
<br>
|
||||
|
||||
:::* <big><big><math>t = b_1 + {(s - a_1)(b_2 - b_1) \over (a_2 - a_1)}</math></big></big>
|
||||
|
||||
|
||||
;Task:
|
||||
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 <big><code> [0, 10] </code></big> to the range <big><code> [-1, 0]. </code></big>
|
||||
|
||||
|
||||
;Extra credit:
|
||||
Show additional idiomatic ways of performing the mapping, using tools available to the language.
|
||||
<br><br>
|
||||
|
||||
5
Task/Map-range/11l/map-range.11l
Normal file
5
Task/Map-range/11l/map-range.11l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
F maprange(a, b, s)
|
||||
R b[0] + (Float(s - a[0]) * (b[1] - b[0]) / (a[1] - a[0]))
|
||||
|
||||
L(s) 0..10
|
||||
print(‘#2 maps to #.’.format(s, maprange((0, 10), (-1, 0), s)))
|
||||
6
Task/Map-range/6502-Assembly/map-range-1.6502
Normal file
6
Task/Map-range/6502-Assembly/map-range-1.6502
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
mapping:
|
||||
byte $00,$00,$80,$BF ;-1.0f (stored little-endian so the bytes are backwards)
|
||||
byte $66,$66,$66,$BF ;-0.9f
|
||||
byte $CD,$CC,$4C,$BF ;-0.8f
|
||||
byte $33,$33,$33,$BF ;-0.7f
|
||||
etc.
|
||||
26
Task/Map-range/6502-Assembly/map-range-2.6502
Normal file
26
Task/Map-range/6502-Assembly/map-range-2.6502
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
;runs during non-maskable interrupt.
|
||||
PrintChar:
|
||||
;a = char to print
|
||||
SEC
|
||||
SBC #$32 ;subtract ascii offset to map the index to the correct tile graphics data.
|
||||
|
||||
;everything below this comment is hardware-specific mumbo-jumbo, feel free to ignore it if you don't care.
|
||||
;ideally you'd want to do this before getting here so that the only thing that happens during NMI is the write to vram.
|
||||
pha
|
||||
LDA Cursor_Y
|
||||
ASL
|
||||
ASL
|
||||
ASL
|
||||
ASL
|
||||
ASL
|
||||
STA tempY ;row * 32
|
||||
LDA #$20
|
||||
ADC Cursor_X
|
||||
STA tempX
|
||||
LDA $2002 ;reset picture processor high-low latch
|
||||
LDA tempX
|
||||
STA $2006 ;this register is big-endian for some reason. Which is why I had to store Cursor_Y << 5 into tempY rather than here directly.
|
||||
LDA tempY
|
||||
STA $2006
|
||||
PLA
|
||||
STA $2007
|
||||
14
Task/Map-range/ACL2/map-range.acl2
Normal file
14
Task/Map-range/ACL2/map-range.acl2
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(defun mapping (a1 a2 b1 b2 s)
|
||||
(+ b1 (/ (* (- s a1)
|
||||
(- b2 b1))
|
||||
(- a2 a1))))
|
||||
|
||||
(defun map-each (a1 a2 b1 b2 ss)
|
||||
(if (endp ss)
|
||||
nil
|
||||
(cons (mapping a1 a2 b1 b2 (first ss))
|
||||
(map-each a1 a2 b1 b2 (rest ss)))))
|
||||
|
||||
(map-each 0 10 -1 0 '(0 1 2 3 4 5 6 7 8 9 10))
|
||||
|
||||
;; (-1 -9/10 -4/5 -7/10 -3/5 -1/2 -2/5 -3/10 -1/5 -1/10 0)
|
||||
9
Task/Map-range/ALGOL-68/map-range.alg
Normal file
9
Task/Map-range/ALGOL-68/map-range.alg
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# maps a real s in the range [ a1, a2 ] to the range [ b1, b2 ] #
|
||||
# there are no checks that s is in the range or that the ranges are valid #
|
||||
PROC map range = ( REAL s, a1, a2, b1, b2 )REAL:
|
||||
b1 + ( ( s - a1 ) * ( b2 - b1 ) ) / ( a2 - a1 );
|
||||
|
||||
# test the mapping #
|
||||
FOR i FROM 0 TO 10 DO
|
||||
print( ( whole( i, -2 ), " maps to ", fixed( map range( i, 0, 10, -1, 0 ), -8, 2 ), newline ) )
|
||||
OD
|
||||
14
Task/Map-range/AWK/map-range.awk
Normal file
14
Task/Map-range/AWK/map-range.awk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# syntax: GAWK -f MAP_RANGE.AWK
|
||||
BEGIN {
|
||||
a1 = 0
|
||||
a2 = 10
|
||||
b1 = -1
|
||||
b2 = 0
|
||||
for (i=a1; i<=a2; i++) {
|
||||
printf("%g maps to %g\n",i,map_range(a1,a2,b1,b2,i))
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
function map_range(a1,a2,b1,b2,num) {
|
||||
return b1 + ((num-a1) * (b2-b1) / (a2-a1))
|
||||
}
|
||||
30
Task/Map-range/Action-/map-range.action
Normal file
30
Task/Map-range/Action-/map-range.action
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
|
||||
|
||||
PROC Map(REAL POINTER a1,a2,b1,b2,s,res)
|
||||
REAL tmp1,tmp2,tmp3
|
||||
|
||||
RealSub(s,a1,tmp1) ;tmp1=s-a1
|
||||
RealSub(b2,b1,tmp2) ;tmp2=b2-b1
|
||||
RealMult(tmp1,tmp2,tmp3) ;tmp3=(s-a1)*(b2-b1)
|
||||
RealSub(a2,a1,tmp1) ;tmp1=a2-a1
|
||||
RealDiv(tmp3,tmp1,tmp2) ;tmp2=(s-a1)*(b2-b1)/(a2-a1)
|
||||
RealAdd(b1,tmp2,res) ;res=b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
BYTE i
|
||||
REAL a1,a2,b1,b2,s,res
|
||||
|
||||
Put(125) PutE() ;clear screen
|
||||
|
||||
ValR("0",a1) ValR("10",a2)
|
||||
ValR("-1",b1) ValR("0",b2)
|
||||
|
||||
FOR i=0 TO 10
|
||||
DO
|
||||
IntToReal(i,s)
|
||||
Map(a1,a2,b1,b2,s,res)
|
||||
PrintR(s) Print(" maps to ")
|
||||
PrintRE(res)
|
||||
OD
|
||||
RETURN
|
||||
33
Task/Map-range/Ada/map-range.ada
Normal file
33
Task/Map-range/Ada/map-range.ada
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
with Ada.Text_IO;
|
||||
procedure Map is
|
||||
type First_Range is new Float range 0.0 .. 10.0;
|
||||
type Second_Range is new Float range -1.0 .. 0.0;
|
||||
function Translate (Value : First_Range) return Second_Range is
|
||||
B1 : Float := Float (Second_Range'First);
|
||||
B2 : Float := Float (Second_Range'Last);
|
||||
A1 : Float := Float (First_Range'First);
|
||||
A2 : Float := Float (First_Range'Last);
|
||||
Result : Float;
|
||||
begin
|
||||
Result := B1 + (Float (Value) - A1) * (B2 - B1) / (A2 - A1);
|
||||
return Second_Range (Result);
|
||||
end;
|
||||
function Translate (Value : Second_Range) return First_Range is
|
||||
B1 : Float := Float (First_Range'First);
|
||||
B2 : Float := Float (First_Range'Last);
|
||||
A1 : Float := Float (Second_Range'First);
|
||||
A2 : Float := Float (Second_Range'Last);
|
||||
Result : Float;
|
||||
begin
|
||||
Result := B1 + (Float (Value) - A1) * (B2 - B1) / (A2 - A1);
|
||||
return First_Range (Result);
|
||||
end;
|
||||
Test_Value : First_Range := First_Range'First;
|
||||
begin
|
||||
loop
|
||||
Ada.Text_IO.Put_Line (First_Range'Image (Test_Value) & " maps to: "
|
||||
& Second_Range'Image (Translate (Test_Value)));
|
||||
exit when Test_Value = First_Range'Last;
|
||||
Test_Value := Test_Value + 1.0;
|
||||
end loop;
|
||||
end Map;
|
||||
6
Task/Map-range/Amazing-Hopper/map-range-1.hopper
Normal file
6
Task/Map-range/Amazing-Hopper/map-range-1.hopper
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
double inc = (nHasta - nDesde) / ( nTotal - 1);
|
||||
lista[0] = nDesde;
|
||||
lista[nTotal] = nHasta;
|
||||
for( n=1; n<nTotal; n++){
|
||||
lista[n] = lista[n-1] + inc;
|
||||
}
|
||||
2
Task/Map-range/Amazing-Hopper/map-range-2.hopper
Normal file
2
Task/Map-range/Amazing-Hopper/map-range-2.hopper
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
#defn Seqspaced(__X__,__Y__,__Z__,_V_) #ATOM#CMPLX;#ATOM#CMPLX;#ATOM#CMPLX;keep;lthan(1);\
|
||||
do{{"Seqspaced: num elements < 1"}throw(2301)},seqsp(_V_)
|
||||
3
Task/Map-range/Amazing-Hopper/map-range-3.hopper
Normal file
3
Task/Map-range/Amazing-Hopper/map-range-3.hopper
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#defn Toksep(__X__) #ATOM#CMPLX;toksep;
|
||||
#defn Cat(_X_,*) #ATOM#CMPLX;#GENCODE $$$*$$$ #ATCMLIST;cat; #ENDGEN
|
||||
#defn Justleft(_X_,_V_) {" "};#ATOM#CMPLX;#ATOM#CMPLX;padright;
|
||||
8
Task/Map-range/Amazing-Hopper/map-range-4.hopper
Normal file
8
Task/Map-range/Amazing-Hopper/map-range-4.hopper
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <jambo.h>
|
||||
Main
|
||||
v=0,w=0
|
||||
Seqspaced(-1,0,11,w) // [-1,0}->[0-10]=11 números
|
||||
Seqspaced(0,10,11,v)
|
||||
Toksep( "\n" )
|
||||
Cat( Justright(5,Str(v))," => ",Justright(5,Str(w))), Prnl
|
||||
End
|
||||
304
Task/Map-range/AppleScript/map-range.applescript
Normal file
304
Task/Map-range/AppleScript/map-range.applescript
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
------------------------ MAP RANGE -----------------------
|
||||
|
||||
-- rangeMap :: (Num, Num) -> (Num, Num) -> Num -> Num
|
||||
on rangeMap(a, b)
|
||||
script
|
||||
on |λ|(s)
|
||||
set {a1, a2} to a
|
||||
set {b1, b2} to b
|
||||
b1 + ((s - a1) * (b2 - b1)) / (a2 - a1)
|
||||
end |λ|
|
||||
end script
|
||||
end rangeMap
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
on run
|
||||
set mapping to rangeMap({0, 10}, {-1, 0})
|
||||
|
||||
set xs to enumFromTo(0, 10)
|
||||
set ys to map(mapping, xs)
|
||||
set zs to map(approxRatio(0), ys)
|
||||
|
||||
unlines(zipWith3(formatted, xs, ys, zs))
|
||||
end run
|
||||
|
||||
|
||||
------------------------- DISPLAY ------------------------
|
||||
|
||||
-- formatted :: Int -> Float -> Ratio -> String
|
||||
on formatted(x, m, r)
|
||||
set fract to showRatio(r)
|
||||
set {n, d} to splitOn("/", fract)
|
||||
|
||||
(justifyRight(2, space, x as string) & " -> " & ¬
|
||||
justifyRight(4, space, m as string)) & " = " & ¬
|
||||
justifyRight(2, space, n) & "/" & d
|
||||
end formatted
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS -------------------
|
||||
|
||||
-- Absolute value.
|
||||
-- abs :: Num -> Num
|
||||
on abs(x)
|
||||
if 0 > x then
|
||||
-x
|
||||
else
|
||||
x
|
||||
end if
|
||||
end abs
|
||||
|
||||
|
||||
-- approxRatio :: Real -> Real -> Ratio
|
||||
on approxRatio(epsilon)
|
||||
script
|
||||
on |λ|(n)
|
||||
if {real, integer} contains (class of epsilon) and 0 < epsilon then
|
||||
set e to epsilon
|
||||
else
|
||||
set e to 1 / 10000
|
||||
end if
|
||||
|
||||
script gcde
|
||||
on |λ|(e, x, y)
|
||||
script _gcd
|
||||
on |λ|(a, b)
|
||||
if b < e then
|
||||
a
|
||||
else
|
||||
|λ|(b, a mod b)
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|λ|(abs(x), abs(y)) of _gcd
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
set c to |λ|(e, 1, n) of gcde
|
||||
ratio((n div c), (1 div c))
|
||||
end |λ|
|
||||
end script
|
||||
end approxRatio
|
||||
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set lst to {}
|
||||
repeat with i from m to n
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
else
|
||||
return {}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
|
||||
-- gcd :: Int -> Int -> Int
|
||||
on gcd(a, b)
|
||||
set x to abs(a)
|
||||
set y to abs(b)
|
||||
repeat until y = 0
|
||||
if x > y then
|
||||
set x to x - y
|
||||
else
|
||||
set y to y - x
|
||||
end if
|
||||
end repeat
|
||||
return x
|
||||
end gcd
|
||||
|
||||
|
||||
-- justifyLeft :: Int -> Char -> String -> String
|
||||
on justifyLeft(n, cFiller, strText)
|
||||
if n > length of strText then
|
||||
text 1 thru n of (strText & replicate(n, cFiller))
|
||||
else
|
||||
strText
|
||||
end if
|
||||
end justifyLeft
|
||||
|
||||
|
||||
-- justifyRight :: Int -> Char -> String -> String
|
||||
on justifyRight(n, cFiller, strText)
|
||||
if n > length of strText then
|
||||
text -n thru -1 of ((replicate(n, cFiller) as text) & strText)
|
||||
else
|
||||
strText
|
||||
end if
|
||||
end justifyRight
|
||||
|
||||
|
||||
-- length :: [a] -> Int
|
||||
on |length|(xs)
|
||||
set c to class of xs
|
||||
if list is c or string is c then
|
||||
length of xs
|
||||
else
|
||||
(2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
|
||||
end if
|
||||
end |length|
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- minimum :: Ord a => [a] -> a
|
||||
on minimum(xs)
|
||||
set lng to length of xs
|
||||
if lng < 1 then return missing value
|
||||
set m to item 1 of xs
|
||||
repeat with x in xs
|
||||
set v to contents of x
|
||||
if v < m then set m to v
|
||||
end repeat
|
||||
return m
|
||||
end minimum
|
||||
|
||||
|
||||
-- ratio :: Int -> Int -> Ratio Int
|
||||
on ratio(x, y)
|
||||
script go
|
||||
on |λ|(x, y)
|
||||
if 0 ≠ y then
|
||||
if 0 ≠ x then
|
||||
set d to gcd(x, y)
|
||||
{type:"Ratio", n:(x div d), d:(y div d)}
|
||||
else
|
||||
{type:"Ratio", n:0, d:1}
|
||||
end if
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
go's |λ|(x * (signum(y)), abs(y))
|
||||
end ratio
|
||||
|
||||
|
||||
-- Egyptian multiplication - progressively doubling a list, appending
|
||||
-- stages of doubling to an accumulator where needed for binary
|
||||
-- assembly of a target length
|
||||
-- replicate :: Int -> a -> [a]
|
||||
on replicate(n, a)
|
||||
set out to {}
|
||||
if n < 1 then return out
|
||||
set dbl to {a}
|
||||
|
||||
repeat while (n > 1)
|
||||
if (n mod 2) > 0 then set out to out & dbl
|
||||
set n to (n div 2)
|
||||
set dbl to (dbl & dbl)
|
||||
end repeat
|
||||
return out & dbl
|
||||
end replicate
|
||||
|
||||
|
||||
-- showRatio :: Ratio -> String
|
||||
on showRatio(r)
|
||||
(n of r as string) & "/" & (d of r as string)
|
||||
end showRatio
|
||||
|
||||
|
||||
-- signum :: Num -> Num
|
||||
on signum(x)
|
||||
if x < 0 then
|
||||
-1
|
||||
else if x = 0 then
|
||||
0
|
||||
else
|
||||
1
|
||||
end if
|
||||
end signum
|
||||
|
||||
|
||||
-- splitOn :: String -> String -> [String]
|
||||
on splitOn(pat, src)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, pat}
|
||||
set xs to text items of src
|
||||
set my text item delimiters to dlm
|
||||
return xs
|
||||
end splitOn
|
||||
|
||||
|
||||
-- take :: Int -> [a] -> [a]
|
||||
-- take :: Int -> String -> String
|
||||
on take(n, xs)
|
||||
set c to class of xs
|
||||
if list is c then
|
||||
if 0 < n then
|
||||
items 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
else if string is c then
|
||||
if 0 < n then
|
||||
text 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
""
|
||||
end if
|
||||
else if script is c then
|
||||
set ys to {}
|
||||
repeat with i from 1 to n
|
||||
set v to xs's |λ|()
|
||||
if missing value is v then
|
||||
return ys
|
||||
else
|
||||
set end of ys to v
|
||||
end if
|
||||
end repeat
|
||||
return ys
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end take
|
||||
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end unlines
|
||||
|
||||
|
||||
-- zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
|
||||
on zipWith3(f, xs, ys, zs)
|
||||
set lng to minimum({length of xs, length of ys, length of zs})
|
||||
if 1 > lng then return {}
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, item i of ys, item i of zs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end zipWith3
|
||||
11
Task/Map-range/Arturo/map-range.arturo
Normal file
11
Task/Map-range/Arturo/map-range.arturo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
getMapped: function [a,b,i][
|
||||
round .to:1 b\0 + ((i - a\0) * (b\1 - b\0))/(a\1 - a\0)
|
||||
]
|
||||
|
||||
rangeA: @[0.0 10.0]
|
||||
rangeB: @[0-1.0 0.0]
|
||||
|
||||
loop 0..10 'x [
|
||||
mapped: getMapped rangeA rangeB to :floating x
|
||||
print [x "maps to" mapped]
|
||||
]
|
||||
10
Task/Map-range/AutoHotkey/map-range.ahk
Normal file
10
Task/Map-range/AutoHotkey/map-range.ahk
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
mapRange(a1, a2, b1, b2, s)
|
||||
{
|
||||
return b1 + (s-a1)*(b2-b1)/(a2-a1)
|
||||
}
|
||||
|
||||
out := "Mapping [0,10] to [-1,0] at intervals of 1:`n"
|
||||
|
||||
Loop 11
|
||||
out .= "f(" A_Index-1 ") = " mapRange(0,10,-1,0,A_Index-1) "`n"
|
||||
MsgBox % out
|
||||
7
Task/Map-range/Axiom/map-range-1.axiom
Normal file
7
Task/Map-range/Axiom/map-range-1.axiom
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
)abbrev package TESTP TestPackage
|
||||
TestPackage(R:Field) : with
|
||||
mapRange: (Segment(R), Segment(R)) -> (R->R)
|
||||
== add
|
||||
mapRange(fromRange, toRange) ==
|
||||
(a1,a2,b1,b2) := (lo fromRange,hi fromRange,lo toRange,hi toRange)
|
||||
(x:R):R +-> b1+(x-a1)*(b2-b1)/(a2-a1)
|
||||
2
Task/Map-range/Axiom/map-range-2.axiom
Normal file
2
Task/Map-range/Axiom/map-range-2.axiom
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
f := mapRange(1..10,a..b)
|
||||
[(xi,f xi) for xi in 1..10]
|
||||
8
Task/Map-range/BASIC256/map-range.basic
Normal file
8
Task/Map-range/BASIC256/map-range.basic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function MapRange(s, a1, a2, b1, b2)
|
||||
return b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
end function
|
||||
|
||||
for i = 0 to 10
|
||||
print i; " maps to "; MapRange(i,0,10,-1,0)
|
||||
next i
|
||||
end
|
||||
12
Task/Map-range/BBC-BASIC/map-range.basic
Normal file
12
Task/Map-range/BBC-BASIC/map-range.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
@% = 5 : REM Column width
|
||||
DIM range{l, h}
|
||||
DIM A{} = range{}, B{} = range{}
|
||||
A.l = 0 : A.h = 10
|
||||
B.l = -1 : B.h = 0
|
||||
FOR n = 0 TO 10
|
||||
PRINT n " maps to " FNmaprange(A{}, B{}, n)
|
||||
NEXT
|
||||
END
|
||||
|
||||
DEF FNmaprange(a{}, b{}, s)
|
||||
= b.l + (s - a.l) * (b.h - b.l) / (a.h - a.l)
|
||||
9
Task/Map-range/BQN/map-range-1.bqn
Normal file
9
Task/Map-range/BQN/map-range-1.bqn
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
_map_ ← {
|
||||
a1‿a2 _𝕣_ b1‿b2 s:
|
||||
b1 + ((s - a1) × b2 - b1) ÷ a2 - a1
|
||||
}
|
||||
|
||||
ZeroTen ← 0‿10 _map_ ¯1‿0
|
||||
|
||||
•Show ZeroTen 0.1
|
||||
•Show ZeroTen 8
|
||||
2
Task/Map-range/BQN/map-range-2.bqn
Normal file
2
Task/Map-range/BQN/map-range-2.bqn
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
¯0.99
|
||||
¯0.19999999999999996
|
||||
16
Task/Map-range/Bc/map-range.bc
Normal file
16
Task/Map-range/Bc/map-range.bc
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* map s from [a, b] to [c, d] */
|
||||
define m(a, b, c, d, s) {
|
||||
return (c + (s - a) * (d - c) / (b - a))
|
||||
}
|
||||
|
||||
scale = 6 /* division to 6 decimal places */
|
||||
"[0, 10] => [-1, 0]
|
||||
"
|
||||
for (i = 0; i <= 10; i += 2) {
|
||||
/*
|
||||
* If your bc(1) has a print statement, you can try
|
||||
* print i, " => ", m(0, 10, -1, 0, i), "\n"
|
||||
*/
|
||||
i; " => "; m(0, 10, -1, 0, i)
|
||||
}
|
||||
quit
|
||||
13
Task/Map-range/Bracmat/map-range.bracmat
Normal file
13
Task/Map-range/Bracmat/map-range.bracmat
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
( ( mapRange
|
||||
= a1,a2,b1,b2,s
|
||||
. !arg:(?a1,?a2.?b1,?b2.?s)
|
||||
& !b1+(!s+-1*!a1)*(!b2+-1*!b1)*(!a2+-1*!a1)^-1
|
||||
)
|
||||
& out$"Mapping [0,10] to [-1,0] at intervals of 1:"
|
||||
& 0:?n
|
||||
& whl
|
||||
' ( !n:~>10
|
||||
& out$("f(" !n ") = " flt$(mapRange$(0,10.-1,0.!n),2))
|
||||
& 1+!n:?n
|
||||
)
|
||||
);
|
||||
26
Task/Map-range/C++/map-range.cpp
Normal file
26
Task/Map-range/C++/map-range.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
template<typename tVal>
|
||||
tVal map_value(std::pair<tVal,tVal> a, std::pair<tVal, tVal> b, tVal inVal)
|
||||
{
|
||||
tVal inValNorm = inVal - a.first;
|
||||
tVal aUpperNorm = a.second - a.first;
|
||||
tVal normPosition = inValNorm / aUpperNorm;
|
||||
|
||||
tVal bUpperNorm = b.second - b.first;
|
||||
tVal bValNorm = normPosition * bUpperNorm;
|
||||
tVal outVal = b.first + bValNorm;
|
||||
|
||||
return outVal;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::pair<float,float> a(0,10), b(-1,0);
|
||||
|
||||
for(float value = 0.0; 10.0 >= value; ++value)
|
||||
std::cout << "map_value(" << value << ") = " << map_value(a, b, value) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
12
Task/Map-range/C-sharp/map-range.cs
Normal file
12
Task/Map-range/C-sharp/map-range.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public class MapRange
|
||||
{
|
||||
public static void Main() {
|
||||
foreach (int i in Enumerable.Range(0, 11))
|
||||
Console.WriteLine($"{i} maps to {Map(0, 10, -1, 0, i)}");
|
||||
}
|
||||
|
||||
static double Map(double a1, double a2, double b1, double b2, double s) => b1 + (s - a1) * (b2 - b1) / (a2 - a1);
|
||||
}
|
||||
16
Task/Map-range/C/map-range.c
Normal file
16
Task/Map-range/C/map-range.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define mapRange(a1,a2,b1,b2,s) (b1 + (s-a1)*(b2-b1)/(a2-a1))
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
puts("Mapping [0,10] to [-1,0] at intervals of 1:");
|
||||
|
||||
for(i=0;i<=10;i++)
|
||||
{
|
||||
printf("f(%d) = %g\n",i,mapRange(0,10,-1,0,i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
53
Task/Map-range/COBOL/map-range.cobol
Normal file
53
Task/Map-range/COBOL/map-range.cobol
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. demo-map-range.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 i USAGE FLOAT-LONG.
|
||||
|
||||
01 mapped-num USAGE FLOAT-LONG.
|
||||
|
||||
01 a-begin USAGE FLOAT-LONG VALUE 0.
|
||||
01 a-end USAGE FLOAT-LONG VALUE 10.
|
||||
|
||||
01 b-begin USAGE FLOAT-LONG VALUE -1.
|
||||
01 b-end USAGE FLOAT-LONG VALUE 0.
|
||||
|
||||
01 i-display PIC --9.9.
|
||||
01 mapped-display PIC --9.9.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
PERFORM VARYING i FROM 0 BY 1 UNTIL i > 10
|
||||
CALL "map-range" USING CONTENT a-begin, a-end, b-begin,
|
||||
b-end, i, REFERENCE mapped-num
|
||||
COMPUTE i-display ROUNDED = i
|
||||
COMPUTE mapped-display ROUNDED = mapped-num
|
||||
DISPLAY FUNCTION TRIM(i-display) " maps to "
|
||||
FUNCTION TRIM(mapped-display)
|
||||
END-PERFORM
|
||||
.
|
||||
END PROGRAM demo-map-range.
|
||||
|
||||
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. map-range.
|
||||
|
||||
DATA DIVISION.
|
||||
LINKAGE SECTION.
|
||||
01 a-begin USAGE FLOAT-LONG.
|
||||
01 a-end USAGE FLOAT-LONG.
|
||||
|
||||
01 b-begin USAGE FLOAT-LONG.
|
||||
01 b-end USAGE FLOAT-LONG.
|
||||
|
||||
01 val-to-map USAGE FLOAT-LONG.
|
||||
|
||||
01 ret USAGE FLOAT-LONG.
|
||||
|
||||
PROCEDURE DIVISION USING a-begin, a-end, b-begin, b-end,
|
||||
val-to-map, ret.
|
||||
COMPUTE ret =
|
||||
b-begin + ((val-to-map - a-begin) * (b-end - b-begin)
|
||||
/ (a-end - a-begin))
|
||||
.
|
||||
END PROGRAM map-range.
|
||||
17
Task/Map-range/Clojure/map-range.clj
Normal file
17
Task/Map-range/Clojure/map-range.clj
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defn maprange [[a1 a2] [b1 b2] s]
|
||||
(+ b1 (/ (* (- s a1) (- b2 b1)) (- a2 a1))))
|
||||
|
||||
> (doseq [s (range 11)]
|
||||
(printf "%2s maps to %s\n" s (maprange [0 10] [-1 0] s)))
|
||||
|
||||
0 maps to -1
|
||||
1 maps to -9/10
|
||||
2 maps to -4/5
|
||||
3 maps to -7/10
|
||||
4 maps to -3/5
|
||||
5 maps to -1/2
|
||||
6 maps to -2/5
|
||||
7 maps to -3/10
|
||||
8 maps to -1/5
|
||||
9 maps to -1/10
|
||||
10 maps to 0
|
||||
5
Task/Map-range/CoffeeScript/map-range.coffee
Normal file
5
Task/Map-range/CoffeeScript/map-range.coffee
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
mapRange = (a1,a2,b1,b2,s) ->
|
||||
t = b1 + ((s-a1)*(b2 - b1)/(a2-a1))
|
||||
|
||||
for s in [0..10]
|
||||
console.log("#{s} maps to #{mapRange(0,10,-1,0,s)}")
|
||||
9
Task/Map-range/Commodore-BASIC/map-range.basic
Normal file
9
Task/Map-range/Commodore-BASIC/map-range.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
10 REM MAP RANGE
|
||||
20 REM COMMODORE BASIC 2.0
|
||||
30 REM ================================
|
||||
40 A1 = 0 : A2 = 10
|
||||
50 B1 = -1 : B2 = 0
|
||||
60 DEF FN MR(S)=B1+(S-A1)*(B2-B1)/(A2-A1)
|
||||
70 FOR S=0 TO 10
|
||||
80 PRINT S;"MAPS TO ";FN MR(S)
|
||||
90 NEXT
|
||||
11
Task/Map-range/Common-Lisp/map-range.lisp
Normal file
11
Task/Map-range/Common-Lisp/map-range.lisp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(defun map-range (a1 a2 b1 b2 s)
|
||||
(+ b1
|
||||
(/ (* (- s a1)
|
||||
(- b2 b1))
|
||||
(- a2 a1))))
|
||||
|
||||
(loop
|
||||
for i from 0 to 10
|
||||
do (format t "~F maps to ~F~C" i
|
||||
(map-range 0 10 -1 0 i)
|
||||
#\Newline))
|
||||
13
Task/Map-range/Craft-Basic/map-range.basic
Normal file
13
Task/Map-range/Craft-Basic/map-range.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
define a1 = 0, b1 = 0, a2 = 0, b2 = 0
|
||||
|
||||
for i = 0 to 10
|
||||
|
||||
let s = i
|
||||
let a1 = 0
|
||||
let a2 = 10
|
||||
let b1 = -1
|
||||
let b2 = 0
|
||||
|
||||
print i, " : ", b1 + ( s - a1 ) * ( b2 - b1 ) / ( a2 - a1 )
|
||||
|
||||
next i
|
||||
13
Task/Map-range/D/map-range.d
Normal file
13
Task/Map-range/D/map-range.d
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
double mapRange(in double[] a, in double[] b, in double s)
|
||||
pure nothrow @nogc {
|
||||
return b[0] + ((s - a[0]) * (b[1] - b[0]) / (a[1] - a[0]));
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
immutable r1 = [0.0, 10.0];
|
||||
immutable r2 = [-1.0, 0.0];
|
||||
foreach (immutable s; 0 .. 11)
|
||||
writefln("%2d maps to %5.2f", s, mapRange(r1, r2, s));
|
||||
}
|
||||
11
Task/Map-range/ERRE/map-range.erre
Normal file
11
Task/Map-range/ERRE/map-range.erre
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
PROGRAM RANGE
|
||||
|
||||
BEGIN
|
||||
AL=0 AH=10
|
||||
BL=-1 BH=0
|
||||
FOR N=0 TO 10 DO
|
||||
RANGE=BL+(N-AL)*(BH-BL)/(AH-AL)
|
||||
WRITE("### maps to ##.##";N;RANGE)
|
||||
! PRINT(N;" maps to ";RANGE)
|
||||
END FOR
|
||||
END PROGRAM
|
||||
30
Task/Map-range/EchoLisp/map-range.l
Normal file
30
Task/Map-range/EchoLisp/map-range.l
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
(lib 'plot) ;; interpolation functions
|
||||
(lib 'compile)
|
||||
|
||||
;; rational version
|
||||
(define (q-map-range x xmin xmax ymin ymax) (+ ymin (/ ( * (- x xmin) (- ymax ymin)) (- xmax xmin))))
|
||||
|
||||
;; float version
|
||||
(define (map-range x xmin xmax ymin ymax) (+ ymin (// ( * (- x xmin) (- ymax ymin)) (- xmax xmin))))
|
||||
; accelerate it
|
||||
(compile 'map-range "-vf")
|
||||
|
||||
(q-map-range 4 0 10 -1 0)
|
||||
→ -3/5
|
||||
(map-range 4 0 10 -1 0)
|
||||
→ -0.6
|
||||
(linear 4 0 10 -1 0) ;; native
|
||||
→ -0.6
|
||||
|
||||
(for [(x (in-range 0 10))] (writeln x (q-map-range x 0 10 -1 0) (map-range x 0 10 -1 0)))
|
||||
|
||||
0 -1 -1
|
||||
1 -9/10 -0.9
|
||||
2 -4/5 -0.8
|
||||
3 -7/10 -0.7
|
||||
4 -3/5 -0.6
|
||||
5 -1/2 -0.5
|
||||
6 -2/5 -0.4
|
||||
7 -3/10 -0.3
|
||||
8 -1/5 -0.2
|
||||
9 -1/10 -0.1
|
||||
9
Task/Map-range/Elixir/map-range.elixir
Normal file
9
Task/Map-range/Elixir/map-range.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
defmodule RC do
|
||||
def map_range(a1 .. a2, b1 .. b2, s) do
|
||||
b1 + (s - a1) * (b2 - b1) / (a2 - a1)
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each(0..10, fn s ->
|
||||
:io.format "~2w map to ~7.3f~n", [s, RC.map_range(0..10, -1..0, s)]
|
||||
end)
|
||||
5
Task/Map-range/Emacs-Lisp/map-range.l
Normal file
5
Task/Map-range/Emacs-Lisp/map-range.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defun maprange (a1 a2 b1 b2 s)
|
||||
(+ b1 (/ (* (- s a1) (- b2 b1)) (- a2 a1))))
|
||||
|
||||
(dotimes (i 10)
|
||||
(message "%s" (maprange 0.0 10.0 -1.0 0.0 i)))
|
||||
5
Task/Map-range/Erlang/map-range.erl
Normal file
5
Task/Map-range/Erlang/map-range.erl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
-module(map_range).
|
||||
-export([map_value/3]).
|
||||
|
||||
map_value({A1,A2},{B1,B2},S) ->
|
||||
B1 + (S - A1) * (B2 - B1) / (A2 - A1).
|
||||
7
Task/Map-range/Euphoria/map-range.euphoria
Normal file
7
Task/Map-range/Euphoria/map-range.euphoria
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function map_range(sequence a, sequence b, atom s)
|
||||
return b[1]+(s-a[1])*(b[2]-b[1])/(a[2]-a[1])
|
||||
end function
|
||||
|
||||
for i = 0 to 10 do
|
||||
printf(1, "%2g maps to %4g\n", {i, map_range({0,10},{-1,0},i)})
|
||||
end for
|
||||
6
Task/Map-range/F-Sharp/map-range.fs
Normal file
6
Task/Map-range/F-Sharp/map-range.fs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
let map (a1: float) (a2: float) (b1: float) (b2: float) (s: float): float =
|
||||
b1 + (s - a1) * (b2 - b1) / (a2 - a1)
|
||||
|
||||
let xs = [| for i in 0..10 -> map 0.0 10.0 -1.0 0.0 (float i) |]
|
||||
|
||||
for x in xs do printfn "%f" x
|
||||
3
Task/Map-range/Factor/map-range-1.factor
Normal file
3
Task/Map-range/Factor/map-range-1.factor
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
USE: locals
|
||||
:: map-range ( a1 a2 b1 b2 x -- y )
|
||||
x a1 - b2 b1 - * a2 a1 - / b1 + ;
|
||||
5
Task/Map-range/Factor/map-range-2.factor
Normal file
5
Task/Map-range/Factor/map-range-2.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
USING: locals infix ;
|
||||
:: map-range ( a1 a2 b1 b2 x -- y )
|
||||
[infix
|
||||
b1 + (x - a1) * (b2 - b1) / (a2 - a1)
|
||||
infix] ;
|
||||
1
Task/Map-range/Factor/map-range-3.factor
Normal file
1
Task/Map-range/Factor/map-range-3.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
10 iota [| x | 0 10 -1 0 x map-range ] map . ! { -1 -9/10 -4/5 -7/10 -3/5 -1/2 -2/5 -3/10 -1/5 -1/10 }
|
||||
34
Task/Map-range/Fantom/map-range.fantom
Normal file
34
Task/Map-range/Fantom/map-range.fantom
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
class FRange
|
||||
{
|
||||
const Float low
|
||||
const Float high
|
||||
// in constructing a range, ensure the low value is smaller than high
|
||||
new make (Float low, Float high)
|
||||
{
|
||||
this.low = ( low <= high ? low : high )
|
||||
this.high = ( low <= high ? high : low )
|
||||
}
|
||||
|
||||
// return range as a string
|
||||
override Str toStr () { "[$low,$high]" }
|
||||
|
||||
// return a point in given range interpolated into this range
|
||||
Float remap (Float point, FRange given)
|
||||
{
|
||||
this.low + (point - given.low) * (this.high - this.low) / (given.high - given.low)
|
||||
}
|
||||
}
|
||||
|
||||
class Main
|
||||
{
|
||||
public static Void main ()
|
||||
{
|
||||
range1 := FRange (0f, 10f)
|
||||
range2 := FRange (-1f, 0f)
|
||||
11.times |Int n|
|
||||
{
|
||||
m := range2.remap (n.toFloat, range1)
|
||||
echo ("Value $n in ${range1} maps to $m in ${range2}")
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Task/Map-range/Forth/map-range-1.fth
Normal file
9
Task/Map-range/Forth/map-range-1.fth
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
\ linear interpolation
|
||||
|
||||
: lerp ( b2 b1 a2 a1 s -- t )
|
||||
fover f-
|
||||
frot frot f- f/
|
||||
frot frot fswap fover f- frot f*
|
||||
f+ ;
|
||||
|
||||
: test 11 0 do 0e -1e 10e 0e i s>f lerp f. loop ;
|
||||
3
Task/Map-range/Forth/map-range-2.fth
Normal file
3
Task/Map-range/Forth/map-range-2.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
: lerp ( o2 r2 r1 o1 s -- t ) fswap f- fswap f/ f* f+ ;
|
||||
|
||||
: test 11 0 do -1e 1e 10e 0e i s>f lerp f. loop ;
|
||||
21
Task/Map-range/Fortran/map-range.f
Normal file
21
Task/Map-range/Fortran/map-range.f
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
program Map
|
||||
implicit none
|
||||
|
||||
real :: t
|
||||
integer :: i
|
||||
|
||||
do i = 0, 10
|
||||
t = Maprange((/0.0, 10.0/), (/-1.0, 0.0/), real(i))
|
||||
write(*,*) i, " maps to ", t
|
||||
end do
|
||||
|
||||
contains
|
||||
|
||||
function Maprange(a, b, s)
|
||||
real :: Maprange
|
||||
real, intent(in) :: a(2), b(2), s
|
||||
|
||||
Maprange = (s-a(1)) * (b(2)-b(1)) / (a(2)-a(1)) + b(1)
|
||||
|
||||
end function Maprange
|
||||
end program Map
|
||||
8
Task/Map-range/FreeBASIC/map-range.basic
Normal file
8
Task/Map-range/FreeBASIC/map-range.basic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Function MapRange(s As Integer, a1 As Integer, a2 As Integer, b1 As Integer, b2 As Integer) As Double
|
||||
Return b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
End Function
|
||||
|
||||
For i As Integer = 0 To 10
|
||||
Print Using "## maps to ##.#"; i; MapRange(i,0,10,-1,0)
|
||||
Next i
|
||||
Sleep
|
||||
4
Task/Map-range/Frink/map-range-1.frink
Normal file
4
Task/Map-range/Frink/map-range-1.frink
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
mapRange[s, a1, a2, b1, b2] := b1 + (s-a1)(b2-b1)/(a2-a1)
|
||||
|
||||
for a = 0 to 10
|
||||
println["$a\t" + mapRange[a, 0, 10, -1, 0]]
|
||||
1
Task/Map-range/Frink/map-range-2.frink
Normal file
1
Task/Map-range/Frink/map-range-2.frink
Normal file
|
|
@ -0,0 +1 @@
|
|||
inverseMapRange[t, a1, a2, b1, b2] := a1 + a1 b1 (-1 b1 + b2)^-1 + -1 a2 b1 (-1 b1 + b2)^-1 + -1 a1 (-1 b1 + b2)^-1 t + a2 (-1 b1 + b2)^-1 t
|
||||
12
Task/Map-range/FutureBasic/map-range.basic
Normal file
12
Task/Map-range/FutureBasic/map-range.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn MapRange( s as double, a1 as double, a2 as double, b1 as double, b2 as double ) as double
|
||||
end fn = b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
|
||||
NSInteger i
|
||||
|
||||
for i = 0 to 10
|
||||
NSLog( @"%2d maps to %5.1f", i, fn MapRange( i, 0, 10, -1, 0 ) )
|
||||
next
|
||||
|
||||
HandleEvents
|
||||
5
Task/Map-range/GDScript/map-range.gd
Normal file
5
Task/Map-range/GDScript/map-range.gd
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
func mapRange(s:float, a1:float, a2:float, b1:float, b2:float) -> float :
|
||||
return b1 + ((b2-b1)/(a2-a1))*(s-a1)
|
||||
|
||||
for i in 11 :
|
||||
print( "%2d %+.1f" % [i,mapRange(i,0.0,10.0,-1.0,0.0)] )
|
||||
19
Task/Map-range/Go/map-range-1.go
Normal file
19
Task/Map-range/Go/map-range-1.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type rangeBounds struct {
|
||||
b1, b2 float64
|
||||
}
|
||||
|
||||
func mapRange(x, y rangeBounds, n float64) float64 {
|
||||
return y.b1 + (n - x.b1) * (y.b2 - y.b1) / (x.b2 - x.b1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
r1 := rangeBounds{0, 10}
|
||||
r2 := rangeBounds{-1, 0}
|
||||
for n := float64(0); n <= 10; n += 2 {
|
||||
fmt.Println(n, "maps to", mapRange(r1, r2, n))
|
||||
}
|
||||
}
|
||||
37
Task/Map-range/Go/map-range-2.go
Normal file
37
Task/Map-range/Go/map-range-2.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type rangeBounds struct {
|
||||
b1, b2 float64
|
||||
}
|
||||
|
||||
func newRangeMap(xr, yr rangeBounds) func(float64) (float64, bool) {
|
||||
// normalize direction of ranges so that out-of-range test works
|
||||
if xr.b1 > xr.b2 {
|
||||
xr.b1, xr.b2 = xr.b2, xr.b1
|
||||
yr.b1, yr.b2 = yr.b2, yr.b1
|
||||
}
|
||||
// compute slope, intercept
|
||||
m := (yr.b2 - yr.b1) / (xr.b2 - xr.b1)
|
||||
b := yr.b1 - m*xr.b1
|
||||
// return function literal
|
||||
return func(x float64) (y float64, ok bool) {
|
||||
if x < xr.b1 || x > xr.b2 {
|
||||
return 0, false // out of range
|
||||
}
|
||||
return m*x + b, true
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
rm := newRangeMap(rangeBounds{0, 10}, rangeBounds{-1, 0})
|
||||
for s := float64(-2); s <= 12; s += 2 {
|
||||
t, ok := rm(s)
|
||||
if ok {
|
||||
fmt.Printf("s: %5.2f t: %5.2f\n", s, t)
|
||||
} else {
|
||||
fmt.Printf("s: %5.2f out of range\n", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Task/Map-range/Go/map-range.go
Normal file
12
Task/Map-range/Go/map-range.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn MapRange( s as double, a1 as double, a2 as double, b1 as double, b2 as double ) as double
|
||||
end fn = b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
|
||||
NSInteger i
|
||||
|
||||
for i = 0 to 10
|
||||
NSLog( @"%2d maps to %5.1f", i, fn MapRange( i, 0, 10, -1, 0 ) )
|
||||
next
|
||||
|
||||
HandleEvents
|
||||
7
Task/Map-range/Groovy/map-range.groovy
Normal file
7
Task/Map-range/Groovy/map-range.groovy
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def mapRange(a1, a2, b1, b2, s) {
|
||||
b1 + ((s - a1) * (b2 - b1)) / (a2 - a1)
|
||||
}
|
||||
|
||||
(0..10).each { s ->
|
||||
println(s + " in [0, 10] maps to " + mapRange(0, 10, -1, 0, s) + " in [-1, 0].")
|
||||
}
|
||||
28
Task/Map-range/Haskell/map-range.hs
Normal file
28
Task/Map-range/Haskell/map-range.hs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import Data.Ratio
|
||||
import Text.Printf (PrintfType, printf)
|
||||
|
||||
-- Map a value from the range [a1,a2] to the range [b1,b2]. We don't check
|
||||
-- for empty ranges.
|
||||
mapRange
|
||||
:: Fractional a
|
||||
=> (a, a) -> (a, a) -> a -> a
|
||||
mapRange (a1, a2) (b1, b2) s = b1 + (s - a1) * (b2 - b1) / (a2 - a1)
|
||||
|
||||
main :: IO ()
|
||||
main
|
||||
-- Perform the mapping over floating point numbers.
|
||||
= do
|
||||
putStrLn "---------- Floating point ----------"
|
||||
mapM_ (\n -> prtD n . mapRange (0, 10) (-1, 0) $ fromIntegral n) [0 .. 10]
|
||||
-- Perform the same mapping over exact rationals.
|
||||
putStrLn "---------- Rationals ----------"
|
||||
mapM_ (\n -> prtR n . mapRange (0, 10) (-1, 0) $ n % 1) [0 .. 10]
|
||||
where
|
||||
prtD
|
||||
:: PrintfType r
|
||||
=> Integer -> Double -> r
|
||||
prtD = printf "%2d -> %6.3f\n"
|
||||
prtR
|
||||
:: PrintfType r
|
||||
=> Integer -> Rational -> r
|
||||
prtR n x = printf "%2d -> %s\n" n (show x)
|
||||
7
Task/Map-range/IS-BASIC/map-range.basic
Normal file
7
Task/Map-range/IS-BASIC/map-range.basic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
100 PROGRAM "MapRange.bas"
|
||||
110 LET A1=0:LET A2=10
|
||||
120 LET B1=-1:LET B2=0
|
||||
130 DEF MR(S)=B1+(S-A1)*(B2-B1)/(A2-A1)
|
||||
140 FOR I=0 TO 10
|
||||
150 PRINT I;"maps to ";MR(I)
|
||||
160 NEXT
|
||||
23
Task/Map-range/Icon/map-range-1.icon
Normal file
23
Task/Map-range/Icon/map-range-1.icon
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
record Range(a, b)
|
||||
|
||||
# note, we force 'n' to be real, which means recalculation will
|
||||
# be using real numbers, not integers
|
||||
procedure remap (range1, range2, n : real)
|
||||
if n < range2.a | n > range2.b then fail # n out of given range
|
||||
return range1.a + (n - range2.a) * (range1.b - range1.a) / (range2.b - range2.a)
|
||||
end
|
||||
|
||||
procedure range_string (range)
|
||||
return "[" || range.a || ", " || range.b || "]"
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
range1 := Range (0, 10)
|
||||
range2 := Range (-1, 0)
|
||||
# if i is out of range1, then 'remap' fails, so only valid changes are written
|
||||
every i := -2 to 12 do {
|
||||
if m := remap (range2, range1, i)
|
||||
then write ("Value " || i || " in " || range_string (range1) ||
|
||||
" maps to " || m || " in " || range_string (range2))
|
||||
}
|
||||
end
|
||||
5
Task/Map-range/Icon/map-range-2.icon
Normal file
5
Task/Map-range/Icon/map-range-2.icon
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
procedure remap (range1, range2, n)
|
||||
n *:= 1.0
|
||||
if n < range2.a | n > range2.b then fail # n out of given range
|
||||
return range1.a + (n - range2.a) * (range1.b - range1.a) / (range2.b - range2.a)
|
||||
end
|
||||
6
Task/Map-range/J/map-range-1.j
Normal file
6
Task/Map-range/J/map-range-1.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
maprange=:2 :0
|
||||
'a1 a2'=.m
|
||||
'b1 b2'=.n
|
||||
b1+((y-a1)*b2-b1)%a2-a1
|
||||
)
|
||||
NB. this version defers all calculations to runtime, but mirrors exactly the task formulation
|
||||
6
Task/Map-range/J/map-range-2.j
Normal file
6
Task/Map-range/J/map-range-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
maprange=:2 :0
|
||||
'a1 a2'=.m
|
||||
'b1 b2'=.n
|
||||
b1 + ((b2-b1)%a2-a1) * -&a1
|
||||
)
|
||||
NB. this version precomputes the scaling ratio
|
||||
1
Task/Map-range/J/map-range-3.j
Normal file
1
Task/Map-range/J/map-range-3.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
maprange=:{{ ({.n) + (n%&(-/)m) * -&({.m) }}
|
||||
2
Task/Map-range/J/map-range-4.j
Normal file
2
Task/Map-range/J/map-range-4.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
2 4 maprange 5 11 (2.718282 3 3.141592)
|
||||
7.15485 8 8.42478
|
||||
3
Task/Map-range/J/map-range-5.j
Normal file
3
Task/Map-range/J/map-range-5.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
adjust=:2 4 maprange 5 11 NB. save the derived function as a named entity
|
||||
adjust 2.718282 3 3.141592
|
||||
7.15485 8 8.42478
|
||||
2
Task/Map-range/J/map-range-6.j
Normal file
2
Task/Map-range/J/map-range-6.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
0 10 maprange _1 0 i.11
|
||||
_1 _0.9 _0.8 _0.7 _0.6 _0.5 _0.4 _0.3 _0.2 _0.1 0
|
||||
12
Task/Map-range/Java/map-range.java
Normal file
12
Task/Map-range/Java/map-range.java
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
public class Range {
|
||||
public static void main(String[] args){
|
||||
for(float s = 0;s <= 10; s++){
|
||||
System.out.println(s + " in [0, 10] maps to "+
|
||||
mapRange(0, 10, -1, 0, s)+" in [-1, 0].");
|
||||
}
|
||||
}
|
||||
|
||||
public static double mapRange(double a1, double a2, double b1, double b2, double s){
|
||||
return b1 + ((s - a1)*(b2 - b1))/(a2 - a1);
|
||||
}
|
||||
}
|
||||
12
Task/Map-range/JavaScript/map-range-1.js
Normal file
12
Task/Map-range/JavaScript/map-range-1.js
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
// Javascript doesn't have built-in support for ranges
|
||||
// Insted we use arrays of two elements to represent ranges
|
||||
var mapRange = function(from, to, s) {
|
||||
return to[0] + (s - from[0]) * (to[1] - to[0]) / (from[1] - from[0]);
|
||||
};
|
||||
|
||||
var range = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
for (var i = 0; i < range.length; i++) {
|
||||
range[i] = mapRange([0, 10], [-1, 0], range[i]);
|
||||
}
|
||||
|
||||
console.log(range);
|
||||
19
Task/Map-range/JavaScript/map-range-2.js
Normal file
19
Task/Map-range/JavaScript/map-range-2.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
var mapRange = function(from, to, s) {
|
||||
// mapRange expects ranges generated by _.range
|
||||
var a1 = from[0];
|
||||
var a2 = from[from.length - 1];
|
||||
var b1 = to[0];
|
||||
var b2 = to[to.length - 1];
|
||||
return b1 + (s - a1) * (b2 - b1) / (a2 - a1);
|
||||
};
|
||||
|
||||
// The range function is exclusive
|
||||
var fromRange = _.range(0, 11);
|
||||
var toRange = _.range(-1, 1);
|
||||
|
||||
// .map constructs a new array
|
||||
fromRange = fromRange.map(function(s) {
|
||||
return mapRange(fromRange, toRange, s);
|
||||
});
|
||||
|
||||
console.log(fromRange);
|
||||
129
Task/Map-range/JavaScript/map-range-3.js
Normal file
129
Task/Map-range/JavaScript/map-range-3.js
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// main :: IO ()
|
||||
const main = () => {
|
||||
|
||||
// rangeMap :: (Num, Num) -> (Num, Num) -> Num -> Num
|
||||
const rangeMap = (a, b) => s => {
|
||||
const [a1, a2] = a;
|
||||
const [b1, b2] = b;
|
||||
// Scaling up an order, and then down, to bypass a potential,
|
||||
// precision issue with negative numbers.
|
||||
return (((((b2 - b1) * (s - a1)) / (a2 - a1)) * 10) + (10 * b1)) / 10;
|
||||
};
|
||||
|
||||
const
|
||||
mapping = rangeMap([0, 10], [-1, 0]),
|
||||
xs = enumFromTo(0, 10),
|
||||
ys = map(mapping, xs),
|
||||
zs = map(approxRatio(''), ys);
|
||||
|
||||
|
||||
const formatted = (x, m, r) => {
|
||||
const
|
||||
fract = showRatio(r),
|
||||
[n, d] = splitOn('/', fract);
|
||||
return justifyRight(2, ' ', x.toString()) + ' -> ' +
|
||||
justifyRight(4, ' ', m.toString()) + ' = ' +
|
||||
justifyRight(2, ' ', n.toString()) + '/' + d.toString();
|
||||
};
|
||||
|
||||
console.log(
|
||||
unlines(zipWith3(formatted, xs, ys, zs))
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------
|
||||
|
||||
// abs :: Num -> Num
|
||||
const abs = Math.abs;
|
||||
|
||||
// Epsilon - > Real - > Ratio
|
||||
// approxRatio :: Real -> Real -> Ratio
|
||||
const approxRatio = eps => n => {
|
||||
const
|
||||
gcde = (e, x, y) => {
|
||||
const _gcd = (a, b) => (b < e ? a : _gcd(b, a % b));
|
||||
return _gcd(abs(x), abs(y));
|
||||
},
|
||||
c = gcde(Boolean(eps) ? eps : (1 / 10000), 1, abs(n)),
|
||||
r = ratio(quot(abs(n), c), quot(1, c));
|
||||
return {
|
||||
type: 'Ratio',
|
||||
n: r.n * signum(n),
|
||||
d: r.d
|
||||
};
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i)
|
||||
|
||||
// gcd :: Int -> Int -> Int
|
||||
const gcd = (x, y) => {
|
||||
const
|
||||
_gcd = (a, b) => (0 === b ? a : _gcd(b, a % b)),
|
||||
abs = Math.abs;
|
||||
return _gcd(abs(x), abs(y));
|
||||
};
|
||||
|
||||
// justifyRight :: Int -> Char -> String -> String
|
||||
const justifyRight = (n, cFiller, s) =>
|
||||
n > s.length ? (
|
||||
s.padStart(n, cFiller)
|
||||
) : s;
|
||||
|
||||
// Returns Infinity over objects without finite length
|
||||
// this enables zip and zipWith to choose the shorter
|
||||
// argument when one is non-finite, like cycle, repeat etc
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs => Array.isArray(xs) ? xs.length : Infinity;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) => xs.map(f);
|
||||
|
||||
// quot :: Int -> Int -> Int
|
||||
const quot = (n, m) => Math.floor(n / m);
|
||||
|
||||
// ratio :: Int -> Int -> Ratio Int
|
||||
const ratio = (x, y) => {
|
||||
const go = (x, y) =>
|
||||
0 !== y ? (() => {
|
||||
const d = gcd(x, y);
|
||||
return {
|
||||
type: 'Ratio',
|
||||
'n': quot(x, d), // numerator
|
||||
'd': quot(y, d) // denominator
|
||||
};
|
||||
})() : undefined;
|
||||
return go(x * signum(y), abs(y));
|
||||
};
|
||||
|
||||
// showRatio :: Ratio -> String
|
||||
const showRatio = nd =>
|
||||
nd.n.toString() + '/' + nd.d.toString();
|
||||
|
||||
// signum :: Num -> Num
|
||||
const signum = n => 0 > n ? -1 : (0 < n ? 1 : 0);
|
||||
|
||||
// splitOn :: String -> String -> [String]
|
||||
const splitOn = (pat, src) =>
|
||||
src.split(pat);
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
|
||||
const zipWith3 = (f, xs, ys, zs) =>
|
||||
Array.from({
|
||||
length: Math.min(length(xs), length(ys), length(zs))
|
||||
}, (_, i) => f(xs[i], ys[i], zs[i]));
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
5
Task/Map-range/Jq/map-range-1.jq
Normal file
5
Task/Map-range/Jq/map-range-1.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# The input is the value to be mapped.
|
||||
# The ranges, a and b, should each be an array defining the
|
||||
# left-most and right-most points of the range.
|
||||
def maprange(a; b):
|
||||
b[0] + (((. - a[0]) * (b[1] - b[0])) / (a[1] - a[0])) ;
|
||||
1
Task/Map-range/Jq/map-range-2.jq
Normal file
1
Task/Map-range/Jq/map-range-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
range(0;11) | maprange([0,10]; [-1, 0])
|
||||
5
Task/Map-range/Jq/map-range-3.jq
Normal file
5
Task/Map-range/Jq/map-range-3.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def maprange_array(a; b):
|
||||
def _helper(a0; b0; factor): b0 + (. - a0) * factor;
|
||||
|
||||
a[0] as $a | b[0] as $b | ((b[1] - b[0]) / (a[1] - a[0])) as $factor
|
||||
| map(_helper( $a; $b; $factor) );
|
||||
8
Task/Map-range/Julia/map-range.julia
Normal file
8
Task/Map-range/Julia/map-range.julia
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function maprange(s, a, b)
|
||||
a₁, a₂ = minimum(a), maximum(a)
|
||||
b₁, b₂ = minimum(b), maximum(b)
|
||||
return b₁ + (s - a₁) * (b₂ - b₁) / (a₂ - a₁)
|
||||
end
|
||||
|
||||
@show maprange(6, 1:10, -1:0)
|
||||
@show maprange(0:10, 0:10, -1:0)
|
||||
14
Task/Map-range/K/map-range.k
Normal file
14
Task/Map-range/K/map-range.k
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
f:{[a1;a2;b1;b2;s] b1+(s-a1)*(b2-b1)%(a2-a1)}
|
||||
|
||||
+(a; f[0;10;-1;0]'a:!11)
|
||||
((0;-1.0)
|
||||
(1;-0.9)
|
||||
(2;-0.8)
|
||||
(3;-0.7)
|
||||
(4;-0.6)
|
||||
(5;-0.5)
|
||||
(6;-0.4)
|
||||
(7;-0.3)
|
||||
(8;-0.2)
|
||||
(9;-0.1)
|
||||
(10;0.0))
|
||||
16
Task/Map-range/Kotlin/map-range.kotlin
Normal file
16
Task/Map-range/Kotlin/map-range.kotlin
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// version 1.0.6
|
||||
|
||||
class FloatRange(override val start: Float, override val endInclusive: Float) : ClosedRange<Float>
|
||||
|
||||
fun mapRange(range1: FloatRange, range2: FloatRange, value: Float): Float {
|
||||
if (value !in range1) throw IllegalArgumentException("value is not within the first range")
|
||||
if (range1.endInclusive == range1.start) throw IllegalArgumentException("first range cannot be single-valued")
|
||||
return range2.start + (value - range1.start) * (range2.endInclusive - range2.start) / (range1.endInclusive - range1.start)
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
for (i in 0..10) {
|
||||
val mappedValue = mapRange(FloatRange(0.0f, 10.0f), FloatRange(-1.0f, 0.0f), i.toFloat())
|
||||
println(String.format("%2d maps to %+4.2f", i, mappedValue))
|
||||
}
|
||||
}
|
||||
21
Task/Map-range/Lambdatalk/map-range.lambdatalk
Normal file
21
Task/Map-range/Lambdatalk/map-range.lambdatalk
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{def maprange
|
||||
{lambda {:a0 :a1 :b0 :b1 :s}
|
||||
{+ :b0 {/ {* {- :s :a0} {- :b1 :b0}} {- :a1 :a0}}}}}
|
||||
-> maprange
|
||||
|
||||
{maprange 0 10 -1 0 5}
|
||||
-> -0.5
|
||||
|
||||
{S.map {maprange 0 10 -1 0} {S.serie 0 10}}
|
||||
->
|
||||
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.30000000000000004
|
||||
8 maps to -0.19999999999999996
|
||||
9 maps to -0.09999999999999998
|
||||
10 maps to 0
|
||||
15
Task/Map-range/Lasso/map-range.lasso
Normal file
15
Task/Map-range/Lasso/map-range.lasso
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
define map_range(
|
||||
a1,
|
||||
a2,
|
||||
b1,
|
||||
b2,
|
||||
number
|
||||
) => (decimal(#b1) + (decimal(#number) - decimal(#a1)) * (decimal(#b2) - decimal(#b1)) / (decimal(#a2) - decimal(#a1))) -> asstring(-Precision = 1)
|
||||
|
||||
with number in generateSeries(1,10) do {^
|
||||
#number
|
||||
': '
|
||||
map_range( 0, 10, -1, 0, #number)
|
||||
'<br />'
|
||||
|
||||
^}'
|
||||
8
Task/Map-range/Liberty-BASIC/map-range.basic
Normal file
8
Task/Map-range/Liberty-BASIC/map-range.basic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
For i = 0 To 10
|
||||
Print "f(";i;") maps to ";mapToRange(i, 0, 10, -1, 0)
|
||||
Next i
|
||||
End
|
||||
|
||||
Function mapToRange(value, inputMin, inputMax, outputMin, outputMax)
|
||||
mapToRange = (((value - inputMin) * (outputMax - outputMin)) / (inputMax - inputMin)) + outputMin
|
||||
End Function
|
||||
5
Task/Map-range/Logo/map-range.logo
Normal file
5
Task/Map-range/Logo/map-range.logo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
to interpolate :s :a1 :a2 :b1 :b2
|
||||
output (:s-:a1) / (:a2-:a1) * (:b2-:b1) + :b1
|
||||
end
|
||||
|
||||
for [i 0 10] [print interpolate :i 0 10 -1 0]
|
||||
7
Task/Map-range/Lua/map-range.lua
Normal file
7
Task/Map-range/Lua/map-range.lua
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
function map_range( a1, a2, b1, b2, s )
|
||||
return b1 + (s-a1)*(b2-b1)/(a2-a1)
|
||||
end
|
||||
|
||||
for i = 0, 10 do
|
||||
print( string.format( "f(%d) = %f", i, map_range( 0, 10, -1, 0, i ) ) )
|
||||
end
|
||||
21
Task/Map-range/M2000-Interpreter/map-range-1.m2000
Normal file
21
Task/Map-range/M2000-Interpreter/map-range-1.m2000
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
module MapRange {
|
||||
class Map {
|
||||
private:
|
||||
a, b, f
|
||||
public:
|
||||
value (x){
|
||||
=.b+(x-.a)*.f
|
||||
}
|
||||
class:
|
||||
module Map (.a,a2,.b,b2) {
|
||||
if a2-.a=0 then error "wrong parameters"
|
||||
.f<=(b2-.b)/(a2-.a)
|
||||
}
|
||||
}
|
||||
|
||||
m1=Map(0,10, -1, 0)
|
||||
for i=0 to 10
|
||||
Print i," maps to ";m1(i)
|
||||
next
|
||||
}
|
||||
MapRange
|
||||
12
Task/Map-range/M2000-Interpreter/map-range-2.m2000
Normal file
12
Task/Map-range/M2000-Interpreter/map-range-2.m2000
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
module MapRange {
|
||||
Map=lambda (a,a2,b,b2) -> {
|
||||
if a2-a=0 then error "wrong parameters"
|
||||
f=(b2-b)/(a2-a)
|
||||
=lambda a,b,f (x)->b+(x-a)*f
|
||||
}
|
||||
m1=Map(0,10, -1, 0)
|
||||
for i=0 to 10
|
||||
Print i," maps to ";m1(i)
|
||||
next
|
||||
}
|
||||
MapRange
|
||||
8
Task/Map-range/Maple/map-range.maple
Normal file
8
Task/Map-range/Maple/map-range.maple
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Map:=proc(a1,a2,b1,b2,s);
|
||||
return (b1+((s-a1)*(b2-b1)/(a2-a1)));
|
||||
end proc;
|
||||
|
||||
for i from 0 to 10 do
|
||||
printf("%a maps to ",i);
|
||||
printf("%a\n",Map(0,10,-1,0,i));
|
||||
end do;
|
||||
1
Task/Map-range/Mathematica/map-range.math
Normal file
1
Task/Map-range/Mathematica/map-range.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
Rescale[#,{0,10},{-1,0}]&/@Range[0,10]
|
||||
4
Task/Map-range/Maxima/map-range.maxima
Normal file
4
Task/Map-range/Maxima/map-range.maxima
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
maprange(a, b, c, d) := buildq([e: ratsimp(('x - a)*(d - c)/(b - a) + c)],
|
||||
lambda([x], e))$
|
||||
|
||||
f: maprange(0, 10, -1, 0);
|
||||
18
Task/Map-range/Nemerle/map-range.nemerle
Normal file
18
Task/Map-range/Nemerle/map-range.nemerle
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module Maprange
|
||||
{
|
||||
Maprange(a : double * double, b : double * double, s : double) : double
|
||||
{
|
||||
def (a1, a2) = a; def (b1, b2) = b;
|
||||
|
||||
b1 + (((s - a1) * (b2 - b1))/(a2 - a1))
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
foreach (i in [0 .. 10])
|
||||
WriteLine("{0, 2:f0} maps to {1:f1}", i, Maprange((0.0, 10.0), (-1.0, 0.0), i));
|
||||
}
|
||||
}
|
||||
20
Task/Map-range/NetRexx/map-range.netrexx
Normal file
20
Task/Map-range/NetRexx/map-range.netrexx
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
|
||||
A = [ 0.0, 10.0 ]
|
||||
B = [ -1.0, 0.0 ]
|
||||
incr = 1.0
|
||||
|
||||
say 'Mapping ['A[0]',' A[1]'] to ['B[0]',' B[1]'] in increments of' incr':'
|
||||
loop sVal = A[0] to A[1] by incr
|
||||
say ' f('sVal.format(3, 3)') =' mapRange(A, B, sVal).format(4, 3)
|
||||
end sVal
|
||||
|
||||
return
|
||||
|
||||
method mapRange(a = Rexx[], b = Rexx[], s_) public static
|
||||
return mapRange(a[0], a[1], b[0], b[1], s_)
|
||||
|
||||
method mapRange(a1, a2, b1, b2, s_) public static
|
||||
t_ = b1 + ((s_ - a1) * (b2 - b1) / (a2 - a1))
|
||||
return t_
|
||||
10
Task/Map-range/Nim/map-range.nim
Normal file
10
Task/Map-range/Nim/map-range.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import strformat
|
||||
|
||||
type FloatRange = tuple[s, e: float]
|
||||
|
||||
proc mapRange(a, b: FloatRange; s: float): float =
|
||||
b.s + (s - a.s) * (b.e - b.s) / (a.e - a.s)
|
||||
|
||||
for i in 0..10:
|
||||
let m = mapRange((0.0,10.0), (-1.0, 0.0), float(i))
|
||||
echo &"{i:>2} maps to {m:4.1f}"
|
||||
8
Task/Map-range/OCaml/map-range-1.ocaml
Normal file
8
Task/Map-range/OCaml/map-range-1.ocaml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
let map_range (a1, a2) (b1, b2) s =
|
||||
b1 +. ((s -. a1) *. (b2 -. b1) /. (a2 -. a1))
|
||||
|
||||
let () =
|
||||
print_endline "Mapping [0,10] to [-1,0] at intervals of 1:";
|
||||
for i = 0 to 10 do
|
||||
Printf.printf "f(%d) = %g\n" i (map_range (0.0, 10.0) (-1.0, 0.0) (float i))
|
||||
done
|
||||
11
Task/Map-range/OCaml/map-range-2.ocaml
Normal file
11
Task/Map-range/OCaml/map-range-2.ocaml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
let map_range (a1, a2) (b1, b2) =
|
||||
let v = (b2 -. b1) /. (a2 -. a1) in
|
||||
function s ->
|
||||
b1 +. ((s -. a1) *. v)
|
||||
|
||||
let () =
|
||||
print_endline "Mapping [0,10] to [-1,0] at intervals of 1:";
|
||||
let p = (map_range (0.0, 10.0) (-1.0, 0.0)) in
|
||||
for i = 0 to 10 do
|
||||
Printf.printf "f(%d) = %g\n" i (p (float i))
|
||||
done
|
||||
14
Task/Map-range/Objeck/map-range.objeck
Normal file
14
Task/Map-range/Objeck/map-range.objeck
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
bundle Default {
|
||||
class Range {
|
||||
function : MapRange(a1:Float, a2:Float, b1:Float, b2:Float, s:Float) ~ Float {
|
||||
return b1 + (s-a1)*(b2-b1)/(a2-a1);
|
||||
}
|
||||
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
"Mapping [0,10] to [-1,0] at intervals of 1:"->PrintLine();
|
||||
for(i := 0.0; i <= 10.0; i += 1;) {
|
||||
IO.Console->Print("f(")->Print(i->As(Int))->Print(") = ")->PrintLine(MapRange(0.0, 10.0, -1.0, 0.0, i));
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Task/Map-range/Oforth/map-range.fth
Normal file
3
Task/Map-range/Oforth/map-range.fth
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
: mapRange(p1, p2, s)
|
||||
s p1 first - p2 second p2 first - * p1 second p1 first - asFloat /
|
||||
p2 first + ;
|
||||
1
Task/Map-range/PARI-GP/map-range.parigp
Normal file
1
Task/Map-range/PARI-GP/map-range.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
map(r1,r2,x)=r2[1]+(x-r1[1])*(r2[2]-r2[1])/(r1[2]-r1[1])
|
||||
13
Task/Map-range/PL-I/map-range.pli
Normal file
13
Task/Map-range/PL-I/map-range.pli
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
map: procedure options (main); /* 24/11/2011 */
|
||||
declare (a1, a2, b1, b2) float;
|
||||
declare d fixed decimal (3,1);
|
||||
|
||||
do d = 0 to 10 by 0.9, 10;
|
||||
put skip edit ( d, ' maps to ', map(0, 10, -1, 0, d) ) (f(5,1), a, f(10,6));
|
||||
end;
|
||||
|
||||
map: procedure (a1, a2, b1, b2, s) returns (float);
|
||||
declare (a1, a2, b1, b2, s) float;
|
||||
return (b1 + (s - a1)*(b2 - b1) / (a2 - a1) );
|
||||
end map;
|
||||
end map;
|
||||
13
Task/Map-range/Pascal/map-range-1.pas
Normal file
13
Task/Map-range/Pascal/map-range-1.pas
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Program Map(output);
|
||||
|
||||
function MapRange(fromRange, toRange: array of real; value: real): real;
|
||||
begin
|
||||
MapRange := (value-fromRange[0]) * (toRange[1]-toRange[0]) / (fromRange[1]-fromRange[0]) + toRange[0];
|
||||
end;
|
||||
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
for i := 0 to 10 do
|
||||
writeln (i, ' maps to: ', MapRange([0.0, 10.0], [-1.0, 0.0], i):4:2);
|
||||
end.
|
||||
56
Task/Map-range/Pascal/map-range-2.pas
Normal file
56
Task/Map-range/Pascal/map-range-2.pas
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Program Map(output);
|
||||
|
||||
type
|
||||
real = double;
|
||||
tRange = Array [0..1] of real;
|
||||
tMapRec = record
|
||||
mrFrom,
|
||||
mrTo : tRange;
|
||||
mrScale : real
|
||||
end;
|
||||
|
||||
function InitRange(rfrom,rTo:real):tRange;
|
||||
begin
|
||||
InitRange[0] :=rfrom;
|
||||
InitRange[1] :=rTo;
|
||||
end;
|
||||
|
||||
function InitMapRec(const fromRange, toRange: tRange):tMapRec;
|
||||
begin
|
||||
With InitMapRec do
|
||||
Begin
|
||||
mrFrom := fromRange;
|
||||
mrTo := toRange;
|
||||
mrScale := (toRange[1]-toRange[0]) / (fromRange[1]-fromRange[0]);
|
||||
end;
|
||||
end;
|
||||
|
||||
function MapRecRange(const value: real;var MR :tMapRec): real;
|
||||
begin
|
||||
with MR do
|
||||
MapRecRange := (value-mrFrom[0]) * mrScale + mrTo[0];
|
||||
end;
|
||||
|
||||
function MapRange(const value: real;const fromRange, toRange: tRange): real;
|
||||
begin
|
||||
MapRange := (value-fromRange[0]) * (toRange[1]-toRange[0]) / (fromRange[1]-fromRange[0]) + toRange[0];
|
||||
end;
|
||||
|
||||
var
|
||||
value:real;
|
||||
rFrom,rTo : tRange;
|
||||
mr : tMapRec;
|
||||
i: LongInt;
|
||||
|
||||
begin
|
||||
rFrom:= InitRange( 0, 10);
|
||||
rTo := InitRange( -1, 0);
|
||||
mr:= InitMapRec(rFrom,rTo);
|
||||
|
||||
for i := 0 to 10 do
|
||||
Begin
|
||||
value := i;
|
||||
writeln (i:4, ' maps to: ', MapRange(value,rFrom, rTo):10:6,
|
||||
MapRecRange(value,mr):10:6);
|
||||
end;
|
||||
end.
|
||||
12
Task/Map-range/Perl/map-range.pl
Normal file
12
Task/Map-range/Perl/map-range.pl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/perl -w
|
||||
use strict ;
|
||||
|
||||
sub mapValue {
|
||||
my ( $range1 , $range2 , $number ) = @_ ;
|
||||
return ( $range2->[ 0 ] +
|
||||
(( $number - $range1->[ 0 ] ) * ( $range2->[ 1 ] - $range2->[ 0 ] ) ) / ( $range1->[ -1 ]
|
||||
- $range1->[ 0 ] ) ) ;
|
||||
}
|
||||
my @numbers = 0..10 ;
|
||||
my @interval = ( -1 , 0 ) ;
|
||||
print "The mapped value for $_ is " . mapValue( \@numbers , \@interval , $_ ) . " !\n" foreach @numbers ;
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue