Dylan  Iqbal

Dylan Iqbal

1630424184

Functional Programming with JavaScript

Functional Programming is becoming increasingly popular and relevant with each year that goes by. With so much discussion around languages such as F#, Haskell and Erlang, it can seem as though getting started with Functional programming would mean first learning a whole new syntax...but what if it didn't?
Using just a few JavaScript libraries, such as RamdaJS, developers can implement some of the most powerful patterns and techniques from the world of functional programming?
This talk will demonstrate how, using ES6 and a few extra libraries, we can write Functional code that is:
More robust
Easier to read
Easier to maintain
This talk might be of interest to anyone looking into moving to a new platform, or in improving the scalability of an existing application, or even just interested in seeing what Functional Programming is all about, but all within the comfort of a familiar language.
We might even attempt the impossible, and explain what a Monad is!

#javascript #es6 #functionalprogramming #functional #programming

What is GEEK

Buddha Community

Functional Programming with JavaScript
Nat  Kutch

Nat Kutch

1596848280

From imperative to declarative JavaScript

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

Vincent Lab

Vincent Lab

1605017502

The Difference Between Regular Functions and Arrow Functions in JavaScript

Other then the syntactical differences. The main difference is the way the this keyword behaves? In an arrow function, the this keyword remains the same throughout the life-cycle of the function and is always bound to the value of this in the closest non-arrow parent function. Arrow functions can never be constructor functions so they can never be invoked with the new keyword. And they can never have duplicate named parameters like a regular function not using strict mode.

Here are a few code examples to show you some of the differences
this.name = "Bob";

const person = {
name: “Jon”,

<span style="color: #008000">// Regular function</span>
func1: <span style="color: #0000ff">function</span> () {
    console.log(<span style="color: #0000ff">this</span>);
},

<span style="color: #008000">// Arrow function</span>
func2: () =&gt; {
    console.log(<span style="color: #0000ff">this</span>);
}

}

person.func1(); // Call the Regular function
// Output: {name:“Jon”, func1:[Function: func1], func2:[Function: func2]}

person.func2(); // Call the Arrow function
// Output: {name:“Bob”}

The new keyword with an arrow function
const person = (name) => console.log("Your name is " + name);
const bob = new person("Bob");
// Uncaught TypeError: person is not a constructor

If you want to see a visual presentation on the differences, then you can see the video below:

#arrow functions #javascript #regular functions #arrow functions vs normal functions #difference between functions and arrow functions

Giles  Goodwin

Giles Goodwin

1603176407

The real reason why JavaScript has arrow functions

Nowadays, all my code is based on the use of arrow functions. If you are still not using them yourself, then don’t be ashamed of who you are. That’s your parent’s job. Instead, find about all the benefits that you can get by using arrow functions like the cool kids.

This is an example of arrow function and the same code written traditionally:

const arrowFunction = (arg1, arg2) => arg1 + arg 2;

const traditionalFunction = function(arg1, arg2) {
  return arg1 + arg2;
};

You may notice that the code is shorter and that there is an arrow. Everything before the arrow is arguments of the function and everything after the arrow is always returned as the result of the function.

If you need a function that contains multiple statements you can still do this:

const arrowFunction = (arg1, arg2) => {
  const result = arg1 + arg2;
  return result;
};

#javascript #js #functional-javascript #functional-programming #javascript-tips

Tia  Gottlieb

Tia Gottlieb

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.


Prior to Reading

This is a series on functional programming, so you might not understand what this article is going to talk about if you haven’t read the previous posts.

You can check out other posts related to this topic


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.

Image for post

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

Let’s Talk Functional Programming

Most of what I will discuss in this article is knowledge accumulated from reading, “Functional Programming in JavaScript”, by Luis Atencio. Let’s dig right in…

What is functional programming?

In simple terms, functional programming is a software development style that places a major emphasis on the use of functions. You might say, “Well, I already use functions daily, what’s the difference?” Well, it’s not a matter of just applying functions to come up with a result. The goal, rather, is to abstract control flows and operations on data with functions in order to avoid side effects and reduce mutation of state in your application.

#programming #javascript #functional #web-development #functional-programming