[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

An "if" without "if"

ram

6/1/2016 11:33:00 PM

At one point in my course, I already have explained
arrow functions, but not »if« nor »?:« or anything
similar.

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

The second line will print »ok«, but the last
line will not.

So I have written a function that will print »ok«
/if and only if/ »a« is undefined.

Now, I would like the behavior not to depend on the
dichotomy between »undefined« and »not undefined«
but between »true« and »false«. So, I still need some
expression that will return »undefined« only for one
of »true« or »false«, but otherwise »defined«.

Here is one idea:

f = x => 1/+x
f( true )
f( false )

f gives »1« for »true«, so I want »undefined« for
»false«, but f gives me »Infinity« for »false«.
So this idea did not work.

If I would have such a function »f«, then

( ( x = console.log( "ok" )) => 0 )( f( a ))

would print »ok« only if »a« is false (or only if
it is true), and I would have built an »if« using
only arrow function expressions.

5 Answers

Thomas 'PointedEars' Lahn

6/2/2016 12:08:00 AM

0

Stefan Ram wrote:

> Could I use arrow functions to emulate an »if«?

Yes, but that is a stupid idea.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Andreas Bergmaier

6/2/2016 12:16:00 AM

0

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

Michael Haufe (\"TNO\")

6/2/2016 7:20:00 AM

0

Stefan Ram wrote
> Could I use arrow functions to emulate an »if«?

Yes, among many other things. What you're looking for is referred to as a Church Encoding:

<https://en.wikipedia.org/wiki/Church_en...

But why would you want to?

Stefan Weiss

6/2/2016 10:38:00 AM

0

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

ram

6/2/2016 12:07:00 PM

0

Stefan Weiss <krewecherl@gmail.com> writes:
>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?

The German-language course notes are available under

http://www.purl.org/stefan_ram/pub/javas...

. If you get a 403, you can try to visit the page with a
Google referrer or report this here, and I will look into it.

The chapters 0, 1, and 2 are more the way I intended them to
be. The later chapters are more experimental and less
stable, because I did not have had enought time yet to work
out everything. Some lessons beyond chapter 2 are terse or
might not be reader friendly, because I did not yet have the
time to work them out. But the main page of the course
should convey an idea of the approximate sequence of topics
treated.

The 18 hour course will end with chapter 6. So it will treat
handling events in the browser, but not control structures
like »if«.