Image for post
Image for post

One can argue that composing functions is the most important idea in programming and functional programming in particular. The idea of compositionality is of monumental importance in human thinking. Creating systems by composing smaller systems that are easier to implement and design can be found in all levels of programming. Staring from the Syntax of the language, to Design patterns, architectural patterns, larger System design etc.

In this section we are going to take a brief look at one of the most basic ones; Function composition.


github repo : https://github.com/dimitris-papadimitriou-chr/FunctionalJsWithCategories/blob/master/monads/state-example-1/demo.js

This is a short article on how to create a game loop using the State monad. Unfortunately i am not going to cover the theory of State monad in this article but hopefully i would do this in a subsequent article.

using the State we can pass a state along with a value. We are going to use this to build a simple loop. It’s a simple string parsing algorithm. This simple game is an haskell example and goes like this :


Passes a string of dictionary {a,b,c}
Game is to produce a number from the string.
By default the game is off, a C toggles the
game on and off. A 'a' gives +1 and a b gives -1.
E.g …


Functors are closed under composition. This means that we should be able to construct a well-behaved map method for the composition.

Functors are closed under composition. The term closed means that if we have two functors and we compose them (have a functor-in-a-functor situation like Maybe<Id<>> or Maybe<Array<>>, Array<Array<>> etc), the resulting structure is also a functor.

This means that we should be able to construct a well-behaved map method for the composition, using only the map methods of the composing functors and nothing more.

Let us examine some specific cases. We will start with the Id<Array<>> case. This is not a Type that you will see in practice but will make the mechanics easier to examine. It’s easy to get a Id<Array<>>. …


Image for post
Image for post

Source Code

This article wants to intergrate the Folktale.js Maybe type in a React.js application

We are going to use the Maybe type from a Functional Library called Folktale.js. Folktale is probably the 3rd most popular Functional library (after Rambda and Senctuary.js)

If you are not familiar with the maybe functor read the previous article :

Folktale provides a Maybe Type with the two Cases named Just and Nothing. Other that that we have a map method and a pattern matching method called matchWith

Maybe.matchWith({
Just: ({ value }) => _,
Nothing: () => _
});

The core example use case, upon which we are going to build the various ideas in this course is a simple retrieval of a Client from a database Repository that matches a specific Id. …


Image for post
Image for post

In real life situations, composite types like CompletableFuture<Optional<>> arise naturally.

We will create a simple Repository that retrievals some Client that matches a specific Id. The mock repository will be implemented with an in-memory storage of the Clients in a simple Array.

This use case can arise for example, when we access a Database with Hibernate, MondoDb etc, or wait for an HttpClient GET request.

To emulate a general case, we will use the following mock Repository

Run This: Fiddle

the findAny( ) method of the stream returns an Optional which is empty in the case of the empty…


Image for post
Image for post

Source Code .NET Fiddle : Maybe Monad Example C# 8.0 Pattern Matching

To be honest i didn't like the pattern matching feature the first time i show it. But i rewrote some sample functional code and i can say i might be a fan.

if you are not familiar with pattern matching read the previous article :

this article revises the Maybe monad seen previously

We will rewrite Maybe using C# 8.0 pattern matching

this is the initial

and now we can just simply replace it with

placing the map definition in the base class

public abstract class Maybe<T>
{
public Maybe<T1> Map<T1>(Func<T, T1> f) =>
this switch
{
None<T> { } => new None<T1>(),
Some<T> { Value: var v } => new Some<T1>(f(v)),
}…


Image for post
Image for post

Source

What is pattern matching ? Lets say that you have a simple hierarchy of shapes

Now if i make an assignment to a Shape variable . I can assign either a rectangle or a circle. Here i will go with the rectangle

Shape shape = new Rectangle { Width = 100, Height = 100, };

How can you find out during runtime what kind of shape did i put in ?

This is the underling problem that pattern matching wants to solve. We will see how C# 8.0 …


Image for post
Image for post

Source: https://codesandbox.io/s/parenthesis-balancing-usingmonoids-8ktvs

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.

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. …


Image for post
Image for post

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

— Categories for the Working Mathematician

Monoids are one of those profound ideas that are easy to understand and are everywhere. Monoids belong to the category of Algebras. There are a couple of very important ways to organize different structures that appear in functional programming. Algebras is one of them. Fantasy land has some of the most important algebras in its specification.

Wikipedia says a monoid is an algebraic structure with a single associative binary operation and an identity element. …


Image for post
Image for post

Trees are the single most important data structure in computer science, after lists. Just about everything you do in your programming career will be related to trees. For example JSON objects, xml, html DOM are tree structures. Object oriented JavaScript developers when they enter the realm of functional programming quickly learn how to use map, and reduce in order to replace traditional for loops. What they do not learn is that Trees also have a map and reduce methods. In this article we are going to display the functional way to reverse a Binary Tree .

if we have a tree that look like…

About

Dimitris Papadimitriou

More than 14 years’ experience as full stack developer and Software Architect .https://leanpub.com/u/dimitrispapadim

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store