Exit a for-loop?
Monday, 27 February 2017 · 3 messages
J
Joseph27 Feb 2017 19:12 #
Hi,
I have two sets of numbers, and I would like to write a for-loop for comparison, so that I get only the first match between the numbers. I was thinking of a double for-loop, but I do not know how to exit the comparison as soon as I get the first match. Any idea? Thanks.
Joseph
DK
Dirk Kirsten27 Feb 2017 19:25 #
Hello Joseph,
to be honest, this doesn't sound particularly like functional programming. The nice thing about functional programming is that you shouldn't care about such optimization stuff (like breaking out of a for loop), but instead our optimizer should be clever enough to stop executing the function once he has enough results. So simply request the first result and you should be fine. Something like this:
(for $a at $pos in $seq1
where $a = $seq2[$pos]
return $a)[1]Cheers
Dirk
Quoted text · Joseph · 9 lines
On 02/27/2017 09:42 PM, Joseph wrote: > Hi, > > I have two sets of numbers, and I would like to write a for-loop for > comparison, so that I get only the first match between the numbers. I > was thinking of a double for-loop, but I do not know how to exit the > comparison as soon as I get the first match. Any idea? Thanks. > > Joseph
--
Dirk Kirsten, BaseX GmbH, http://basexgmbh.de
|-- Firmensitz: Blarerstrasse 56, 78462 Konstanz
|-- Registergericht Freiburg, HRB: 708285, Geschäftsführer:
| Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 91 68 276, Fax: 0049 7531 20 05 22
MG
Marc van Grootel27 Feb 2017 20:25 #
Hi Joseph,
Maybe hof:until [1] and hof:take-while [2]?
[1] http://docs.basex.org/wiki/Higher-Order_Functions_Module#hof:until [2] http://docs.basex.org/wiki/Higher-Order_Functions_Module#hof:take-while
--Marc
Quoted text · Dirk Kirsten · 38 lines
> On 27 feb. 2017, at 19:25, Dirk Kirsten <dk at basex.org> wrote: > > Hello Joseph, > > to be honest, this doesn't sound particularly like functional > programming. The nice thing about functional programming is that you > shouldn't care about such optimization stuff (like breaking out of a for > loop), but instead our optimizer should be clever enough to stop > executing the function once he has enough results. So simply request the > first result and you should be fine. Something like this: > > > (for $a at $pos in $seq1 > where $a = $seq2[$pos] > return $a)[1] > > Cheers > > Dirk > > >> On 02/27/2017 09:42 PM, Joseph wrote: >> Hi, >> >> I have two sets of numbers, and I would like to write a for-loop for >> comparison, so that I get only the first match between the numbers. I >> was thinking of a double for-loop, but I do not know how to exit the >> comparison as soon as I get the first match. Any idea? Thanks. >> >> Joseph > > -- > Dirk Kirsten, BaseX GmbH, http://basexgmbh.de > |-- Firmensitz: Blarerstrasse 56, 78462 Konstanz > |-- Registergericht Freiburg, HRB: 708285, Geschäftsführer: > | Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle > `-- Phone: 0049 7531 91 68 276, Fax: 0049 7531 20 05 22 >
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery