Project

Profile

Help

Iteration on odd items in a sequence

Added by Anonymous about 16 years ago

Legacy ID: #4964668 Legacy Poster: Vladimir Nesterovsky (vnesterovsky)

I have a question regarding preferrable way to express iteration on odd items in a sequence. I have two alternatives: 1. for $i in 1 to count($items) idiv 2 return ... work with $items[$i * 2 - 1] and $items[$i * 2] 2. for $i in 1 to count($items) return if (($i mod 2) = 0) then () else ... work with $items[$i] and $items[$i + 1] Which alternative is preferrable in Saxon, if any? Thanks.


Replies (5)

Please register to reply

RE: Iteration on odd items in a sequence - Added by Anonymous about 16 years ago

Legacy ID: #4964687 Legacy Poster: Michael Kay (mhkay)

I can't see a significant reason for preferring one over the other. If small percentage differences in performance are important to you, you need to make some measurements. You don't say whether it's XSLT or XQuery. In XSLT you could also do <xsl:for-each select="$items[position() mod 2 = 1]"> ... and in XQuery you could also do for $item at $p in $items ... Michael Kay http://www.saxonica.com/

RE: Iteration on odd items in a sequence - Added by Anonymous about 16 years ago

Legacy ID: #4964689 Legacy Poster: David Lee (daldei)

I would do for $i in $items[(position() mod 2) = 0] return work with $i -David Lee

RE: Iteration on odd items in a sequence - Added by Anonymous about 16 years ago

Legacy ID: #4964708 Legacy Poster: Michael Kay (mhkay)

>I would do for $i in $items[(position() mod 2) = 0] return work with $i But that doesn't give you any way to find the item before/after $i. Of course another solution here is recursion: declare function f:process($seq as item()*) { if (exists($seq)) then process $seq[1] process $seq[2] f:process(subsequence($seq,3)) else () } Michael Kay http://www.saxonica.com/

RE: Iteration on odd items in a sequence - Added by Anonymous about 16 years ago

Legacy ID: #4964760 Legacy Poster: Vladimir Nesterovsky (vnesterovsky)

> You don't say whether it's XSLT or XQuery. In XSLT you could also do I intentionally tried to stay in common scope of XSLT and XQuery. I'm using xslt. In my task I need to iterate pairs of items. This means both odd and even items are required. I was thinking that (2) does not need to know sequence size in advance, in contrast to (1). Is this matter?

RE: Iteration on odd items in a sequence - Added by Anonymous about 16 years ago

Legacy ID: #4964768 Legacy Poster: Michael Kay (mhkay)

>I was thinking that (2) does not need to know sequence size in advance, in contrast to (1). Is this matter? That starts to be a question of memory vs time tradeoffs, which are very hard to evaluate in the abstract. If you care enough then I think you need to do some measurements.

    (1-5/5)

    Please register to reply