From Functional to Reactive Programming

GIDS’17 Conference

As an attendee, I’m very much happy and I have gained some knowledge from GIDS 2017, and I would like to share the insights which I gained out of this conference.

I wish to share about the session “From Functional to Reactive Programming” given by Dr. Venkat Subramaniam, he is an excellent speaker, the session was very interesting and more interactive, the way he explained the concepts was really awesome.

From Functional to Reactive Programming:

What is Reactive Programming?

full-reactive

Reactive Programming is an asynchronous programming paradigm oriented around data streams and the propagation of change. This means that it should be possible to express static or dynamic data streams with ease in the programming languages used, and that the underlying execution model will automatically propagate the changes through the data flow.

Mostly as a software developer, we are writing code for quick solutions. But that code is not responsive and feasible even though it gives solution for that problem. In Reactive programming, it simplifies the codes and gives feasible and highly responsible solutions.

Imperative to Functional Style:

1-AM83LP9sGGjIul3c5hIsWg

Imperative, every programmer knows this style and most of the institutions teach this style, programmers gain significant experience with its style. But using functional style, we can avoid writing lot of code inside the function for simple calculations.

In this example, we are finding the first even number which is greater than 3 and doubling it. This is imperative style that too very complex. The same example in functional style,

Its clear, concise and readable code, and lets you focus on solving the problem rather than the required procedure.

Function composition:

composition-of-functions (1)

Function compositions means create a simple and reusable functions that we can combine to compose new function. In the about example we created small functions like isGreaterThan3() for finding number that is greater than three, isEven() for finding whether the number is even or not and doubleIt() for doubling the number. Then finally we have combined these functions to get the result.

Lazy Evaluations:

lazy (1)

Lazy evaluation is the program will not evaluate the expression until just-in -time when its value is needed. If the value is not needed during the execution of the program, the evaluation of the expression will be skipped.

In the above example the program will not evaluate the expression until we calling the findFirst() function. This function only triggering the program to filter isGreaterThan3() function, isEven() function and doubleIt() function.

Benefits of lazy evaluation:

The ability to define control flow

The ability to define potentially infinite data structures

Increase performance by avoiding needless calculations

Functional Reactive Programming:

Reactive programming also called functional reactive programming because it applies functions like lambdas and closures in a reactive manner asynchronous sequence of data. Reactive programming paradigm stays responsive in the face of failure, more responsive and responds in timely manner. An example of the reactive systems is Google Spreadsheet. The library Rx-Java used for reactive + functional programming in java 8.

Thanks to Dr.Venkat Subramaniam,  is an award-winning author, founder of Agile Developer, Inc., and an adjunct faculty at the University of Houston.

Golang Http Middleware

Http Middleware

Http Middleware is a shared functionality, using that we can log every request, gzip every response and check a cache before doing some heavy processing. Http Middleware has a single responsibility; we can plug it in our app at the interface level, it doesn’t affect our coding style like another layer in our request handling cycle. It is a pluggable and self-contained code, if we really want middleware we can plug it or if don’t want just remove it.                         wsgi_middlewareUsing middleware in Golang is very simple, we have to use middleware between a ServeMux and application handlers. The Http Middleware function accepts a handler as a parameter and returns a handler so that we can directly register the Middleware function with the ServeMux.

Middleware Chaining

T_AspNetCoreStartup_02In Golang, we can create handler chain by nested middleware functions inside another function. We can share values and also pass errors from one handler to another using middleware chaining.

Capture

This is an example of middleware chaining; we are passing the handler to “middlewareOne” as a variable, then transfer the control to the next “middlewareTwo” and then finally “loggingHandler”. This is necessary to know the control flow of middleware handlers, chain with other middleware handlers.

In Golang we can use Http Middleware for some specific handlers, no need to use it for all handlers. There are some third-party packages offers such kind of facilities.

Third-Party Middleware                                  

In Golang, we can use third-party package middleware instead of using our own middleware all the time. Well known third-party packages are Gorilla’s package and Negroni package.

img36

The Gorilla package has collection handler for use with Golang’s net/http package. It mainly offers handler for logging, compressing http request and responses, validating content types.

Negroni provides an idiomatic approach to use Http Middleware in Golang. It supports to use net/http handlers, and also offers a way to handle Http Middleware. Negroni package offers middleware functions for logging handlers and to use the middleware functions some specific routes.

Conclusion

The Http Middleware is one of the advantage in Golang, by using this we can log each http request, gzip all responses and also we can check the cache before doing some heavy processing. The flow control of middleware handler is very useful and effective, also we can use middleware functions for specific routes.