Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67886 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 56187 invoked from network); 26 Jun 2013 15:49:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jun 2013 15:49:03 -0000 Authentication-Results: pb1.pair.com header.from=pencap@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=pencap@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.175 as permitted sender) X-PHP-List-Original-Sender: pencap@gmail.com X-Host-Fingerprint: 209.85.223.175 mail-ie0-f175.google.com Received: from [209.85.223.175] ([209.85.223.175:62642] helo=mail-ie0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 89/A5-29746-D6D0BC15 for ; Wed, 26 Jun 2013 11:49:02 -0400 Received: by mail-ie0-f175.google.com with SMTP id a13so31111687iee.20 for ; Wed, 26 Jun 2013 08:48:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=IUgYJ3OHJOzYKy8VowF+cEpOofElJ/5/OCVqFoADBqM=; b=mk0i5j6w/+x0O25yOVVr+1mTEbw161JHwhsoqJf7d2+Miqxj6gS2/VKHfGb0B22QXr dwi1wJ5MNBTIe4YCuHOg7EuJcH9zyjzfJ41LWg89SHazyw5HpX26bYYJua9U1qvwc2bu X9gIi0PXtAHB3R3dtIPWBmjRlmR43rMuput5fFVwWL1QSdgNs3GUg/q+lKa6oOYz8BBz qlCJk1kxgD1ddnEbtJOu1uSfg/ZTsmGF3mJkrPlsGYiK7nPC6DLuZZlwAlLo/EgRUBdl WZfk01BdqIc+exdyTfGg3FLK7rTQXz+OUPjN7v7hC9kSncbD40V4ZuGlSGfCybCOIPmU 3QGQ== MIME-Version: 1.0 X-Received: by 10.42.249.72 with SMTP id mj8mr2484178icb.32.1372261739444; Wed, 26 Jun 2013 08:48:59 -0700 (PDT) Received: by 10.50.76.132 with HTTP; Wed, 26 Jun 2013 08:48:59 -0700 (PDT) In-Reply-To: <4622f2d7-8c09-4a31-943e-7732a9481a9a@me.com> References: <4622f2d7-8c09-4a31-943e-7732a9481a9a@me.com> Date: Wed, 26 Jun 2013 08:48:59 -0700 Message-ID: To: Jeremy Curcio Cc: PHP Internals Content-Type: multipart/alternative; boundary=20cf3010ecc984c33e04e01095dd Subject: Re: [PHP-DEV] Gauging Interest:RFC to add map() function From: pencap@gmail.com (Mike Willbanks) --20cf3010ecc984c33e04e01095dd Content-Type: text/plain; charset=UTF-8 Hello Jeremy, On Wed, Jun 26, 2013 at 8:20 AM, Jeremy Curcio 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. > A map() function is normally part of functional programming and as such would cause confusion and likely would not mean what most programmers would assume. See python and JS docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map http://docs.python.org/2/howto/functional.html Yes; we do have array_map; but I still do not assume that this would be a mathematical function. > > 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); > } > I am curious with something that is so easy; why would you want it in core? > > 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 > j.curcio@me.com > --20cf3010ecc984c33e04e01095dd--