[ExM Users] 'break' statement?
Timothy Stitt (TGAC)
Timothy.Stitt at tgac.ac.uk
Tue Mar 17 14:03:23 CDT 2015
Thanks Tim. That makes sense. I don't need it now but your example will be useful if this becomes a bigger issue later.
---
Timothy Stitt PhD / Head of Scientific Computing
The Genome Analysis Centre (TGAC)
http://www.tgac.ac.uk/
p: +44 1603 450378
e: timothy.stitt at tgac.ac.uk<mailto:timothy.stitt at tgac.ac.uk>
From: Tim Armstrong <tim.g.armstrong at gmail.com<mailto:tim.g.armstrong at gmail.com>>
Date: Tuesday, 17 March 2015 16:58
To: Timothy Stitt <timothy.stitt at tgac.ac.uk<mailto:timothy.stitt at tgac.ac.uk>>
Cc: "exm-user at lists.mcs.anl.gov<mailto:exm-user at lists.mcs.anl.gov>" <exm-user at lists.mcs.anl.gov<mailto:exm-user at lists.mcs.anl.gov>>
Subject: Re: [ExM Users] 'break' statement?
There isn't a break statement - it doesn't really fit into the language design since multiple iterations of a loop can be running at the same time.
With a foreach loop, you can't prevent iterations from running - they're all independent so there's no concept of a "next" iteration.
With the for loop, you can achieve roughly what you're talking about using the loop condition.
E.g. a contrived example where we're searching for the first odd number in an array is:
/*
* Searching for odd numbers
*/
int A[] = [2, 4, 6, 8, 9, 11];
for (int i = 0, boolean found = false; i < size(A) && !found; i = i + 1, found = is_odd) {
boolean is_odd = A[i] %% 2 == 1;
if (is_odd) {
trace(A[i]);
}
}
This probably looks a little wonky, but with Swift sometimes you have to approach things a little differently to make them work.
I'd be a little careful trying to do some of these optimisations - there are some situations where things that work great in other languages can backfire. The trade-off here is that if you just do a regular foreach it can start everything running entirely in parallel, rather than starting things up one at a time. If you're not sure about something or want some ideas for how to speed up code, I'm more than happy to help.
- Tim
On 17 March 2015 at 11:32, Timothy Stitt (TGAC) <Timothy.Stitt at tgac.ac.uk<mailto:Timothy.Stitt at tgac.ac.uk>> wrote:
Hi all,
I was just wondering if there is 'break' statement in Swift 'for' and possibly 'foreach' constructs?
I have a potential use case that would benefit from this although it probably doesn't take too much overhead right now to go through the entire array looking for a particular item rather than breaking when the item is found.
Just wondered in case I missed it in the documentation.
Tim.
---
Timothy Stitt PhD / Head of Scientific Computing
The Genome Analysis Centre (TGAC)
http://www.tgac.ac.uk/
p: +44 1603 450378<tel:%2B44%201603%20450378>
e: timothy.stitt at tgac.ac.uk<mailto:timothy.stitt at tgac.ac.uk>
_______________________________________________
ExM-user mailing list
ExM-user at lists.mcs.anl.gov<mailto:ExM-user at lists.mcs.anl.gov>
https://lists.mcs.anl.gov/mailman/listinfo/exm-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/exm-user/attachments/20150317/27fa7ea3/attachment.html>
More information about the ExM-user
mailing list