<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GuShH&#039;s DevBlog &#187; average</title>
	<atom:link href="http://gushh.net/blog/?tag=average&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://gushh.net/blog</link>
	<description>This blog is about software, electronics engineering and game development.</description>
	<lastBuildDate>Tue, 24 Jan 2012 00:31:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Average template</title>
		<link>http://gushh.net/blog/average-template/</link>
		<comments>http://gushh.net/blog/average-template/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 12:16:52 +0000</pubDate>
		<dc:creator>GuShH</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PureBasic]]></category>
		<category><![CDATA[average]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[purebasic]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://gushh.net/blog/?p=161</guid>
		<description><![CDATA[A tiny template for averaging numbers. I needed this for a prototype and I didn&#8217;t want to &#8220;wing it&#8221; anymore. I think there should be a package with similar templates that people could download to enhance the language, I&#8217;m just saying&#8230; It takes little to come up with this kind of code and in the [...]]]></description>
			<content:encoded><![CDATA[<p>A tiny template for averaging numbers.<br />
I needed this for a prototype and I didn&#8217;t want to &#8220;wing it&#8221; anymore.</p>
<p>I think there should be a package with similar templates that people could download to enhance the language, I&#8217;m just saying&#8230; It takes little to come up with this kind of code and in the end it&#8217;s always useful to have it around.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Macro</span> Average_Register<span style="color: #000066;">&#40;</span> _type <span style="color: #000066;">&#41;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Structure</span> AVERAGE#_type
		sum._type
		average._type
		count.i
	<span style="color: #000066; font-weight: bold;">EndStructure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Create<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">Define</span>.AVERAGE#_type <span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> <span style="color: #0000ff;">AllocateMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #0000ff;">SizeOf</span><span style="color: #000066;">&#40;</span> AVERAGE#_type <span style="color: #000066;">&#41;</span> <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Destroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #0000ff;">FreeMemory</span><span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this <span style="color: #000066;">=</span> #Null
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Average#_type#_Calculate<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">If</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count <span style="color: #000066; font-weight: bold;">And</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum
			<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average <span style="color: #000066;">=</span> <span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum <span style="color: #000066;">/</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count <span style="color: #000066;">&#41;</span>
			<span style="color: #000066; font-weight: bold;">ProcedureReturn</span>	<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average
		<span style="color: #000066; font-weight: bold;">EndIf</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Reset<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0.0</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0.0</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count 	<span style="color: #000066;">=</span> <span style="color: #CC0000;">0</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Add<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type, sample.f <span style="color: #000066;">&#41;</span>
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum 	<span style="color: #000066;">+</span> sample
		<span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count 	<span style="color: #000066;">+</span> <span style="color: #CC0000;">1</span>
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>.i Average#_type#_Count<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>count
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Average#_type#_Sum<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>sum
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">Procedure</span>._type Average#_type#_Get<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span>this.AVERAGE#_type <span style="color: #000066;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">ProcedureReturn</span> <span style="color: #000066;">*</span>this<span style="color: #000066;">\</span>average
	<span style="color: #000066; font-weight: bold;">EndProcedure</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">EndMacro</span></pre></div></div>

<p>You may register it as any numerical type.<br />
Usage example:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;">Average_Register<span style="color: #000066;">&#40;</span> f <span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; register with floats.</span>
<span style="color: #000066; font-weight: bold;">Define</span>.AVERAGEf <span style="color: #000fff;">test</span>
&nbsp;
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">1.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">2.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">3.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">4.0</span> <span style="color: #000066;">&#41;</span>
Averagef_Add<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span>, <span style="color: #CC0000;">5.0</span> <span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Calculate<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Count<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Sum<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
&nbsp;
Averagef_Reset<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Calculate<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Count<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span>
<span style="color: #000066; font-weight: bold;">Debug</span> Averagef_Sum<span style="color: #000066;">&#40;</span> <span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span></pre></div></div>

<p>You can allocate an instance of the structure with the Create function.<br />
For example, if you registered the template as float, then you may create a dynamic &#8220;object&#8221; in this way:</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">Define</span>.AVERAGEf <span style="color: #000066;">*</span><span style="color: #000fff;">test</span> <span style="color: #000066;">=</span> Averagef_Create<span style="color: #000066;">&#40;</span><span style="color: #000066;">&#41;</span> <span style="color: #ff0000; font-style: italic;">; the *test variable holds an instance of the AVERAGEf structure</span></pre></div></div>

<p>Likewise, you should call the Destroy() function to get rid of it.</p>

<div class="wp_syntax"><div class="code"><pre class="purebasic" style="font-family:monospace;">Averagef_Destroy<span style="color: #000066;">&#40;</span> <span style="color: #000066;">*</span><span style="color: #000fff;">test</span> <span style="color: #000066;">&#41;</span></pre></div></div>

<p>Trivial but useful. I&#8217;m not particularly happy with the naming convention though. But, let&#8217;s not forget that this is not an attempt at emulating OOP at all; it&#8217;s just a way of giving flexibility to an encapsulated piece of code. For OOP there are proper languages and there is no point whatsoever in trying to emulate it.</p>
<p>Cheers</p>
<g:plusone href='http://gushh.net/blog/average-template/'></g:plusone>]]></content:encoded>
			<wfw:commentRss>http://gushh.net/blog/average-template/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: gushh.net @ 2012-02-09 07:13:00 by W3 Total Cache -->
