Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30607

Re: An "if" without "if"

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: An "if" without "if"
Date 2016-06-02 12:37 +0200
Organization albasani.net
Message-ID <nip29s$r1a$1@news.albasani.net> (permalink)
References <if-20160602002158@ram.dialup.fu-berlin.de> <nintrv$2uc$1@news.albasani.net>

Show all headers | View raw


andber93@web.de wrote:
> 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;
>   }

The first thing that would come to my mind are logical operators:

  (/* condition */) && (() => {/* statements */})();

And yes, there are many other ways to write conditionals without `if`.

> 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]);

Typo: {true:t, false:f}


I have some doubts about the wisdom of introducing arrow functions and
default arguments to a class before covering basics like if-else branching,
but there may be a valid reason for this. OP, are the course notes
available? I'm curious how this would work.


- stefan

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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