Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: An "if" without "if"

48 views
Skip to first unread message

Thomas 'PointedEars' Lahn

unread,
Jun 1, 2016, 8:08:31 PM6/1/16
to
Stefan Ram wrote:

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

Yes, but that is a stupid idea.

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

andb...@web.de

unread,
Jun 1, 2016, 8:16:04 PM6/1/16
to
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)

unread,
Jun 2, 2016, 3:20:17 AM6/2/16
to
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_encoding>

But why would you want to?

Stefan Weiss

unread,
Jun 2, 2016, 6:37:54 AM6/2/16
to
andb...@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

Scott Sauyet

unread,
Jul 2, 2016, 8:13:19 PM7/2/16
to
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

Ben Bacarisse

unread,
Jul 3, 2016, 6:07:57 AM7/3/16
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> Scott Sauyet <sc...@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.

Michael Haufe (TNO)

unread,
Jul 3, 2016, 1:29:38 PM7/3/16
to
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

Scott Sauyet

unread,
Jul 3, 2016, 9:08:33 PM7/3/16
to
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



John Harris

unread,
Jul 4, 2016, 9:49:40 AM7/4/16
to
On Sun, 3 Jul 2016 00:13:13 -0000 (UTC), Scott Sauyet
<sc...@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
0 new messages