Discussion:
[dart-misc] Conditionally calling String methods
Jon Kleiser
2018-08-30 14:37:28 UTC
Permalink
I have two String variables txt and part, and I want to call either
txt.startsWith(part), txt.endsWith(part), or txt.contains(part), depending
on another variable cond. If the method being called returns true, I will
do just stdout.write("$txt "). I know I can achieve this by using a switch,
but since my variable cond remains unchanged through a loop, it would have
been more elegant and probably more efficient if I could put the correct
method into a variable test just before the loop, and then call something
like txt.test(part), or maybe test(txt, part), inside the loop.
Is there an elegant way to do this in Dart?
--
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/a3bc0bfa-9fd1-439b-9274-b19e23603935%40dartlang.org.
Randal L. Schwartz
2018-08-30 14:57:51 UTC
Permalink
Jon> Is there an elegant way to do this in Dart?

You could assign a closure to a variable once you've determined which
function to call, and then call the closure in your loop.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<***@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/Dart consulting, Technical writing, Comedy, etc. etc.
Still trying to think of something clever for the fourth line of this .sig
--
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/865zzr9a1c.fsf%40red.stonehenge.com.
tatumizer-v0.2
2018-08-30 16:32:28 UTC
Permalink
if your cond is an int (0, 1,2) then use
[txt.startsWith, txt.contains, txt.endsWith][cond](part)
--
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/aeabb595-47b2-46d7-933b-99d8fafe07e4%40dartlang.org.
Jon Kleiser
2018-08-30 19:30:24 UTC
Permalink
Great! This works fine:

void main() {
var txt = "xx99yy99";
var part = "99";
var txtMeth = [txt.startsWith, txt.contains, txt.endsWith];
[0, 1, 2].forEach((i) {
print("$i: ${txtMeth[i](part)}");
});
}
Post by tatumizer-v0.2
if your cond is an int (0, 1,2) then use
[txt.startsWith, txt.contains, txt.endsWith][cond](part)
--
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/45d56e76-27ba-415e-939f-34a28891e6b8%40dartlang.org.
'Brian Oney' via Dart Misc
2018-08-31 08:50:08 UTC
Permalink
There was a minor misunderstanding. tatumizer(?) meant to give a solution if there was only one test sought. The solution would be to called the desired method by its index in a predefined list of possible methods. The correct index would be the integer result (cond) of the some undefined test(s). The following would be less brittle if you want to call a bunch of methods.
void main() {  var txt = "xx99yy99";  var part = "99";  var txtMeth = [txt.startsWith, txt.contains, txt.endsWith];  txtMeth.forEach((i) {    print("${i(part)}");  });}
This has little to do with conditionally calling string methods. What is it that you want to do?
void main() {  var txt = "xx99yy99";  var part = "99";  var txtMeth = [txt.startsWith, txt.contains, txt.endsWith];  [0, 1, 2].forEach((i) {    print("$i: ${txtMeth[i](part)}");  });}
if your cond is an int (0, 1,2) then use[txt.startsWith, txt.contains, txt.endsWith][cond](part)
--
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 view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/45d56e76-27ba-415e-939f-34a28891e6b8%40dartlang.org.
--
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/1535705408.1756.8.camel%40gmail.com.
Jon Kleiser
2018-09-01 07:28:05 UTC
Permalink
I ended up using Randal's closure idea. Below is my complete solution. The
constructor WordsMatching(...) is the initial setup, while handleWord(...)
is called repeatedly in the following scanning process.

class WordsMatching extends ScanTask {
Function matchRes;
final String argument;

WordsMatching(String methodCode, this.argument) {
switch (methodCode[0].toUpperCase()) {
case "S":
matchRes = (word, arg, ln) => word.startsWith(arg) ? "$word " :
null;
break;
case "C":
matchRes = (word, arg, ln) => word.contains(arg) ? "$word " : null;
break;
case "E":
matchRes = (word, arg, ln) => word.endsWith(arg) ? "$word " : null;
break;
case "L":
matchRes = (word, arg, ln) => word.length > int.parse(arg) ? "$ln:
$word\n" : null;
break;
default:
matchRes = null;
}
}

bool handleWord(List<String> lineParts, int lineNo) {
String word = lineParts.first;
if (matchRes != null) {
String res = matchRes(word, argument, lineNo);
if (res != null) stdout.write(res);
return SCAN_MORE;
}
stdout.write("Unknown method code");
return QUIT_SCAN;
}
}
Post by 'Brian Oney' via Dart Misc
There was a minor misunderstanding. tatumizer(?) meant to give a solution
if there was only one test sought. The solution would be to called the
desired method by its index in a predefined list of possible methods. The
correct index would be the integer result (cond) of the some undefined
test(s). The following would be less brittle if you want to call a bunch of
methods.
void main() {
var txt = "xx99yy99";
var part = "99";
var txtMeth = [txt.startsWith, txt.contains, txt.endsWith];
txtMeth.forEach((i) {
print("${i(part)}");
});
}
This has little to do with conditionally calling string methods. What is
it that you want to do?
void main() {
var txt = "xx99yy99";
var part = "99";
var txtMeth = [txt.startsWith, txt.contains, txt.endsWith];
[0, 1, 2].forEach((i) {
print("$i: ${txtMeth[i](part)}");
});
}
if your cond is an int (0, 1,2) then use
[txt.startsWith, txt.contains, txt.endsWith][cond](part)
--
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/45d56e76-27ba-415e-939f-34a28891e6b8%40dartlang.org
<https://groups.google.com/a/dartlang.org/d/msgid/misc/45d56e76-27ba-415e-939f-34a28891e6b8%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 email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/ce169afc-15a7-4aec-90ad-8c563a61dd72%40dartlang.org.
tatumizer-v0.2
2018-09-01 12:13:53 UTC
Permalink
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 email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/aa592cfa-0700-4f12-9361-b39974172995%40dartlang.org.
Jon Kleiser
2018-09-02 08:44:18 UTC
Permalink
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.
Post by tatumizer-v0.2
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 email to misc+***@dartlang.org.
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.
'Brian Oney' via Dart Misc
2018-09-02 09:43:07 UTC
Permalink
Hi 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. Please consider: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.
RegardsBrian
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 view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/935da3d2-d103-471f-9c83-10ff05800a6e%40dartlang.org.
--
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/1535881387.1674.2.camel%40gmail.com.
'Lasse R.H. Nielsen' via Dart Misc
2018-09-02 10:43:18 UTC
Permalink
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 Misc
Hi 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.
Jon Kleiser
2018-09-02 19:50:19 UTC
Permalink
Thanks a lot, all of you, for all the good suggestions. I can also mention
that I recently got very good advice in a question re. RegExp use over at
https://github.com/dart-lang/sdk/issues/34306. It helps a lot having places
like these to get input.

/Jon

sÞndag 2. september 2018 12.43.35 UTC+2 skrev Lasse Reichstein Holst
Post by 'Lasse R.H. Nielsen' via Dart Misc
From the code I've seen so far, this looks like a problem that can be
handled quite efficiently by specialized strategy objects.
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 Misc
Hi 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>
.
--
'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/382e49ab-6e0a-4a19-b4ee-46af28fc48c3%40dartlang.org.
Ahmet A. Akın
2018-09-03 06:45:31 UTC
Permalink
Hello,
Do you intent to get vector values by moving in the .vec text file? Perhaps
it is better to convert text vector file to a binary form (with 32 bit
float values) and search values from there.
Post by Jon Kleiser
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.
Post by tatumizer-v0.2
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 email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/b7badade-3d33-4b41-8032-613e996616c6%40dartlang.org.
Jon Kleiser
2018-09-03 08:30:32 UTC
Permalink
Yes, I'm reading vector values from .vec text files. Is writing and
reading 32-bit float values to/from files trivial in Dart? I may not change
my strategy from reading text files very soon, but it could be interesting
to know a bit about handling binary files from Dart.
Post by Ahmet A. Akın
Hello,
Do you intent to get vector values by moving in the .vec text file?
Perhaps it is better to convert text vector file to a binary form (with 32
bit float values) and search values from there.
Post by Jon Kleiser
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.
Post by tatumizer-v0.2
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 email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/e47b0cb7-44c2-462d-baea-e61d6ab72179%40dartlang.org.
Ahmet A. Akın
2018-09-03 10:51:34 UTC
Permalink
I would strongly suggest not dealing with text vector files. When
vocabulary and vector size is large reading values from word vector text
files become extremely slow.
As an anecdote, a friend of mine was using word vectors in a Java NER
project (Vocabulary size = 100_000 and dimension = 200 ) and they were
loading whole thing during initialization. It took about 1 to 2 minutes
loading vectors to memory as float arrays (parsing text to float numbers is
slow). I am quite familiar with fastText and word vectors in general (I
wrote a Java port of fastText). So we made an engineering attempt and
convert the vector file to 2 files, one vocabulary other is for vector
values. After some IO optimization, loading time reduced to 2-3 seconds
(whole thing). It could be faster but Java also has limitations.

For even faster loading and more compact memory usage, one idea is using
quantization. You can apply linear quantization (or binning, k-means etc,
but linear is straight forward) to vector values and save them as bytes
instead of 32 bit floats. This way loading can be much faster. But this
requires some experimentation as quantization causes precision loss.

If you want to deal with this kind of data in Dart, you probably will need
to get familiar with "typed data" classes. (
https://api.dartlang.org/stable/2.0.0/dart-typed_data/dart-typed_data-library.html
) Also probably RandomAccessFiles and readBytesSync etc methods. Sadly I
did not write Dart code for a while.

Anyway, your case may be very different so please go ahead and try your
strategy first.
Post by Jon Kleiser
Yes, I'm reading vector values from .vec text files. Is writing and
reading 32-bit float values to/from files trivial in Dart? I may not change
my strategy from reading text files very soon, but it could be interesting
to know a bit about handling binary files from Dart.
Post by Ahmet A. Akın
Hello,
Do you intent to get vector values by moving in the .vec text file?
Perhaps it is better to convert text vector file to a binary form (with 32
bit float values) and search values from there.
Post by Jon Kleiser
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.
Post by tatumizer-v0.2
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 email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/4d31ba85-8710-4f3f-aaea-4509fa67d0de%40dartlang.org.
tatumizer-v0.2
2018-09-04 15:11:00 UTC
Permalink
I benchmarked all low-level operations in dart a while ago, just found my
old archives. Conversion from text to double proceeds at a lively rate of
200MB/sec, which is not bad.
In java, it's even faster, so maybe you used some wrong API in java?

It's much worse in reverse: converting double to String is almost 7 times
slower.
--
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/d7a23946-d78a-4603-b113-729102070cc5%40dartlang.org.
Continue reading on narkive:
Loading...