Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #39642
| Path | csiph.com!news.swapon.de!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Anton Shepelev <anton.txt@gmail.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: input/output parameters |
| Date | Sun, 14 Mar 2021 22:17:23 +0300 |
| Organization | A noiseless patient Spider |
| Lines | 110 |
| Message-ID | <20210314221723.d4bccc22ee2647d1b474151a@gmail.com> (permalink) |
| References | <20210313153232.40fa0c8b0adf07d7e807084d@gmail.com> <slrns4plpr.4fd.jon+usenet@raven.unequivocal.eu> <20210314185124.0bcbab76d172caf076ac3839@gmail.com> <slrns4sdlm.4fd.jon+usenet@raven.unequivocal.eu> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| Injection-Info | reader02.eternal-september.org; posting-host="1d29bea40a1fa08e460c8d6f4bac9272"; logging-data="27601"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1838r6KBzqEHsGai4gbSfpKHnHwoLf8EVk=" |
| Cancel-Lock | sha1:SqmBBivQKK6cGIW9cYope+I4nE0= |
| X-Newsreader | Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) |
| Xref | csiph.com comp.lang.javascript:39642 |
Show key headers only | View raw
Jon Ribbens:
> parseInt() - returns `NaN` on failure
I know and use it in my first attempt to get something like
out parameters:
function tryparse_int_1(s, cb)
{ var res = parseInt(s);
cb(res);
return !isNaN( res );
}
function test_1(s)
{ var /* inreger */ i;
if( tryparse_int_1( s, x=>i=x ) )
console.log(`String "${s}" is parsed as integer ${i}.`); else
console.log(`String "${s}" does not start with an integer.`);
}
test_1("47");
test_1("forty-seven");
> Map.get() - returns `undefined` if the key isn't in the
> mapping
I see -- JS takes avail of its dymamic typing by returning
`undefined' for both mutable and immutable values, but that
approach requires two lines instead of one, i.e.:
elem = map.get( key );
if( elem == undefined )
{ /*handle error*/ }
unless I use a feature of C-like languages that an
assignment is also an expression with a value:
var /*integer*/ int;
var /*boolean*/ ok;
if( !isNaN(int = parseInt("5")) )
console.log("ok: " + int); else
console.log("not at int.");
> > while( read_next( stream, out element )
> > { // process the element
> > }
> >
> > What alternative(s) does JavaScript propose?
>
> It would depend on the details of what you're doing. A lot
> of stream handling stuff in JavaScript was added before
> Promises, so you can't do the above and instead you'd be
> adding callbacks or event handlers.
I did not mean streams in particular but the programming of
a while loop that reads elements from a source until it is
exhausted. It may be rows from a recordset or tokens from a
string. I think I can do it this way:
while( ( element = read_next( stream ) != undefined )
{ // process the element
}
// or:
while( true )
{ element = read_next( stream )
if( element == undefined ) break;
// process the element
}
But it is a choice between a bit clumsy and a bit wordy.
> Throw an exception on error then.
I generally dislike exceptions and try to use normal control-
flow instead. My rule for exceptions is identical to
Joel's: catch them as early as possible, and never thow them
yourself:
"Exceptions", by Joel Spolsky
https://www.joelonsoftware.com/2003/10/13/13/
Ok, I do throw exceptions by way of asserts, if my program
can afford crashing at all.
> Or 'return data' from the function and use destructuring:
> [...]
> const { multiplied, divided } = multiplyAndDivide(2, 4)
> console.log(multiplied, divided)
Thanks for another alternative, I will certainly consider
it, although I find it somewhat aesthetically displeasing to
have more than one token on the left-hand side of an
assignment, but it may be better than passing several
anonymous setters or using eval:
[ok, err_text, result] = get_temp_file();
This is shorter and cleaner than any of the alternatives I
know of.
> Or if you really, *really* want output arguments then, er,
> don't use JavaScript and use a different language that you
> like better instead.
Sometimes it is not an option. I am learning to live with
JavaScript.
--
() ascii ribbon campaign -- against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-13 15:32 +0300
Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-13 15:16 +0000
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-14 18:51 +0300
Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 16:15 +0000
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-14 18:31 +0000
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-14 22:27 +0300
Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 23:44 +0000
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:41 -0700
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-14 22:17 +0300
Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 23:28 +0000
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:39 -0700
Re: input/output parameters Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-03-14 22:32 +0000
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:36 -0700
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-20 14:49 +0300
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 11:59 -0700
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 13:07 -0700
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-20 19:29 +0000
Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-20 20:35 +0000
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-15 00:27 +0300
Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-14 23:51 +0000
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-20 14:56 +0300
Re: input/output parameters Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-03-20 15:08 +0000
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 15:16 +0300
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-21 08:46 -0700
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-04-10 14:54 +0300
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 12:05 -0700
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 15:58 +0300
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-15 06:43 -0700
Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-15 18:01 +0100
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-16 18:06 +0000
Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-16 23:58 +0100
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-17 19:14 +0000
Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-17 23:46 +0100
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-18 18:04 +0000
Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-22 00:39 +0100
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-23 10:44 +0000
Re: input/output parameters Silvio <silvio@internet.com> - 2021-03-30 00:33 +0200
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-31 19:53 +0100
Re: input/output parameters Silvio <silvio@internet.com> - 2021-04-01 01:17 +0200
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-01 18:02 +0100
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-20 15:50 +0300
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-20 18:52 +0000
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 17:35 +0300
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-21 08:50 -0700
Re: input/output parameters Anton Shepelev <anton.txt@gmail.com> - 2021-03-21 19:31 +0300
Re: input/output parameters John G Harris <niam@jghnorth.org.uk.invalid> - 2021-03-21 19:27 +0000
Re: input/output parameters "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-03-20 13:02 -0700
Re: input/output parameters Postiljon Petskin <yyyyyyyyyyyywwwww@zohomail.eu> - 2023-04-22 08:47 -0700
csiph-web