From the code I've seen so far, this looks like a problem that can be
handled quite efficiently by specialized strategy objects.
Example:
class WordsMatching extends ScanTask {
factory WordsMatching(String methodCode, String argument) {
switch (methodCode.codeUnitAt(0)) {
case 0x53: /* S */
case 0x73: /* s */
return WordsMatchingStart(argument);
case 0x43: /* C */
case 0x63: /* c */
return WordsMatchingContains(argument);
case 0x45: /* E */
case 0x65: /* e */
return WordsMatchingEnd(argument);
case 0x4c: /* L */
case 0x6c: /* l */
return WordsMatchingLength(argument);
}
return const WordsMatchingNone();
}
bool handleWord(List<String> lineParts, int lineNo);
}
class WordsMatchingStart implements WordsMatching {
final String argument;
WordsMatchingStart(this.argument);
bool handleWord(List<String> lineParts, int lineNo) {
var word = lineParts.first;
if (word.startsWith(argument)) {
stdout..write(word)..write(" ");
}
return true;
}
}
class WordsMatchingContains implements WordsMatching {
final String argument;
WordsMatchingContains(this.argument);
bool handleWord(List<String> lineParts, int lineNo) {
var word = lineParts.first;
if (word.contains(argument)) {
stdout..write(word)..write(" ");
}
return true;
}
}
class WordsMatchingEnd implements WordsMatching {
final String argument;
WordsMatchingEnd(this.argument);
bool handleWord(List<String> lineParts, int lineNo) {
var word = lineParts.first;
if (word.endsWith(argument)) {
stdout..write(word)..write(" ");
}
return true;
}
}
class WordsMatchingLength implements WordsMatching {
final int length;
final String lengthString;
WordsMatchingLength(this.lengthString) : length = int.parse(lengthString);
bool handleWord(List<String> lineParts, int lineNo) {
var word = lineParts.first;
if (word.length > length) {
stdout..write(lineNo)..write(": ")..writeln(word);
}
return true;
}
}
class WordsMatchingNone implements WordsMatching {
const WordsMatchingNone();
bool handleWord(List<String> lineParts, int lineNo) {
stdout.writeln("Unknown method code");
return false;
}
}
(I don't know what ScanTask is, so maybe I"m just missing the point :)
On Sun, Sep 2, 2018 at 11:43 AM 'Brian Oney' via Dart Misc <
Post by 'Brian Oney' via Dart MiscHi Jon,
that makes your intent more clear. You may be more successful in getting
help to solve your problem, if you start with the problem and make a small
reproducible example. With such a terse and powerful language there are
guaranteed to be several more or less elegant solutions. The 'effective
dart' serves as a compass, when there are a few solutions on the table
https://www.dartlang.org/guides/language/effective-dart. I very much
appreciate it.
For example, you use a class with a switch where a function and a map may
do.
https://www.dartlang.org/guides/language/effective-dart/design#classes
Again, I am learning Dart and trying to learn smart ways of
implementation, and I don't have enough information from you to further
help. I thought I'd share this gem of logic. HTH.
Regards
Brian
Hi. The reason why I pass the whole list of lineParts, is that I have
several subclasses of ScanTask, and most of them need access to the entire
lineParts in their handleWord implementation.
The lines of the file being scanned each contain a word followed by
(typically 300) floating point values, all separated by spaces. It's about
"word vectors", see https://fasttext.cc. I've found Dart to be a very
efficient language for dealing with those big (.vec) model files.
I shall consider your "map". Thanks.
Instead of switch, you can use map;
final jmp = {
"S" : (word, arg, ln) => word.startsWith(arg) ? "$word " : null,
"C": (word, arg, ln) => word.contains(arg) ? "$word " : null,
/// etc
};
Overall, not clear what your program is trying to achieve, but it looks a
bit strange - e.g. if handleWords always takes the lineParts.first, then
why do you pass the whole list of lineParts?
Anyway... In dart, switch statement is a bit awkward, and in many cases
(like yours) I'd prefer using jump map instead - but it's a matter of taste.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/935da3d2-d103-471f-9c83-10ff05800a6e%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/935da3d2-d103-471f-9c83-10ff05800a6e%40dartlang.org?utm_medium=email&utm_source=footer>
.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/a/dartlang.org/d/msgid/misc/1535881387.1674.2.camel%40gmail.com
<https://groups.google.com/a/dartlang.org/d/msgid/misc/1535881387.1674.2.camel%40gmail.com?utm_medium=email&utm_source=footer>
.
--
Lasse R.H. Nielsen - ***@google.com
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 KÞbenhavn K
- Denmark - CVR nr. 28 86 69 84
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CA%2BeWuVBgGw32Uh2FZUvtHiJ7NsPqVc%3DYMeTTSUTrw60hdLsuJA%40mail.gmail.com.