<?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-11-03T11:12:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.9</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&amp;#039;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&amp;#039;ll need to find out which line is longest.&lt;br /&gt;
&lt;br /&gt;
===The chump&amp;#039;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&amp;#039;s wrong with it?&lt;br /&gt;
&lt;br /&gt;
*	&amp;#039;&amp;#039;&amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop is crappy&amp;#039;&amp;#039; - We&amp;#039;re good coders, so we recognise that this looks for a maximum without even reading it. But if we really sat down and &amp;#039;&amp;#039;read&amp;#039;&amp;#039; it, we&amp;#039;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;
*	&amp;#039;&amp;#039;Focus is on iteration instead of task&amp;#039;&amp;#039; - 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&amp;#039;t even need it.&lt;br /&gt;
*	&amp;#039;&amp;#039;Copy-paste coding style&amp;#039;&amp;#039; - 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 &amp;#039;&amp;#039;mapped&amp;#039;&amp;#039; from strings to their widths according to the system font, and those widths have been &amp;#039;&amp;#039;reduced&amp;#039;&amp;#039; to find the maximum of them.&lt;br /&gt;
&lt;br /&gt;
Note that we can use Sphere&amp;#039;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&amp;#039;s good:&lt;br /&gt;
&lt;br /&gt;
*	&amp;#039;&amp;#039;Less typing&amp;#039;&amp;#039; - Compare these to the chump&amp;#039;s way. Do you really want to type all that? Less typing == less typos == less chance of errors.&lt;br /&gt;
*	&amp;#039;&amp;#039;No unnecessary index&amp;#039;&amp;#039; - I told you we didn&amp;#039;t need the index.&lt;br /&gt;
*	&amp;#039;&amp;#039;No copy-paste&amp;#039;&amp;#039; - Copy-paste == bad. I shouldn&amp;#039;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&amp;#039;s JavaScript engine gave us the standard JavaScript library. It doesn&amp;#039;t include much, but it&amp;#039;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&amp;#039;s shorter, and now the intent is very, very clear. This is what we&amp;#039;re aiming for.&lt;/div&gt;</summary>
		<author><name>Apollolux</name></author>
	</entry>
</feed>