Prasenjit Kumar Nag a.k.a Joy

A Developers Adventure in Coding

Function as Data, Data as Function

Learning programming with C, functions were a very special thing for me in my early days. Though there were function pointers back then, didn’t use those much and I had this idea that functions are very different from data.

After being introduced to closures and lambdas, functions started becoming less special, with the ability to use them as first class citizens they seemed very much like data as I could pass those around like values, could create higher order functions.

After spending some time with Haskell it became a lot more clear that functions and data aren’t very different. And by function here I mean functions without any funny business aka pure functions. For anyone not knowing what pure functions are, they are like Matemathical functions which always return the same value for the same set of arguments.

My purpose in this post is to try explaining why functions are very much like data. The code examples will be in Haskell but they will be simple enough to be understandable to anyone having decent programming experience.

Designing Good Background Jobs: Idempotence

What are Background Jobs?

Background jobs are almost part and parcel of every modern Web applications now-a-days. It’s the process of deferring time-intensive operations in the background and sending early feedback to end users. Examples of background jobs could be generating reports, processing large amount of data, sending scheduled emails and doing any other kind of works which are necessary but not worth blocking the otherwise quick response to waiting user. I can go on forever with examples of background jobs, candidates for background jobs are omnipresent (everywhere).

Folding Structures Aka Catamorphisms

What does it mean by Folding or Reducing a data structure?

Folding or Reducing is the notion of creating a summary value from a list of values contained inside a certain structure. The structure can be as simple as list of values or a complex structure. The way one will want to reduce the structure is something that will depend on the end goal or the structure of data. Folding or Reducing generalizes this notion which are also known as Catamorphisms.

Understanding the Ubiquitous Notion of Mapping a Function Over a Structure

What is Map?

The notion of Map has become so prevalent in all programming languages that almost everyone has some kind of exposure to it. Generically speaking, Map means applying a function to values which are inside some kind of structure.

We usually use it a lot to apply a function to a list of values e.g mapping over the values of an Array or List to be more specific.

In this post we will first see some examples of mapping in different languages and then we dive deep into it’s internals in terms of Haskell and maybe finish with a better understanding of how everything fits together. Saying that lets start.

Hello Haskell – Partial Function Application

In this blog post, We will be talking shortly about Haskell’s type system( which is a very broad topic, this post won’t eve scratch the surface). I will just explain what is needed to explain partial function application means and how is it done in Haskell.

N.B. Installing Haskell is very easy with Stack, if you are on a Mac, you can just do brew install haskell-stack to install Stack. Then starting the repl is as easy as writing stack ghci in your favorite terminal.

Haskell is a statically typed functional programming language. That means everything has a type associated with it and the compiler checks and enforce types at compile time. So there are two phases of code in Haskell one is Type Level Code which involves type definition, type constructors etc. Another is Term Level Code which is the body of the program. That doesn’t necessarily mean you will always have to write type signatures for trivial things. Haskell has a very powerful Type Inference system which can infer types from untyped code, so you don’t have to specify types of all the things but Haskell will infer them from the code and complain if types of anything doesn’t match. It uses Hindley-Milner Inference algorithm( well, an extended version of it for Constrained Types).

Intro to Lambda Calculus and It’s Relationship to Functional Programming

This post is written based on the First Chapter (All you need is lambda) of the HaskellBook which is great book for learning Haskell. All the code examples in this post will be using Haskell but should be understandable to anyone having decent experience in any programming language.

Lambda calculus is a model of computation devised in the 1930s by Alanzo Church. It’s one process for formalizing a method. Like Turing machines it formalizes the concept of effective computability, thus determining which problems or classes of problems, can be solved. You might be thinking but thats Calculus(Math), how is it related to functional programming or programming in general. It is indeed deeply related.

Laravel View Presenters From Scratch

Why do We need View Presenters?

View should be dumb, they shouldn’t do much other than showing data to the user. So where’s the correct place to place your view specific logic in Laravel. Like you need the gravatar url of the user to see if the user already has a gravatar set to save the user from uplaoding a new one in your site. Or maybe you save user name as two field in db, as first_name and last_name, now you need to create user’s full name, but you also want apply the function ucfirst first on them. Or maybe you have users profile pic’s filename saved in database, now you need to create profile pic url from that by concatenating the upload directory.

Where should we put this sorts of logic. Eloquent Model? This can seem a very tempting choice, but why should the Model handle View specific logics. Then where is the place to put this kind of things?

Well maybe in their own class whose only responsibility will be to do all the logic needed in view using a specific model.

In laravel, there’s ins’t any built in way to do 1-to-1 Model-Presenter mapping out of the box but it’s incredibly easy to set up one.

Command Driven Design: The World of DTOs, Handlers and Domain Events

Command driven desing is quite a new buzzword in php world which the community is adopting quickly. Lets dive into some theoritical details of whats and whys of this desing patterns. Code examples will be based on Laravel.

Coming out from the CRUD mentality

We developer like to think in terms of CRUD(CREATE, READ, UPDATE, DELETE) like kind of one-to-one mapping with business needs and out models but all business needs doesn’t go well with CRUD.

Exploring Ruby’s Proc, Lambda and Blocks

I have a weakness for ROR(Ruby on Rails) partly due the reason I am always obsessed with learning to do things in better ways for a long time. I am still a noob but I want be a very good Software Architect someday, with deep knowledge and practical experience on software design and development following all the best practices.

And what I have found, Rails projects are naturally more likely to follow better ways of doing things, and community is much more robust. Though the reason behind my this perception can be the fact that I started with CodeIgniter mainly and worked on some some CI projects at the early stage of my Career which were initially done by some Indian developer and they did mostly everything from controller in CodeIgniter, there were no models, not rules of where to put what. It was a pure mess. Anyway, these things pushed me towards my quest for searching for better ways of doing things, and I did some research on ROR and found the community around ROR is great. They do TDD( Test Driven Design ) and there were many great tools to help developers test their code, TDD in PHP community is gaining traction too recently. But still I see most PHP devs takes testing as optional and find excuses to not write automated tests. Projects I have worked on didn’t have chance/scope to do TDD. I am a not-so-experienced avid follower of TDD and design patterns and wanna be good on them.