Skip to content

Instantly share code, notes, and snippets.

@jeremyroman
Created December 15, 2011 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyroman/1479694 to your computer and use it in GitHub Desktop.
Save jeremyroman/1479694 to your computer and use it in GitHub Desktop.
$ g++ -c monads.cc
monads.cc: In member function ‘lvector<B> Monad<lvector>::bind(lvector<A>, Function)’:
monads.cc:24: error: expected `;' before ‘it_a’
monads.cc:26: error: ‘it_a’ was not declared in this scope
monads.cc:28: error: expected `;' before ‘it_b’
monads.cc:28: error: ‘it_b’ was not declared in this scope
#include <string>
#include <vector>
template <template <typename> class M>
class Monad {
template <typename A, typename B, typename Function>
M<B> bind(M<A> in, Function function);
template <typename T>
M<T> m_return(T data);
template <typename T>
M<T> fail(std::string message);
};
template <typename T>
class lvector : public std::vector<T> {
};
template <>
class Monad<lvector> {
template <typename A, typename B, typename Function>
lvector<B> bind(lvector<A> in, Function function) {
lvector<B> result;
for (lvector<A>::iterator it_a = in.begin(); it_a != in.end(); ++it_a) {
lvector<B> partial_result = function(*it_a);
for (lvector<B>::iterator it_b = partial_result.begin(); it_b != partial_result.end(); ++it_b) {
result.push_back(*it_b);
}
}
return result;
}
template <typename T>
lvector<T> m_return(T data) {
lvector<T> result;
result.push_back(data);
return result;
}
template <typename T>
lvector<T> fail(std::string message) {
return lvector<T>();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment