Re: Gauging Interest:RFC to add map() function

From: Date: Thu, 27 Jun 2013 08:27:21 +0000
Subject: Re: Gauging Interest:RFC to add map() function
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Thu, Jun 27, 2013 at 10:17 AM, Tjerk Anne Meesters <[email protected]> wrote:
> On Wed, Jun 26, 2013 at 11:20 PM, Jeremy Curcio <[email protected]> wrote:
>
>> Hello,
>>
>> I would like to submit an RFC to add a new function to the PHP language.
>> The function would be called "map()". The purpose of this function would be
>> to take an existing value within a range and make it to a corresponding
>> location within a new range.
>>
>> The map() method would have 5 required parameters, $originalLow,
>> $originalHigh, $newLow, $newHigh, and $value.
>>
>> map() would be implement the following:
>>
>> function map($originalLow, $originalHigh, $newLow, $newHigh, $value) {
>> return $newLow + ($value - $originalLow) * ($newHigh - $newLow) /
>> ($originalHigh- $originalLow);
>> }
>>
>>
> Purely from a development perspective, having five required function
> arguments is bad; it can be reduced (by treating the first four as two
> ranges) to three arguments, e.g.
>
>     map([55, 92], [70, 100], 55);
>
> You can go one step further and call it what it is, not a mapper, but a
> single dimensional range transformer and use a closure, e.g.:
>
>     $transformer = get_1d_range_transformer([55, 92], [70, 100]);
>     echo $transformer(55); // get transformed value
>
> You might also benefit from an OOP approach. I won't paste it here, but
> I've created a pastie for it: http://codepad.org/nGZv8GJa
>
> It's debatable whether this somewhat specialized code would need to be
> coded at something other than the language level; in most likelihood you
> won't gain any appreciable performance increase.
>
>
>> Example:
>> Let's say we are teachers and are grading final exams. We have a policy
>> that the best score is 100, and the worst score is a 70. Students scored
>> between 55 and 92. We want to easily re-score the exams to be within the
>> new score range, so we would use the new map() function. Let's begin with
>> mapping the lowest score:
>>
>> $newScore = map(55, 92, 70, 100, 55); //$newScore = 70
>>
>> If we have all of our scores in an array:
>>
>> $scores = array(71, 65, 55, 85, 88, 86, 92, 77, 73);
>>
>> We could use a foreach loop to remap each value:
>>
>> $newScores = array();
>> foreach($score as $scores) {
>>  $newScores[] = map(55, 92, 70, 100, $score);
>> }
>> var_dump($newScores);
>> /*
>> array(9) {
>>   [0]=>
>>   float(82.972972972973)
>>   [1]=>
>>   float(78.108108108108)
>>   [2]=>
>>   int(70)
>>   [3]=>
>>   float(94.324324324324)
>>   [4]=>
>>   float(96.756756756757)
>>   [5]=>
>>   float(95.135135135135)
>>   [6]=>
>>   int(100)
>>   [7]=>
>>   float(87.837837837838)
>>   [8]=>
>>   float(84.594594594595)
>>   }
>> */
>>
>> Just like that, we have the new exam grades that fit our policy, within
>> the proper scale, without having to do any of the messy math ourselves.
>>
>> While I do recognize that this is somewhat trivial to anyone who knows the
>> proper formula, I feel as though it would serve the PHP community well.
>> Much the same as the pow() or pi() functions do. I appreciate your thoughts
>> on this matter and whether or not this is worth pursuing as an RFC.
>>
>> Thank you,
>> Jeremy Curcio
>> [email protected]
>>
>
>
>
> --
> --
> Tjerk


Hi,


May I kindly ask why all the PHP users would want this function in the core?
I've never needed such a function nor do I understand why we should
have it. It's good for what?
And as I understand, PHP delegates the math stuff to the underlying C
implementation so why would it be faster having it in PHP core rather
that in PHP userland?


Thanks
----
Florin Patan
https://github.com/dlsniper
http://www.linkedin.com/in/florinpatan


Thread (15 messages)

« previous php.internals (#67924) next »