Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67922 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94746 invoked from network); 27 Jun 2013 08:17:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Jun 2013 08:17:07 -0000 Authentication-Results: pb1.pair.com smtp.mail=tjerk.meesters@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tjerk.meesters@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.44 as permitted sender) X-PHP-List-Original-Sender: tjerk.meesters@gmail.com X-Host-Fingerprint: 209.85.216.44 mail-qa0-f44.google.com Received: from [209.85.216.44] ([209.85.216.44:46965] helo=mail-qa0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 78/28-51393-005FBC15 for ; Thu, 27 Jun 2013 04:17:05 -0400 Received: by mail-qa0-f44.google.com with SMTP id o13so1868095qaj.10 for ; Thu, 27 Jun 2013 01:17:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=jey8aOdlOv08Jok9X3lN5uiLDjLMSN6sJJwL/XoG0NE=; b=Tc1skNgtS7TMFJrC2sXG80vkiUIKXhCfTFsMWelnYR5u0h3GQy0XQ+K8gzSFs75WsK THY0mJym3/WkzWO9J0CDi3LOB4PD90Il7ehWtQQcP0s9jW1PQYt8255PrhGk10AZj8/y +UYG1LZ15M8ASCyRjgIXZufx97DEEQWnO1hDP9i9SbbLYaFgSMkx9BjQrBGrC+OjZ+NI HCAJpaRZMAEaLqgta5vKQVpMK7W20Uz/sVPeHkAp7FH2dTGxqp1jM8mkCGJFrH2uJe1+ 4WFcW5eKXVnjE+AzpAboDlFDfJUxbn5EbTqZDF+Gf3bq8QFHQee/sVRN1nm6vO5WN2If XJsw== MIME-Version: 1.0 X-Received: by 10.224.13.19 with SMTP id z19mr10269743qaz.12.1372321022054; Thu, 27 Jun 2013 01:17:02 -0700 (PDT) Sender: tjerk.meesters@gmail.com Received: by 10.49.99.67 with HTTP; Thu, 27 Jun 2013 01:17:01 -0700 (PDT) In-Reply-To: <4622f2d7-8c09-4a31-943e-7732a9481a9a@me.com> References: <4622f2d7-8c09-4a31-943e-7732a9481a9a@me.com> Date: Thu, 27 Jun 2013 16:17:01 +0800 X-Google-Sender-Auth: Ohgf3fTsnIxUkhmLSQAinIqfL8Y Message-ID: To: Jeremy Curcio Cc: PHP Internals Content-Type: multipart/alternative; boundary=089e0149ce0409984304e01e632c Subject: Re: [PHP-DEV] Gauging Interest:RFC to add map() function From: datibbaw@php.net (Tjerk Anne Meesters) --089e0149ce0409984304e01e632c Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jun 26, 2013 at 11:20 PM, 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. > > 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 > j.curcio@me.com > -- -- Tjerk --089e0149ce0409984304e01e632c--