 1602819699

# Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions?

I think we, as programmers, all do.

Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

Let’s get into it.

## 1. Pure Functions

When it comes to pure functions, you need to remember only two things:

• They return the same result if given the same arguments.
• They don’t cause any side effect.

### Returning the same result if given the same arguments

Let’s look at the simple example below:

``````const DISCOUNT = 0.5;

const calculatePrice = price => price * DISCOUNT;
let actualPrice = calculatePrice(15); // 7.5
``````

Is the function **calculatePrice **pure? No, it’s not.

Why? Because the global variable DISCOUNT is not passed to the function as an argument. If you change the DISCOUNT’s value, the returning result will also change, although the argument price stays the same.

To make it pure function, you have to add one more parameter to the function like this:

``````const DISCOUNT = 0.5;

const calculatePrice = (price, discount) => price * discount;
let actualPrice = calculatePrice(15, DISCOUNT); // 7.5
``````

#javascript-tips #javascript-development #coding #javascript #functional-programming

## Buddha Community  1602819699

## Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions?

I think we, as programmers, all do.

Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

Let’s get into it.

## 1. Pure Functions

When it comes to pure functions, you need to remember only two things:

• They return the same result if given the same arguments.
• They don’t cause any side effect.

### Returning the same result if given the same arguments

Let’s look at the simple example below:

``````const DISCOUNT = 0.5;

const calculatePrice = price => price * DISCOUNT;
let actualPrice = calculatePrice(15); // 7.5
``````

Is the function **calculatePrice **pure? No, it’s not.

Why? Because the global variable DISCOUNT is not passed to the function as an argument. If you change the DISCOUNT’s value, the returning result will also change, although the argument price stays the same.

To make it pure function, you have to add one more parameter to the function like this:

``````const DISCOUNT = 0.5;

const calculatePrice = (price, discount) => price * discount;
let actualPrice = calculatePrice(15, DISCOUNT); // 7.5
``````

#javascript-tips #javascript-development #coding #javascript #functional-programming 1623551520

## 3 Pandas Functions That Will Make Your Life Easier

### A practical guide with worked through examples

Pandas is a prevalent data analysis and manipulation in the data science ecosystem. It provides lots of versatile functions and methods to perform efficient data analysis.

In this article, we will cover 3 Pandas functions that will expedite and simplify the data analysis process.

### Conclusion

#programming #python #machine-learning #data-science #pandas functions that will make your life easier #pandas functions 1603857900

## 4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

According to an analysis, a developer creates 70 bugs per 1000 lines of code on average. As a result, he spends 75% of his time on debugging. So sad!

Bugs are born in many ways. Creating side effects is one of them.

Some people say side effects are evil, some say they’re not.

I’m in the first group. Side effects should be considered evil. And we should aim for side effects free code.

Here are 4ways you can use to achieve the goal.

## 1. use strict;

Just add use strict; to the beginning of your files. This special string will turn your code validation on and prevent you from using variables without declaring them first.

#functional-programming #javascript-tips #clean-code #coding #javascript-development #javascript 1596300660

## Functional Programming Series (2): What Is a Monoid?

For those interested in functional programming, I’ll talk about monoids and why they’re very important to understand ahead of time.

Don’t get confused: This isn’t monad — it’s monoid. I’m pretty sure you already know of monoids and you use them almost every day — you just didn’t know the term for them.

# Identity Function

Let’s assume there’s a function named `identity` that takes `A` and returns `A`.

``````const identity: <A>(a: A): A => a;

interface Student {
name: string;
age: number;
}
identity<number>(3) // 3
identity<string>('hello') // hello
identity<Student>({
name: 'Bincent',
age: 5
}); // { name: 'Bincent', age: 5 }
``````

In functional programming, this useless function (seems useless) is an important factor for many other concepts (such as monoids) that we’re about to talk about. Basically, a monoid is a set of elements that holds the rules of the semigroup and the identity-element rule.

If `S` is a set of elements, `a` is a member of `S`, and `·` is a proper binary operation, `a·e = e·a ∈ S` must be satisfied to be a monoid.

``````Identity: a ∈ S, a·e = e·a = a ∈ S
``````

Some documentation calls this using the number 1 and the any alphabet in subscript — for example, 1x referring to the identity on the variable x. Or some documentation uses just a single alphabet letter, such as or e.

That’s all there is to know about monoids, let’s practice with some simple examples.

#typescript #programming #functional-programming #javascript #coding #function 1596848280

# Introduction

In this post, I will explain why declarative code is better than imperative code.

Then I will list some techniques to convert imperative JavaScript to a declarative one in common situations, defining key terms along the way.

# Why declarative ?

First, let’s define what declarative and imperative mean.

Declarative code is one that highlights the intent of what it’s doing.

It favors the “what” over the “how”.

In other words, the exact implementations actually doing the work (aka the “how”) are hidden in order to convey what that work actually is (aka the “what”).

At the opposite, imperative code is one that favors the “how” over the “what”.

Let’s see an example:

The snippet below perform two things: it computes the square of `x`, then check if the result is even or not.

``````// imperative way

const x = 5;

const xSquared = x * x;

let isEven;

if (xSquared % 2 === 0) {
isEven = true;
} else {
isEven = false;
}
view raw
block1.js hosted with ❤ by GitHub
``````

Here, we can see that we finally get `isEven` after several steps that we must follow in order.

These steps describe “how” we arrive to know if the square of `x` is even, but that’s not obvious.

If you take a non-programmer and show him this, he might have a hard time deciphering it.

Now let’s see another snippet where I introduce a magic `isSquareEven` function that performs the two same things than the previous one.

#functional-programming #javascript #javascript-tips #programming #declarative-programming #function