<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=JavaScript%2FHigher-order_programming%2FExample_2</id>
		<title>JavaScript/Higher-order programming/Example 2 - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=JavaScript%2FHigher-order_programming%2FExample_2"/>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Higher-order_programming/Example_2&amp;action=history"/>
		<updated>2026-05-10T12:41:05Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.29.0</generator>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=JavaScript/Higher-order_programming/Example_2&amp;diff=834&amp;oldid=prev</id>
		<title>Apollolux: created</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Higher-order_programming/Example_2&amp;diff=834&amp;oldid=prev"/>
				<updated>2013-06-15T00:42:30Z</updated>
		
		<summary type="html">&lt;p&gt;created&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Say I've got this, an array of strings holding stats to be displayed in a box:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var stats = [&lt;br /&gt;
  &amp;quot;Hero L&amp;quot; + level,&lt;br /&gt;
  &amp;quot;Exp: &amp;quot; + exp + &amp;quot; / &amp;quot; + next,&lt;br /&gt;
  &amp;quot;&amp;quot;,&lt;br /&gt;
  &amp;quot;HP: &amp;quot; + hp + &amp;quot; / &amp;quot; + hpMax,&lt;br /&gt;
  &amp;quot;MP: &amp;quot; + mp + &amp;quot; / &amp;quot; + mpMax&lt;br /&gt;
];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In order to draw a window style around it, we'll need to find out which line is longest.&lt;br /&gt;
&lt;br /&gt;
===The chump's way===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var maxWidth = 0;&lt;br /&gt;
for (var i = 0; i &amp;lt; stats.length; ++i) {&lt;br /&gt;
  if (maxWidth &amp;lt; GetSystemFont().getStringWidth(stats[i]))&lt;br /&gt;
    maxWidth = GetSystemFont().getStringWidth(stats[i]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What's wrong with it?&lt;br /&gt;
&lt;br /&gt;
*	''&amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop is crappy'' - We're good coders, so we recognise that this looks for a maximum without even reading it. But if we really sat down and ''read'' it, we're looking at 20+ tokens spread across four lines. And this is just a simple loop. In a more complex loop body, a bug would be almost impossible to find.&lt;br /&gt;
*	''Focus is on iteration instead of task'' - The variable &amp;lt;var&amp;gt;i&amp;lt;/var&amp;gt; occurs in no less than 5 places. As it turns out later on, we don't even need it.&lt;br /&gt;
*	''Copy-paste coding style'' - The body of the &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop has two lines which are almost identical. Copy-paste == more lines to change if a single line needs changing.&lt;br /&gt;
&lt;br /&gt;
===A smarter way===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var maxWidth = reduce(function (a, b) {&lt;br /&gt;
  return a &amp;gt; b ? a : b;&lt;br /&gt;
}, 0, map(GetSystemFont().getStringWidth, stats));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Basically, the stats have been ''mapped'' from strings to their widths according to the system font, and those widths have been ''reduced'' to find the maximum of them.&lt;br /&gt;
&lt;br /&gt;
Note that we can use Sphere's API functions with our higher-order functions, just by omitting the &amp;quot;call&amp;quot; parentheses.&lt;br /&gt;
&lt;br /&gt;
Why it's good:&lt;br /&gt;
&lt;br /&gt;
*	''Less typing'' - Compare these to the chump's way. Do you really want to type all that? Less typing == less typos == less chance of errors.&lt;br /&gt;
*	''No unnecessary index'' - I told you we didn't need the index.&lt;br /&gt;
*	''No copy-paste'' - Copy-paste == bad. I shouldn't have to explain this.&lt;br /&gt;
&lt;br /&gt;
===An even smarter way===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var maxWidth = Math.max.apply(null, map(GetSystemFont().getStringWidth, stats));&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The kind folk behind Sphere's JavaScript engine gave us the standard JavaScript library. It doesn't include much, but it's got [https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/max Math.max], which we can link with our own higher-order functions to do lots of heavy lifting.&lt;br /&gt;
&lt;br /&gt;
Same benefits as the smarter way, plus it's shorter, and now the intent is very, very clear. This is what we're aiming for.&lt;/div&gt;</summary>
		<author><name>Apollolux</name></author>	</entry>

	</feed>