Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Using the XSLT sum function

This XML and XSLT code is an example of storing and displaying a car mileage log. Notably the XSLT sum() function displays the total no of miles.


file: car_log.xsl
<?xml version="1.0"?>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      
    <xsl:template match="car_log">
      <div>      
        <p>Total miles this month: <xsl:value-of select="sum(records/entry/miles)" /></p>
        <xsl:apply-templates select="records"/>
      </div>
    </xsl:template>
    
    <xsl:template match="car_log/records/entry"><xsl:variable name="pos" select="position()"/>
    <div class="record">
      <dl>
        <dt><label for="trip"><xsl:value-of select="@title"/>trip</label></dt>
        <dd>
          <input type="text" id="trip{$pos}" name="trip{$pos}" value="{trip}" size="{@size}" />
        </dd>
          
        <dt><label for="miles"><xsl:value-of select="@title"/>miles</label></dt>
        <dd>
          <input type="text" id="miles{$pos}" name="miles{$pos}" value="{miles}" size="2" />
        </dd>
          
        <dt><label for="description"><xsl:value-of select="@title"/>description</label></dt>
        <dd>
          <input type="text" id="description{$pos}" name="description{$pos}" value="{description}"  />
        </dd>        
      </dl>
    </div>
    </xsl:template>
    </xsl:stylesheet>

file: car_log.xml
<car_log>
  <summary>
    <project>car_log</project>
  </summary>
  <records>
    <entry id='17962'><date>Wed Dec 12 20:00:33 +0000 2007</date><trip>trip to the supermarket</trip><miles>11.8</miles><description>my first trip with the GPS installed</description></entry>
    <entry id='18024'><date>Fri Dec 14 11:45:09 +0000 2007</date><trip>driving around the park</trip><miles>26.1</miles><description/></entry>
    <entry id='18173'><date>Thu Dec 20 01:21:27 +0000 2007</date><trip>trip to the supermarket</trip><miles> 8.5</miles><description/></entry>
    <entry id='18174'><date>Thu Dec 20 01:34:26 +0000 2007</date><trip>visiting parents</trip><miles>12.7</miles><description/>
    </entry>
  </records>
</car_log>

output
Total miles this month: 59.1
...

Weighted Mean

Weighted mean ( http://en.wikipedia.org/wiki/Mean#Weighted_arithmetic_mean )

class Array
  #sum (and mean) found on http://snippets.dzone.com/posts/show/2161
  def sum
    inject( nil ) { |sum,x| sum ? sum + x.to_f : x.to_f }
  end
  
  def mean
    sum.to_f / size
  end
  
  #http://en.wikipedia.org/wiki/Mean#Weighted_arithmetic_mean
  def weighted_mean(weights_array)
    raise "Each element of the array must have an accompanying weight.  Array length = #{self.size} versus Weights length = #{weights_array.size}" if weights_array.size != self.size
    w_sum = weights_array.sum
    w_prod = 0
    self.each_index {|i| w_prod += self[i] * weights_array[i].to_f}
    w_prod.to_f / w_sum.to_f
  end
end


Example
>> a = [1,2,3]
=> [1, 2, 3]
>> w = [1,1,1]
=> [1, 1, 1]
>> a.weighted_arithmetic_mean(w)
=> 2.0
>> a.mean
=> 2.0
>> w = [5,2,1]
=> [5, 2, 1]
>> a.weighted_arithmetic_mean(w)
=> 1.5

days-up-to-month - returns the number of days in a year, preceding the given month.

days-in-months: [31 28 31 30 31 30 31 31 30 31 30 31]
days-in-months-leap: head change at copy days-in-months 2 29

days-up-to-month: func [month /in year] [
    sum copy/part either leap-year?/with any [year now] [days-in-months-leap] [days-in-months] month - 1
]

Generic 'sum' and 'mean' methods for Ruby arrays

class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; end


It's class agnostic, so you can do this:

[1,2,3].sum              # => 6
['a','b','c'].sum        # => 'abc'
[['a'], ['b','c']].sum   # => ['a', 'b', 'c']


You can then add a 'mean' operator easily:

class Array; def mean; sum / size; end; end


mean only works for numbers though, of course, like so:

[1,2,1000].mean    # => 334


That said, if your class implements division, it'll also work!
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS