I have a value from a, say 5. If I were to map it onto range b its equivalent would be 50. In python you can write a function that does this like this:
I thought they used range(start, stop) or something.
I guess I was confused by the use of the word ārangeā, and the reference to 1:10. The OP actually isnāt asking about ranges at all, but tuples, both in Python and Julia.
julia> function map_ranges(t1 ::Tuple, t2::Tuple, val)
r1=range(t1...)
r2=range(t2...,length(r1))
return r2[val]
end
map_ranges (generic function with 2 methods)
julia> map_ranges((1,10),(2,37),1)
2.0
julia> map_ranges((1,10),(2,37),10)
37.0
julia> map_ranges((1,10),(2,37),7)
25.333333333333332
This scheme seems to me more adherent to the Python version and responds to the expectation that the same mapping can be done in Julia in a more āsimpleā way
In my opinion, itās as you say! As I wrote without using the linear transformation formula, which is implicitly realized by the length parameter of range(start, stop, length).
Being a linear mapping, it needs to be verified (only) in two distinct points (only one straight line passes through two points ).
The reason you get 45 is that in python the same range would be 0 to 100 (not including the end). Yes, the python version uses tuples because using actual ranges in python would be impractical. I was wondering if there was a better way to do it in julia. Using actual ranges is more readable imo.
To me it looks like ranges are the wrong concept for this. Arenāt you actually looking for an interval. Ranges donāt just have a start and a stop, but a steplength or resolution as well. It doesnāt make sense to me to say that 1:10 vs 1:100 maps 5 to 50. This makes more sense for intervals (though it should be [0, 10] and [0, 100]).
Tuples are more suited to describing intervals than ranges are.