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


Groups > comp.lang.javascript > #30601 > unrolled thread

Re: An "if" without "if"

Started byandber93@web.de
First post2016-06-02 02:16 +0200
Last post2016-07-04 14:49 +0100
Articles 7 — 6 participants

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#30601 — Re: An "if" without "if"

Fromandber93@web.de
Date2016-06-02 02:16 +0200
SubjectRe: An "if" without "if"
Message-ID<nintrv$2uc$1@news.albasani.net>
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

[toc] | [next] | [standalone]


#30607

FromStefan Weiss <krewecherl@gmail.com>
Date2016-06-02 12:37 +0200
Message-ID<nip29s$r1a$1@news.albasani.net>
In reply to#30601
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

[toc] | [prev] | [next] | [standalone]


#30811

FromScott Sauyet <scott@sauyet.com>
Date2016-07-03 00:13 +0000
Message-ID<nl9lap$sco$1@dont-email.me>
In reply to#30607
Stefan Weiss wrote:

> 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. 

Another very interesting work which proceeds the same way is Reg 
Braithwaite's _JavaScript Allongé_, either the original edition [1] or 
the ES6 one [2].

To my mind, this is one of the very best books yet written on Javascript.

  [1]: https://leanpub.com/javascript-allonge
  [2]: https://leanpub.com/javascriptallongesix


  -- Scott

[toc] | [prev] | [next] | [standalone]


#30814

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-07-03 11:07 +0100
Message-ID<87y45j6nm0.fsf@bsb.me.uk>
In reply to#30811
ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Scott Sauyet <scott@sauyet.com> writes:
>>Stefan Weiss wrote:
>>>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. 
>>Another very interesting work which proceeds the same way is Reg 
>>Braithwaite's _JavaScript Allongé_, either the original edition [1] or 
>>the ES6 one [2].
>
>   In the meantime, my course has finished, and I have learned
>   that for the goal of my beginner's course, which is to learn
>   event handling with JavaScript, I do not need neither
>   parameters nor default values nor the keyword »function«,
>   so, in the next course, I'll just explain parameterless
>   arrows only. For example, I'll explain
>
> ()=> 1
>
>   and slightly later I'll explain blocks, like, for example, in
>
> f = ()=> { console.log( "a" ); return 3; }
>
>   . Then, near the end of the short (18 hours) course,
>   I can show code like the following:
>
> onClick = () => span.classList.toggle( "off" );
>
> span = window.document.getElementById( "number" );
>
> window.document.getElementById( "brackets" ).
> addEventListener( "click", onClick );

Avoiding parameters seems like an odd choice:

  window.document.getElementById( "brackets" ).
  addEventListener( "click",
      (span => {return () => span.classList.toggle( "off" );})(
          window.document.getElementById( "number" ))
  );

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#30816

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-07-03 10:29 -0700
Message-ID<76b83093-9b17-4910-93bc-2b311ed0f16e@googlegroups.com>
In reply to#30811
On Saturday, July 2, 2016 at 7:13:19 PM UTC-5, Scott Sauyet wrote:
> Stefan Weiss wrote:
> 
> > 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. 
> 
> Another very interesting work which proceeds the same way is Reg 
> Braithwaite's _JavaScript Allongé_, either the original edition [1] or 
> the ES6 one [2].
> 
> To my mind, this is one of the very best books yet written on Javascript.
> 
>   [1]: https://leanpub.com/javascript-allonge
>   [2]: https://leanpub.com/javascriptallongesix


It looks like this writing was inspired somewhat by the style of the book series by Daniel P. Friedman and Matthias Felleisen:

"The Little Schemer"
"The Seasoned Schemer"
"The Reasoned Schemer"
and others in the collection

[toc] | [prev] | [next] | [standalone]


#30817

FromScott Sauyet <scott@sauyet.com>
Date2016-07-04 01:08 +0000
Message-ID<nlccua$u8f$1@dont-email.me>
In reply to#30816
Michael Haufe (TNO) wrote:
> Scott Sauyet wrote:

>> Another very interesting work which proceeds the same way is Reg
>> Braithwaite's _JavaScript Allongé_, either the original edition [1] or
>> the ES6 one [2].
>> 
>> To my mind, this is one of the very best books yet written on
>> Javascript.
>> 
>>   [1]: https://leanpub.com/javascript-allonge 
>>   [2]: https://leanpub.com/javascriptallongesix
> 
> 
> It looks like this writing was inspired somewhat by the style of the
> book series by Daniel P. Friedman and Matthias Felleisen:
> 
> "The Little Schemer"
> "The Seasoned Schemer"
> "The Reasoned Schemer"
> and others in the collection

That's an interesting observation.  I've read those, as well as _The 
Little MLer_ and _The Little Prover_, and I did not make that 
connection.  I can certainly see some overlap, but Braithwaite's style 
here seems _sui generis_.

Clearly there is a focus on simple explanations and building up from a 
very small basis, but that's about all I find in common stylistically.  
Nonetheless, I very much enjoy both styles.

  -- Scott


[toc] | [prev] | [next] | [standalone]


#30818

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2016-07-04 14:49 +0100
Message-ID<iaqknbhe2r6k5lkb7b5scj165idc67iqkj@4ax.com>
In reply to#30811
On Sun, 3 Jul 2016 00:13:13 -0000 (UTC), Scott Sauyet
<scott@sauyet.com> wrote:

  <snip>
>Another very interesting work which proceeds the same way is Reg 
>Braithwaite's _JavaScript Allongé_, either the original edition [1] or 
>the ES6 one [2].
>
>To my mind, this is one of the very best books yet written on Javascript.
>
>  [1]: https://leanpub.com/javascript-allonge
>  [2]: https://leanpub.com/javascriptallongesix

Version [2] starts well. The author says 

"But while JavaScript Allongé attempts to be provocative, it is not 
prescriptive. There is absolutely no suggestion that any of the 
techniques shown here are the only way to do something, the best way, 
or even an acceptable way to write programs that are intended to be 
used, read, and maintained by others."

How different from Douglas Crockford!

  John

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web