

Functional JS with ES6 — Booleans, Conditionals, and Operators
source link: https://www.tuicool.com/articles/hit/Ib2QfiR
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

I’ll start this article with a warning, I mention the use of Lambda Calculus, but do not let that scare you! I promise, no knowledge of λ-Calculus is required to understand or use the main concepts in this article! Phew!
In my last Functional JS article , we went over recursive patterns that allow you to operate/iterate over array values. This time, we’re going to get a bit more abstract to hopefully explain some of the bare fundamentals of functional programming.
A moment on Currying
You’ll notice that we never define a function with more than 1 argument. We return a new function for every additional argument needed. This is a technique used very commonly in functional programming that has its roots in Lambda Calculus. This has multiple benefits, but is used mainly to simplify partial application of functions.
What are we doing?
Glad you asked! We’re going to re-create: true
, false
, ||
(or), &&
(and), !
(not), ==
(equals), and !=
(not equals). As a bonus, we’re going to use some magical Lambda Calculus to make our functions much more efficient!
Let’s get started!
The first functions we need to define are our booleans: true
and false
. How we define these functions makes more sense once they’re used with the conditional function we soon define. The way we represent booleans as functions is a concept known as Church Booleans.
TRUE (true):
No, I’m not trying to yell, I’m using this naming convention because true in all lowercase is a reserved word and can’t be used to name our function. This function takes two arguments and will always return the first argument. In comparison, the FALSE
function does the same, but always returns the second argument. These functions are also known as selectFirst
and selectSecond
(in other languages) as they select the first and second arguments of the function passed in.
FALSE (false):
Almost identical to the TRUE
function, but returns the second argument rather than the first.
Conditional (cond):
The next function we need to define is our function that will handle conditional logic. Normally we’d use an if/else statement or a ternary to do this, but we want everything to be a function. It’s functions all the way down, this comes in handy later on.
If you break down a conditional in JavaScript, you end up with 3 parts:
true false
To mimic this, we will create a function that takes 3 arguments and returns the conditional function with both expressions applied. If you notice, I place the conditional last rather than first. This is a functional programming convention that helps when creating partially applied functions.
If you remember how we defined our booleans, they are both functions that when called with 2 arguments, return one of these arguments. This is exactly how our conditional function is going to work. The conditional function ( TRUE
or FALSE
) passed in is called with 2 arguments, our expressions when true and false.
Not (!)
This is the first logical operator we will define. It takes a single argument: x
. If x
is TRUE
it returns FALSE
, if x
is FALSE
it returns TRUE
. We make use of the previously defined cond
function to handle the conditional logic. Within the conditional: if x
is TRUE
we return FALSE
otherwise we return TRUE
.
Or (||)
This is the first logical operator we will define. It takes 2 arguments: x
and y
. If either x
or y
is TRUE
, we will return TRUE
, otherwise we return FALSE
. Again, we use the cond
function to handle this. Within the conditional: if x
is TRUE
we return TRUE
otherwise we return y
.
And (&&)
This function takes 2 arguments: x
and y
. Both x
and y
must be TRUE
for this to return TRUE
, otherwise it will return FALSE
. We make use of the cond
function again, not much is different. Within the conditional: if x
is TRUE
we return y
otherwise we return FALSE
.
Equal (==)
This function takes 2 arguments: x
and y
. Both x
and y
must be the same value for this to return TRUE
otherwise it returns FALSE
. We need to use the cond
function and the not
function to handle this one! Within the conditional: if x
is TRUE
we return y
otherwise we return not(y)
to only return TRUE
when y
is also FALSE
.
Not Equal (!=)
This function takes 2 arguments: x
and y
. x
and y
must not be the same value for this to return TRUE
, otherwise FALSE
is returned.
EXTRA:β (Beta) Reductionsthrough λ-Calculus
Disclaimer: do not feel the need to understand this at all as it is not a requirement to understand and use functional concepts. However, I think it’s an interesting topic to learn about and helps demonstrate the power of functional programming through math. I will gloss over topics as I am not the best resource to learn λ-Calculus, it is not the focus of this article, just an added bonus. These optimizations are handled automatically with most statically compiled functional languages.
And (&&) Reduction
In the following example, we use β reduction to remove the use of our cond
function entirely from our and
function. Our and
function is now composed entirely with booleans and is mathematically equivalent!
Or (||) Reduction
Again, we use reduction to remove the use of our cond
function entirely from our or
function. Like before, our or
function is now composed entirely with booleans and is mathematically equivalent.
Equal (==) Reduction
Just like before, we use reduction to remove the use of our cond
function entirely from our equal
function. Our equal
function is now composed entirely with booleans and is mathematically equivalent.
Not Equal(!=) Reduction
For the last time, we remove the use of our cond
function entirely from our notEqual
function. Our notEqual
function is now composed entirely with booleans and is mathematically equivalent.
Wrapping Up
I hope this helped demonstrate the power and flexibility of functions. This logic can be shared and re-written in any language that has first-class functions!
The functions and concepts used in the article will be expanded in a later article where we use Church numerals to represent natural numbers as pure functions. We will even implement arithmetic and comparison operators for these numbers! :rocket:
Recommend
-
75
Store a associative array of booleans in the database efficiently using binary numbers.
-
57
In my last Functional JS article, we went over recursive patterns that allow you to operate/iterate over array values. This time, we’re going to get a bit more abstract to hopefully explain some of…
-
11
Programming with truth and statefulness. I like formal specification because I don’t really understand it. That is kind of my...
-
13
Be Careful with Async Functions that Return Booleans Here’s a fun bug I recently encountered… Let’s say we have this async JavaScript function: const isBroken = async () =>...
-
9
Compiling a Lisp: Booleans, characters, nil September 2, 2020 first –
-
4
Writing a Lisp, Part 1: Booleans October 28, 2016 Last time we wrote a simple interpreter that could read in numbers. That’s cool and all, but we’ll need to re...
-
8
Basic Data Types in Python 3: BooleansWelcome back to our ongoing series of blog posts on basic data types in Python 3! Last time, we explored the functionality of
-
8
« back — written by Brent on February 14, 2020 Bitwise booleans in PHP ...
-
7
Python Booleans: Leveraging the Values of Truth Understanding how Python Boolean values behave is important to programming well in Python. The Python Boolean type is one of Python’s
-
5
What are conditionals and what do I use them for? The best way to think about conditionals is essentially just as a choice. Whenever a choice comes up, we can take one of various different paths forward. Either path i...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK