Skip to main content

Week 3 [25-31.03] Monad


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:
  1. Have you ever tried FP? What are your thoughts of it?
  2. Have you ever used some other programming paradigm than OOP? If yes, which?
  3. 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

Okaaay, so I'm not an IT student, sooo I can't really answer for any questions :(. Even after reading your article I don't understand anything (cause it's extremely specific aspect of the programming). But I really appreciate nice usage of the memes! And also the monad is a term used in philosophy - it describes the elementary particle of the everything aroud us, the world.
Ok, fair enough xD Functional programming is quite hard even for people we know how to code, so that's nothing to be ashamed of if you do not learn much from this poste. Memes are life <3
Before I answer the questions, I’d like to refer to the introduction of your post in which you said that if we are the students of IT, then we at least should know a little bit of Java. Well, I personally do, but it was only a short adventure during one project which was completely unrelated to our tasks at C#/.NET oriented school. Just saying – we are not all the same.
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.
This comment has been removed by the author.
I'm sorry but I can not agree with you in most points. I'm not sure if you are aware of it, but in "business" world there are a lot of problems to solve, and not every company are OOP only. FP is great to create architecture that will run a multi thread app and will work with really big amount of data. Twitter is the most popular company that is using Scala + FP, so I guess their problems are real. I'm also working with real problems every day so it is not true, that other approach than OOP is bad / unused.

Anna Koca said…
Have you ever tried FP? What are your thoughts of it?

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.
Some things, like doing front end can be made without knowing math, but AI or gave dev? I do not think so. Try FP someday ;)
Andrzej, you know that I'm trying to be a language pluralist, so I'd be happy to talk with you about functional programming.

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.
Nataliya Tkach said…
Have you ever tried FP? What are your thoughts of it?
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.
I have never tried functional programming and I don’t know if I want to try it just because I don’t know much about it. But as you said it has a lot of match in it so I will probably pass because math is not my strong site. I think I tried to program only in object-oriented programming languages, so I don’t have any experience or opinion about any other paradigms. I have never heard of Theory Category. Also, as I said earlier I’m not that good in math so I’m trying to not mix IT with math, whenever it is possible.
1. Yes, I have tried functional programming and this is not for me. Maybe because I do not like math, and theory is hard? I don't know. Never anymore. Object oriented programming paradigms are the best for me. I love them.

2. As above.

3. Never heard of it. And nope, I don't like mixing math with IT. Just pure IT.
Very happy to see someone like you! It's funny how most people think that if you know a little of math/physics then it means you are simple theorist. When you want to solve more complex problems than saving something in database, then sometimes it is good to use right tool for it - and every often it's necessary to use other tan OO paradigm. Also, as I quite said - it's great for everyone ( if you are programmer, of course) to learn something new - even if it's just for fun.
Ok, said to hear that ;( you can learn how to code someday, it's really cool.
You can always try some other than OO just for yourself. You can do it as a training. Cheers!
Sometimes you use math and do not know about it. As I said before - maybe someday try some other paradigms of programming, maybe you will like something different.
1. No, I haven't. It seems like a interesting concept but I just haven't got chance to try it out.

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.
Anton Medvediev said…
1. I have not tried FP.
2. Procedural programming.
3. Yes ,I heard of Theory Category. Yes, i’d like.
Have you ever tried FP? What are your thoughts of it?
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.
Peter Clemenza said…
I can tell you one thing. Python is better. And they lie when they say that 3 billion devices run java. (I wish people stop using Java to make desktop applications so we don't have to install Java. On the other hand using Java on server side is a god send)
I do not want to argue with someone who thinks that one programming language is good for everything. Python is not an exception - there are things that python would be great tool to build, and there are some that python would suck. Also mu article shows code is Scala, not in Java.
Ok, so maybe try FP one day, you may enjoy it.
If you like ML, than you are not probably math affraid. I sugest you that one day while having free time you may try FP.
If you find it interesting, than check it out one day, you might fell in love with it!
Maciej Sadoś said…
1. Have you ever tried FP? What are your thoughts of it?

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.
I have studied Java on my first year here and some time before university. The one thing I struggled the most were generics. I reckon, if you are familiar with monads, and what s more, excited about them, then you probably know what those are. Well, for some odd reason, I cannot comprehend such a topic, and all my projects that included them have failed miserably. Now I work with C++, and occasionally have to deal with template classes. Although, C++ in gamedev is a bit more different, as many aspects of it are handled by game engine and/or functional macros.
Maybe I will answer a little differently than for individual questions. Mainly deals with embedded systems, and we often use mathematics to calculate the values that sensors return or to filter data. FP I tried so much what happened in college.
Adam Nguyen said…
I’ve never tired functional programming before, I think I prefer object oriented programming more.

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
I have tried FP during a brief course in Scala and I find it interesting but besides that course, I have never used it again.
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.
Yana Lytvynenko said…
I had to straddle with Java, since I had a couple of course in academy, where was she the main language. But still, unfortunately, I am not very good at it. So I can not prove my point on this issue. But this approach is very interesting and perhaps in the future I will consider it in more detail.
Many thanks for your post. You said a lot of true things, like we should know when to use specific approach. And as you said, no matter if you like math or not, it's related with coding.
Generics is something different than Monad, but generics also allows you to code with some abstraction ;) cheers!
Do we even have FP @ PJATK? I do not remember any course with it :P
So if you do not like mixing math with coding, then maybe FP is not a best choice. Still, you probably use a lot of math in OOP and do not know about it.
I totally agree with you last sentences. Try FP someday, maybe you will like it.
And what course was it? I've enjoyed prof. Odersky course @ coursera - check it out someday.
Try someday, maybe you will fall in love with it.
I too, am not an IT student. Never tried FP, but no doubt it’s useful. My limited knowledge of programming comes down to trickles of C++ and HTLM so no, no other paradigms. When saying Theory Category, do you mean category theory? Anyway, I’ve heard of it. That’s about it. Great presentation thou, I actually got most of that.
s18716 said…
Have you ever tried FP? What are your thoughts of it?
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.
I have never tries FP language. I am more interested in app development and web development, i think is it not for 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 .
Illia Lukisha said…
Have you ever tried FP? What are your thoughts of it?
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?
1. I never tried but maybe someday if I have more time, I will try to look on this subject a little bit more
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
1. & 2. I must say I've never been big into Monad or anything related to it so this topic does seem pretty crazy to me and I don't have enough experience with FP and OOP to know any advantages or if the alternatives are better.
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.

Popular posts from this blog

Week 12 (12.01-18.01.15) Are you an early bird or a night owl ?

Owls are nocturnal creatures. They’re wide awake at night and they sleep during the day. If this sounds like bliss to you, then, like about 20 percent of the population who find themselves most active at around 9 pm, you may fall into the same category as our feathered friend. Night owls often have difficulty waking up in the morning, and like to be up late at night.  Studies of animal behaviour indicate that being a night owl may actually be built into some people’s genes. This would explain why those late-to-bed, late-to-rise people find it so difficult to change their behaviour. The trouble for night owls is that they just have to be at places such as work and school far too early. This is when the alarm clock becomes the night owl’s most important survival tool. Experts say that one way for a night owl to beat their dependence on their alarm clocks is to sleep with the curtains open. The Theory is that if they do so, the morning sunlight will awaken them gently and natura...

Week 11 [03-09.06.2019] The problem with ecological cars emission in UK

The problem with ecological cars emission in UK Since the adoption of the European Emission Allowance Directive in the European Parliament, all car makers have tried to submit. Since 1992, the Euro I standard has been in force, which limited the emission of carbon monoxide to the atmosphere. The Euro VI standard currently applies, which limits the series of exhaust gases. These include: hydrocarbons, nitrogen and carbon oxides, and dust.   The most significant change was brought by the Euro IV standard. For the first time it introduced the limitation of nitrogen oxides, which are responsible for the harmful compounds of smog.   What is smog?   Smog consists of sulfur oxides, nitrogen and carbon. In addition, solid substances such as suspended dust (PM). Dust suspend in atmospheric aerosols may be in liquid and solid form. These can be particles of sea salt, clouds from the Sahara and artificial compounds made by people. These compounds...

Week 4 [06-12.11.2017] This is what happens when you reply to spam email.

James Veitch is a British comedian. In today’s Ted Talk James with characteristic for himself a sense of humor shows how he deals with spam emails and why responding to junk messages may be sometimes dangerous. Questions: What do you think about James’s  way of dealing with spam? Why are junk messages legal, even though it sometimes may be a fraud? Dou you have a problem with spam? How do you deal with with it?