From Mathematics to Generic Programming

Category: Programming
Author: Alexander A. Stepanov, Daniel E. Rose
This Month Hacker News 1


by Iwan-Zotow   2019-01-27
It is not about distaste, though idea about keeping and carrying 1 around is a recipe for bugs. It is all about composability.

Could you make proper monoid out of Julia ranges? What would be a unit in that monoid? Could you tell me how empty range looks in Julia (in Python/C++ it looks trivial)?

> Python cannot even index lists by ranges (or other lists)

sure, Python has deficiencies as well.

C++ STL iterators/ranges are made in the same way, and for a good reason - composability first. It described very well in Alex Stepanov book (, as well as how it helps when we move to parallel algorithms (

It is even more explicit in upcoming Ranges library in C++20

by Suncho   2018-11-10
Yes! Alexander Stepanov is fantastic. He's the designer of the Standard Template Library (STL) for C++, which is C++'s core library of generic algorithms. It's part of the standard libary.

If you can understand how to use the STL properly, you're well on your way to becoming a profiicent C++ programmer.

His books are fantastic too.

Elements of Programming:

and From Mathematics to Generic Programming:

If you prefer video lectures, his second book is based off his lecture series Four (three) Algorithmic Journeys:

It's a combination of history and math/algorithms and programming (using C++). If you're just looking for a straight intro to C++ course this isn't it. But it's a ton of fun and the generic programming/STL mindset is very powerful.

Programming Conversations is another great lecture series by Alexander Stepanov:

I would also recommend searching YouTube for videos by Sean Parent. This one in particular is very enlightening:

Sean Parent is very good at getting you in the STL mindset and showing off the expressive power of using the standard STL algorithms in your code. Except in the simplest cases, you should try to use them instead of writing your own loops.

Here are some random tips if you're coming from C or Java:

"new" is not the way to create objects in C++. new and delete should almost never be used by serious programmers in C++. Never use new and delete (or malloc and free). If you want a dynamically-allocated array, use the standard vector.

It's much easier to write C++ than it is to read it. This is because you can always write using a simple clear subset of C++ that you understand. Try to pick a style that you think as many people as possible will understand. Programming languages are for humans to read. I try to write code that I think C programmers can read.

Edit -- Another tip:

Exception safety isn't about C++'s exception-handling language feature. Exceptions still happen in C. There's just not a language feature that directly expresses them.

Writing exception-safe code is nearly impossible in C. RAII and C++'s built-in exceptions make it possible. If you're careful to always use RAII by default, you can get the basic exception safety guarantee automatically without thinking about it. And you can get the strong exception safety guarantee whenever you need it.

Good luck!