Where is an example of a monad? [duplicate] - monads

This question already has answers here:
What is a monad?
(47 answers)
Closed 8 years ago.
I recently have tried to understand what monad is.
Although I watched lots of posts and videos which explains what monad is and what category theory is, because it is too abstract I cant fully understand it. So
Can I have a useful example of monad??

I wrote this post as an example of using the monad abstraction purely as a practical way to avoid code duplication.
(Many things are Monads; what makes Monad useful is writing a function once (in terms of Monad) and then being able to reuse that same function with Future, Option, Either, Writer, State and so on).

Related

public/private in C++ [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am working on a C++ project, where the lead is saying that private/public doesn't matter in practice, and so that all the classes should have only public members. Basically, he wants C++ with struct only.
His main argument is that huge code bases were written in C without private, and no bugs would have been prevented in those code bases by having a private keyword, so why bother?
I am countering by saying that private state is cleaner modularization, that it makes the code easier to reason about, easier to test, easier to refactor, that I could write assertions to make assumptions explicit with private state, that different teams could work independently by just agreeing on the interface of classes, but nothing seems to convince him.
What are the purely technical/objective arguments in this discussion, for/against struct/class with private members? It is a given that there is no generic answer, but I would like to list all the objective arguments I could use to evaluate which one to use in any given situation. Something like reasonable guidelines that we could all agree on, or an analysis grid that would tell us which one to use, given parameters of the situation. I would like to steer clear from personal preferences, as discussion of personal preferences is probably not productive.
It would be particularly useful to see a few concrete examples that demonstrate the pros/cons of each in specific situations.
My answer would be, "No, there is nothing to add." As you said, he has already dismissed good reasons. Maybe he's pleased with himself for recognizing that code can work without any private members, so he's being stubborn. I don't know.
A struct is fine where it makes sense, but people (like me) who have maintained code extensively know that encapsulation does improve design.

What is the seperate implementation interfact class idiom and when do I use it? [duplicate]

This question already has answers here:
Is the PIMPL idiom really used in practice?
(12 answers)
Closed 7 years ago.
I have occasionally come across what I can only describe as the "interface-implementation idiom", where a class is separated into an "interface class" and an "implementation class".
What is the philosophy or reasoning behind this?
What is an example usage of this methodology? What does an example of such a class system look like and why would it be useful to separate a class into an interface and an implementation?
I think what you're talking about is also known as the pImpl pattern. There is lots of discussion about it at Why Should the pImpl idiom be used. That question is also marked as a duplicate of an older question/answer with more information.

Erlang and Elixir [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
SO community, just have a quick question to ask. I've been doing a bit of research on learning languages that have concurrency in mind. For the moment, I've been working with Scala and I've been loving it. I've decided to also learn some others, so as not to specialize in a particular language and instead learn as many as I can. So far, I've taken a keen liking to the EVM, specifically Elixir.
So, my question is this: how beneficial would it be for me to learn Erlang before learning Elixir? I'm very interested in the syntax and such of Elixir, but being built on Erlang, I'm not sure if it would hurt to jump straight into it.
I don't think learning Erlang would be directly useful for learning Elixir; learning what an atom looks like in Erlang isn't going to affect how you learn about atoms in Elixir, for example.
However, there's a lot more to Erlang than just Erlang the language. The obvious one is OTP, which Elixir can make use of. Now, there's no inherent reason that you can't learn OTP within the confines of Elixir, but Erlang's had years to accumulate documentation, tutorials, books, etc on the whole Erlang ecosystem, which is available to Elixir. If you want to learn details of OTP, you might pick up the book Erlang and OTP in Action, but all the code there is Erlang. If you know both Erlang and Elixir, it'd be a lot easier to apply what you've learned from the book to Elixir.
Or think of third-party tools written in Erlang. These are useable by Elixir, but the documentation is very likely going to assume you're using Erlang, so again, knowing Erlang will help you apply what you learn to Elixir.
EDIT by rvirding:
I think you are wrong in your 1st paragraph as everything you would learn in Erlang is directly applicable to Elixir. All the data types are exactly the same and behave in exactly the same way which is what you would expect as they are what the underlying BEAM provides. So an Elixir atom and an Erlang atom are the same and are used in exactly the same way even if the syntax is (slightly) different. You have the same basic control structures and ways of organising code, in functions and modules, as again they are what the underlying BEAM provides. So while Elixir provides some extra high-level constructs underneath they are the same.
This what a bit too big for a comment, hence it ended up here.

Style and Enumerators [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
So, in the process of taking a data-structures class (in C++), my professor wanted us to manipulate a linked list of playing cards. That doesn't matter however, what I am interested in is why she did not use an enumerator to represent the suites.
In her code, she used strings to hold the suite of a card. This seemed inefficient because she wanted us to sort them based on suite, under the circumstances, it would have been considerably easier if she had used an enumerated type instead of a string. The string did not offer any help either, because in printing the suite, she output a Unicode character, roughly doubling the length of her function, simply because she did not use an enum.
Is there any reason for her to have done this, or does she simply have strange preferences when it comes to code style?
If you really want to know what your professor's reasoning is, you have to ask your professor. I can only speculate.
But if I were to speculate, I would guess that there are two possible reasons why your professor chose to use strings as descriptors for these attributes.
She is trying to keep the code simple and easy for newbie C++ programmers to understand. Whether the means meet the goal is debateable.
(Personal bias alert) Professors and others in academia, with no real-world experience, often do and teach things that I would consider to be highly sub-optimal.
My guess would be that she either had not considered that approach or that she wanted to test your ability to work with sorting strings.
Code examples might help in that they might clarify what she did and what you think she should have done.
The likely answer is that she just didn't think about the problem she was using to demonstrate whatever she is trying to teach you. That is, she wanted you to focus on sorting (for example), and probably took some code written by someone else and just adapted it to that purpose without much thought.

Could somebody recommend a book about development of portable C/C++ code? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
The Definitive C++ Book Guide and List
I am faced with the problem of portable code development (C or C++).
Could somebody advise me which book could help in this question?
Edited:
Actually I don't know the types of platforms. I was asked to write a simple program. This program must be portable. I am not expert in this question. I don't understand what it means and what should I do.
Edited
Here is an article devoted to this problem:
http://www.feyrer.de/PGC/Fighting_the_Lemmings.pdf
As I understood, the problem is real.
I'd like to research this question before writing my program.
How 'portable' is portable? DO you mean just Windows/Linux or do you mean it must also port to an 8051 uProc?
The C standard library and C++ are pretty much well supported everywhere. Some C++11 features aren't implemented on all compilers - but the common ones are.
The tricky part comes when you want to talk to anything in the OS or hardware - at which point it might be worth just using something like Qt or WxWidgets - or sticking to Boost.