开发者

RunOnce in Foreach

I'm writing a little scripting language just for a bit of fun and the learning of the codes :P

I would just like your opinions/suggestions. I have an idea but I don't want to include something that people are going to w开发者_开发问答ant to spit on. I plan on making this language open source once, soon.

Does anybody think that it would be cool to have something like:

[Foreach] Uppercase Letter s
          in Case-Insensitive Word SallySawtheSeafiShandateit:

          Count++.

          s.Highlight: True.

          RunOnce.ProtectedMethod.ActivateProtectedMethod: IsTrue.

[Protected Method.LockTo: [Foreach]].IsTrue
          StatusBar.Message: Match for s was found. Total: Count..
          RunOnce.ProtectedMethod.Disable.

Explanation: What the above actually does is it searches through a string of text "SallySawtheSeafiShandateit" and highlights every single match. But when it finds the very first match for "s", it runs a method called "IsTrue", and sets the statusbar text to "match was found...". And then deactivates the RunOnce method so it may no longer be accessed, since there's no need for it to be run again.

This might not be the best example, but I think you get the idea. There have been plenty of times where I've needed to do something only once in a foreach loop, but couldn't, without writing a whole bunch of other code.

I figure, atleast this way, everything can be done in just two methods.

Please be brutally honest. :)

Thank you


This just seems like an over-complication of the following structure (in java style):

boolean ranOnce = false;
for (char c : string.toCharArray()) {
  if (c != 's') continue;
  if (!ranOnce) {
    // do stuff once
    ranOnce = true;
  }
  // do other stuff
}

It just seems like extreme over-engineering to me, when a single boolean and an if condition do the trick.


Hm. For this sort of situation I'd normally just use a flag variable and a conditional.

I'd reconsider "runOnce" -- it's a little ambiguous. Does it run the first iteration, the last iteration, somewhere in the middle? From what I can tell it looks like yours runs in the very first iteration, but then again what use would displaying the total count be in the first iteration? You'll know it's just "1".

For my money, I think I'd actually use two keywords that fired events/methods/etc at the first iteration and at the last iteration, respectively.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜