Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #30601
| From | andber93@web.de |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: An "if" without "if" |
| Date | 2016-06-02 02:16 +0200 |
| Organization | albasani.net |
| Message-ID | <nintrv$2uc$1@news.albasani.net> (permalink) |
| References | <if-20160602002158@ram.dialup.fu-berlin.de> |
Stefan Ram wrote:
> Could I use arrow functions to emulate an »if«?
>
> Here is one observation (only user input to the
> console is shown):
>
> a = undefined;
> ( ( x = console.log( "ok" )) => 0 )( a );
> a = 1;
> ( ( x = console.log( "ok" )) => 0 )( a );
>
> So I have written a function that will print »ok«
> /if and only if/ »a« is undefined.
That doesn't really rely a lot on arrow functions, but rather on the
distinction of arguments to use default values or not. And there are
many of such control flow branchings in the language. The simplest way
to emulate an if-statement would probably be
while (/* condition */) {
/* conditional statements */
break;
}
But since you are interested in functions, I'd recommend to use
something along the lines of
const when = t => f => c => ({true:f, false:f}[!!c]);
Now you can do
const log = when(()=>console.log("ok"))(()=>console.log("not ok");
log(1)() // ok
log(0)() // not ok
log(true)() // ok
log(false)() // not ok
log(…)() // and so on
You also might be interested in Church Booleans.
Kind regards,
Bergi
Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread
Re: An "if" without "if" andber93@web.de - 2016-06-02 02:16 +0200
Re: An "if" without "if" Stefan Weiss <krewecherl@gmail.com> - 2016-06-02 12:37 +0200
Re: An "if" without "if" Scott Sauyet <scott@sauyet.com> - 2016-07-03 00:13 +0000
Re: An "if" without "if" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-03 11:07 +0100
Re: An "if" without "if" "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-07-03 10:29 -0700
Re: An "if" without "if" Scott Sauyet <scott@sauyet.com> - 2016-07-04 01:08 +0000
Re: An "if" without "if" John Harris <niam@jghnorth.org.uk.invalid> - 2016-07-04 14:49 +0100
csiph-web