Monad – Theory Category
Most of us here are students of IT department, which means most of us know a bit of Java programming language, or at least we should know. Java is a well-known object oriented programming language (of course, you can use some other paradigms in Java, but it’s mostly OOP language). Some of you may know that there are other paradigms of programming like logic or scripting. When it comes to me, I’m a big fan of functional programming.
What is functional programming? It’s an approach that is really strongly related to math, especially theory category. There are books (quite thick ones) that try to explain some parts of FP, so it would be impossible for me to explain it in a short post. What I can do is present a quick introduction to really helpful thing – Monad.
Let’s look at monad definition from Wikipedia: “monad is a design pattern that allows structuring programs generically while automating away boilerplate code needed by the program logic. Monads achieve this by providing their own data type, which represents a specific form of computation, along with one procedure to wrap values of any basic type within the monad (yielding a monadic value) and another to compose functions that output monadic values (called monadic functions)” .
So, translating it to English, monad can be described as an abstract box that can keep different things and always has some specific methods/functions/actions that can be used or applied. Still not quite understandable? A friend of mine likes to describe monads as burritos, which can be made of almost everything. Every burrito has some kind of specific actions that can be applied to it, like put sauce on it or simply eat it.
When it comes to coding, I can show you some examples in most popular monads in Scala programming language, like:
- List[A] - a lot like in other programming languages, list is a structure that can be made of elements of type A.
- Try[A] - monad that can have two values, some success value of creating A or failure value.
- Option[A] - there can be stored two values - Some(A) or None.
Some more complex ones: Either, Writing Monad, State Monad.
On every monad you can use functions like:
- map - it applies a given function to every element of monad (or in monads like Try/Option it applies to a function if an element is present).
- foreach - doing some action, but not returning anything ( in most languages known as void or unit).
- filter - removes every element in monad if some logic condition is made.
- fold, foldLeft, foldRight - more complex one ;)
- and lot of others :)
Now, I would like to give you some quick code example of how it looks in real life. There is an example of code that tries to solve simple problem: we have lists of strings that should be int, but we are not sure if there is any “garbage” in it and we want to divide n number of every list with n (index) number of another list. It looks like that:
My questions for today:
- Have you ever tried FP? What are your thoughts of it?
- Have you ever used some other programming paradigm than OOP? If yes, which?
- Have you ever heard of Theory Category? Do you like mixing IT field with math?
Sources:
https://github.com/ssledz <- great thanks to Sławek, he is a great FP programmer and monad maniac. Happy to know you man!
https://en.wikipedia.org/wiki/Monad_(functional_programming)
Comments
1. There was a very short episode of Scala in my previous school, but it was only to show us a different paradigm that object-oriented. It was definitely too short to build up an opinion. In overall, I have never found any concrete “business problem” that could not be easily solved by object-oriented solution, so currently I am not interested in functional programming, but I guess that it just a matter of occasion.
2. Yes, I had to use the Prolog programming language because the “Artificial Intelligence” subject at school required that. It is a logic programming language. Did I like it? – No, not at all. It has a strange syntax and requires completely different way of thinking. In addition is has no real-life applications other than education.
3. No, unfortunately I have never heard of Theory Category. I don’t like mixing IT with math. I know that there is a natural correlation between math and IT, but I am a business kind of a programmer who tries to solve business problems. In addition to that, when I started to learn programming at the high school, it was presented as a math and algorithm subject only which really discouraged me to learn it at some point. I don’t say that it is wrong to use IT for the math purpose (which is perfectly fine and awesome) but I hate when somebody tries to identify information technology with math.
I am not very strong on different kinds of paradigm, I guess object programming offered by standard languages such as Java and Python are what I' sticking to for now.
Have you ever used some other programming paradigm than OOP? If yes, which?
As mentioned before: no, I haven't. But functional programming sounds interesting.
Have you ever heard of Theory Category? Do you like mixing IT field with math?
I have never heard about it. I think IT field and math are tightly related, so you cannot "mix" what is already mixed ;). However, interestingly enough, there are people who claim that IT can be done by people who do not know math well enough.
1. Have you ever tried FP? What are your thoughts of it?
Of course I used functional programming both at university and at work. Bah! Even a piece of my master's thesis is based on FP. :D
My thoughts on this subject are very simple, although not everyone is able to agree with this opinion - FP is one of the most convenient and most pleasant ways to solve many issues, especially those related to complex calculations.
2. Have you ever used some other programming paradigm than OOP? If yes, which?
PP (Procedural programming), OOP, FP are the basics, it is worth take a moment of simple curiosity to such programming trends as: Metaprogramming and LIST language or Rule-Based Programming.
On the one hand, it can be said that it will never be useful, but it certainly allows us to have a broader perspective on the world of programming that surrounds us.
3. Have you ever heard of Theory Category? Do you like mixing IT field with math?
What kind of computer scientists would we be without a mathematical foundation! Mixing IT and mathematics, especially in the field of science is indispensable and enables us to develop personally in the areas of interest to us.
Even when it seems to us that mathematics is not needed for programming, we subconsciously use it in every aspect of our work.
Not try. I don't think anything and I'm not interested. I'm not a IT student. How many obscure topics can you do for most people? It's difficult for us to answer.
Have you ever used some other programming paradigm than OOP? If yes, which?
I didn't use anything like that. I'm a creative person.
Have you ever heard of Theory Category? Do you like mixing IT field with math?
No,I haven't heard of Theory Category.I hate math and everything about it.
2. As above.
3. Never heard of it. And nope, I don't like mixing math with IT. Just pure IT.
2.Most of my engineering degree course was based on structured programming and I hated every minute of it.
3. No. I mostly do ML and data exploration so knowledge of statistics is quite important there and this is my favorite part of math.
2. Procedural programming.
3. Yes ,I heard of Theory Category. Yes, i’d like.
I'm working in web development, so FP is not a good thing to use it in that kind of area. As i know, FP helps to avoid changing state of components and data, that is not useful thing in web programming. Maybe i'm not profficient enough in it, but it's not right to use pure functions in web.
Have you ever used some other programming paradigm than OOP? If yes, which?
Luckily, i have never used programming paradigm instead of OOP.
Have you ever heard of Theory Category? Do you like mixing IT field with math?
I have never heard of Theory Category, but definitely i will take a look on it after your article. I think it might be interesting for me and for my colleagues.
I feel like people who code in C# and claims that they haven’t used and don’t like/don’t know anything about functional programming forgot about, for instance, LINQ which manifests many of functional features ;-) So yeah, I’ve tried FP and for me, like any tool, it is exceptionally good for it’s specific use-cases.
2. Have you ever used some other programming paradigm than OOP? If yes, which?
In our job we definitely can’t escape from using different tools, languages, paradigms and patterns. Saying so, except for OOP, personally I’ve been using paradigms like declarative programming, functional programming, logic programming, procedural programming and possibly many more which I can’t even remember now. Every approach has it’s pros and cons and targets various usage scenarios and even if we don’t plan to use it regularly it allows us, like Sławomir mentioned, to have a broader perspective on the programming world. Something that is also worth mentioning is that in many situations we can mix those approaches to find the best solution for smaller cases in our bigger project.
3. Have you ever heard of Theory Category? Do you like mixing IT field with math?
It appears that I’ve heard about the concept but I didn’t know the name of it. Mixing IT with math is often inevitable, doesn’t matter if we like it or not. Personally I don’t have any problem with it and I find it an inseparable part of the whole real-life programming.
No I haven’t, but maybe in the future.
I’ve never heard before about “Theory Category” and I don’t really enjoy mixing math and programming.
Functional programming, hmm, sounds very interesting. Unfortunately I have not had contact with this type of programming, but maybe it will change thanks to you. Generally, it looks strange.
I like object-oriented programming, but I know that many problems are difficult to solve by them.
In my opinion IT dont exists without math. Everything what we can do on computer contains some math. Sometimes it's deep inside and sometimes we can easily see it
Yes of course, for example logical programming when I was making a family tree in Prolog or declarative programming in SQL, imperative programming in C. I might be wrong and I don't want to be rude but nowadays people don't know much about programming paradigm because they either work as frontend dev or backend and that is the only distinction they are familiar with.
I have never heard about that theory. In my opinion, math is about solving problems and not about what is 2+2*2 and so is IT. People can say that they can do programming with knowing math but in my opinion, they are mistaken. Let's say IT stems from mathematics.
In programming, I recently, and just the same began my journey with Java. I was a little scared when I started reading your post, but calmed down when it came to Java. Well, as a result, I use and studied only the OOP model.
Have you ever used some other programming paradigm than OOP? If yes, which?
No, unfortunately not yet. But as necessary, I will try to study other models. It is likely that FP will be among them.
Have you ever heard of Theory Category? Do you like mixing IT field with math?
I think these are bordering areas. And programming without the use of mathematical methods is very difficult to imagine, I can be wrong, but for now I see such a picture in front of me.
Once i have to use prolog while studing IT on our university but I didn't liked it. I do not like the syntax and requirements.
I don't like mixing IT with math, ofcourse programming is somehow related to math but I don;t like to use alogitmhs in my work than I have to .
Of course, I think that it is suitable for many tasks, but not a panacea. I like restive approach in programing, and reactive programing steels a lot of features from FP.
Have you ever used some other programming paradigm than OOP? If yes, which?
I've used procedural programing paradigm bacon school while learning pascal. But now I'm working in Swift, and most of the time I'm using OOP, but Swift really allows you to use most of functional programing features with ease. Also I think Java is heavy and wasn't designed for this paradigm, and resulting code looks bulky and not readable.
Have you ever heard of Theory Category? Do you like mixing IT field with math?
No I didn't, please remind me when we started to separate IT field from math?
2. Nope
3. Never heard about Theory Category and about math in It I would say that I use only bare minimum of math when programming
3. I've never heard of “Theory Category”. I think IT is directly correlated with math no matter on the type of programming we do, the language may be different but math will always be at the bottom of it all.