Functional Programming: Using the Functor Type Class in TypeScript

This will be a series with a few articles in which I attempt to explain and show the use cases of some functional programming patterns.
We will work with TypeScript and use the functional programming library fp-ts. The examples and explanations are inspired by the great article series by the author of fp-ts.

#programming #typescript #javascript #functional-programming #software-development

What is GEEK

Buddha Community

Functional Programming: Using the Functor Type Class in TypeScript

Functional Programming: Using the Functor Type Class in TypeScript

This will be a series with a few articles in which I attempt to explain and show the use cases of some functional programming patterns.
We will work with TypeScript and use the functional programming library fp-ts. The examples and explanations are inspired by the great article series by the author of fp-ts.

#programming #typescript #javascript #functional-programming #software-development

Edmond  Herzog

Edmond Herzog

1594971540

Functional Programming: Using the Monad Type Class

This will be a series with a few articles, where I attempt to explain and show the use cases of some functional programming patterns.

We will work with TypeScript and use the functional programming library fp-ts. The examples and explanations are inspired by the great article series from the author of fp-ts.

This is Part 7 of our series, and this article is about the Monad type class. We will explore what Monads are and what we can use them for.


The Problem With Nested Contexts

So far we have seen how we can use map() from the Functor type class to lift pure unary functions to the Functor “world” and how we can use ap() from the Apply type class to lift pure n-ary functions.

Functors allow us to lift functions like (a: A) => B.

Apply allows us to lift functions like (a: A) => (b: B) => C.

But what if we have a function like (a: A) => M<B> (M is a placeholder for an effect, e.g., Option or Array)? If we were to use this function with map(), we would end up with a nested context: M<M<B>>.

#software-development #functional-programming #typescript #programming #javascript

Tia  Gottlieb

Tia Gottlieb

1596645240

Parenthesis Balancing using Monoids in TypeScript

The Parenthesis Balancing problem is a classic problem that says :

Given an expression string exp, write a program to examine whether the pairs and the orders of “(“, “)” are correct in exp.

so this (()()()())is balanced but this one ())) is not

in this article we are going to solve this in a functional manner using Typescript and the monoids from the previous article.

Monoids in TypeScript

“Alternatively, the fundamental notion of category theory is that of a Monoid”

medium.com

we are going to define a simple balance type that will hold the number of the left end right values.

this amazingly forms a monoid. Since we can concatenate two Balance objects and get a new balance.

#typescript #functional-programming #functional #javascript #monoids #function

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

Julie  Donnelly

Julie Donnelly

1602291900

How to Design Pure Functions

Today I am going to talk about how to design Pure Function and help you understand the benefits of pure function.

Nowadays Functional Programming is getting a lot of attention due to the advantages it offers like parallelismeasier testing_predictability _ and many others.

Pure functions is a concept mainly used in functional programming languages but it can be applied in any programming paradigm

Definition

Pure functions can be defined as

  • The function always returns the same value for the same inputs. The output is only determined by its input values.
  • A function where the return value is only determined by its input values, without observable side effects

Characteristics Of Pure Function

  • Function should always return a result.
  • Function must only depend on their inputs. That means this function should not use any other value than the function inputs to calculate the result
  • Pure functions cannot have other side effects than the computation of its return value.
  • Referentially transparent: An expression is said to be referentially transparent if
  • it can be replaced with its corresponding value without changing the
  • program’s behaviour.

#functional-programming #lambda-function #functor #kotlin #programming #functional-components #coding #coding-skills