Re: [xsl] XSLT repetition constructs

Subject: Re: [xsl] XSLT repetition constructs
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 7 Mar 2019 15:18:30 -0000
Yes, in Haskel there is a special function for this -- scanl

And this was implemented 15 years ago in FXSL:

Here is the code for f:scanl:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:f="http://fxsl.sf.net/";
exclude-result-prefixes="f"
>
    <xsl:import href="func-apply.xsl"/>

    <xsl:function name="f:scanl">
      <xsl:param name="pFunc" as="element()"/>
      <xsl:param name="pA0"/>
      <xsl:param name="pList" as="item()*"/>

      <xsl:sequence select=
             "$pA0,
             (if (exists($pList))
               then
                 f:scanl($pFunc,
                         f:apply($pFunc, $pA0, $pList[1]),
                         $pList[position() > 1]
                         )
               else ()
             )"
      />
    </xsl:function>

  <xsl:function name="f:scanl1">
    <xsl:param name="pFun" as="element()"/>
    <xsl:param name="pList" as="item()+"/>

    <xsl:sequence select="f:scanl($pFun, $pList[1], $pList[position() > 1])"/>
  </xsl:function>
</xsl:stylesheet>


And a test:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:f="http://fxsl.sf.net/";
  exclude-result-prefixes="f"
>
  <xsl:import href="../f/func-scanl.xsl"/>
  <xsl:import href="../f/func-Operators.xsl"/>

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:value-of separator=", " select="f:scanl(f:add(), 0, /*/*)"/>
     <xsl:text>&#xA;- - - - - - - - - - -&#xA;</xsl:text>
     <xsl:value-of separator=", "  select="f:scanl1(f:add(), /*/*)"/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on this XML document:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

the wanted result is produced:

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55
- - - - - - - - - - -
01, 3, 6, 10, 15, 21, 28, 36, 45, 55

As an exercise one can code this in XPath 3.0


Cheers,
Dimitre

On Thu, Mar 7, 2019 at 5:27 AM Martin Honnen martin.honnen@xxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Am 07.03.2019 um 12:55 schrieb Michael Kay mike@xxxxxxxxxxxx:
> > A good simple use case for fold-left() is to accumulate a running total, i.e. turn (1,2,3,4) into (1,3,6,10).
> >
>
> The example to simply compute the running total (e.g. map (1,2,3,4) to
> 10) is in the spec with
>
>     fold-left((1 to 4), 0, function($a, $b) { $a + $b})
>
>
> But to map the whole sequence (1,2,3,4) with fold-left to a new sequence
> of (1,3,6,10) I am already struggling to express that in a compact way, is
>
> fold-left(
>    (1 to 4),
>    (),
>    function ($a, $b) {  $a, if (empty($a)) then $b else $b + $a[last()] }
> )
>
> a good way? Or can the third argument, the function be expressed in a
> more compact way?
> 



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread