core: vector/minmax/_minreduce_: New template

* test/core/suite.xml: Import `test/core/vector/minmax'.
* test/core/vector/minmax.xml: New package.
* vector/minmax.xml (_minreduce_, _minreduce): New template, function.
master
Mike Gerwitz 2019-02-13 14:38:08 -05:00
parent bd73ea0121
commit 46b7c234dd
3 changed files with 168 additions and 3 deletions

View File

@ -33,6 +33,7 @@
<import package="vector/fold" />
<import package="vector/interpolate" />
<import package="vector/length" />
<import package="vector/minmax" />
<import package="vector/table" />
<import package="aggregate" />

View File

@ -0,0 +1,72 @@
<?xml version="1.0"?>
<!--
Copyright (C) 2014-2019 Ryan Specialty Group, LLC.
This file is part of tame-core.
tame-core is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
xmlns:c="http://www.lovullo.com/calc"
xmlns:t="http://www.lovullo.com/rater/apply-template"
desc="Testing minimum and maximum values of vectors">
<import package="../../spec" />
<import package="../../../base" />
<import package="../../../vector/minmax" />
<t:describe name="_minreduce_">
<t:it desc="reduces empty vector to 0">
<t:given>
<t:minreduce>
</t:minreduce>
</t:given>
<t:expect>
<t:match-result eq="0" />
</t:expect>
</t:it>
<t:it desc="reduces one-element vector to its only element">
<t:given>
<t:minreduce>
<c:value-of name="#3" />
</t:minreduce>
</t:given>
<t:expect>
<t:match-result eq="3" />
</t:expect>
</t:it>
<t:it desc="reduces n-element vector to its minimum">
<t:given>
<t:minreduce>
<c:value-of name="#7" />
<c:value-of name="#3" />
<c:value-of name="#1" />
<c:value-of name="#6" />
</t:minreduce>
</t:given>
<t:expect>
<t:match-result eq="1" />
</t:expect>
</t:it>
</t:describe>
</package>

View File

@ -24,14 +24,104 @@
title="Maximum and Minimum Elements">
<import package="../base" />
<import package="../when" />
<import package="../numeric/common" />
<import package="../numeric/minmax" />
<section title="Vector Reduction">
Core currently only offers a~maximum reduction on
a~vector. \todo{Add a~minimum reduction.} \ref{_maxreduce_}
provides a convenient template-based abstraction.
Two types of reductions are provided for minimum and maximum respectively:
\ref{_minreduce_} and \ref{_maxreduce_}.\footnote{
This is because TAME does not have first-class functions.}
They both produce scalar values of the minimums and maximums
(respectively) of vectors.
<template name="_minreduce_"
desc="Reduce a vector to its minimum">
<param name="@values@" desc="Values to reduce" />
<param name="@isvector@" desc="Set to 'true' if the nodes should
not be wrapped in c:vector" />
<param name="@label@" desc="Application label">
<!-- default empty -->
<text></text>
</param>
<c:apply name="_minreduce" label="@label@">
<c:arg name="vector">
<if name="@isvector@" eq="true">
<param-copy name="@values@" />
</if>
<unless name="@isvector@" eq="true">
<c:vector>
<param-copy name="@values@" />
</c:vector>
</unless>
</c:arg>
</c:apply>
</template>
<function name="_minreduce"
desc="Minimum value in a vector">
<param name="vector" type="float" set="vector"
desc="Vector to find minimum of" />
<c:let>
<c:values>
<c:value name="length" type="integer"
desc="Length of vector">
<c:length-of>
<c:value-of name="vector" />
</c:length-of>
</c:value>
</c:values>
<c:cases>
<c:case label="Empty vector">
<t:when-eq name="length" value="#0" />
<c:value-of name="#0" />
</c:case>
<c:case label="Single-element vector">
<t:when-eq name="length" value="#1" />
<c:value-of name="vector">
<c:index>
<c:value-of name="#0" />
</c:index>
</c:value-of>
</c:case>
<c:otherwise label="Non-empty vector">
<c:apply name="min">
<c:arg name="min1">
<c:value-of name="vector">
<c:index>
<c:value-of name="#0" />
</c:index>
</c:value-of>
</c:arg>
<c:arg name="min2">
<c:recurse>
<c:arg name="vector">
<c:cdr>
<c:value-of name="vector" />
</c:cdr>
</c:arg>
</c:recurse>
</c:arg>
</c:apply>
</c:otherwise>
</c:cases>
</c:let>
</function>
<template name="_maxreduce_"
desc="Reduce a set to its maximum">
@ -72,6 +162,8 @@
let~expressions and other convenience templates. It has since
been refactored slightly, but can be made to be more concise.}
<!-- TODO: rewrite this to be more concise, with the more lisp-like
recursive strategy of minreduce -->
<function name="maxreduce" desc="Reduce a set to its maximum">
<param name="maxreduce_set" type="float" set="vector"
desc="Set to find max of" />