Monads are Omnipresent in Rust

Monads, as they occur in functional programming, seem crazy and difficult to understand to me. Above all explanations and references to the mathematical subfield of category theory are confusing to me. In this post, I’m trying to explain a concept that I haven’t penetrated very deeply myself, in an introductory, application-oriented and superficial way, so that you can become the head nerd at the next party of imperative nerds. This is a translation of the original German article in a collaboration between man and machine. ...

December 15, 2023

Parsing Operators in Rust

The Advent of Code is just around the corner. This is an advent calendar of programming tasks, the solution of which can serve as a pastime for the fun of joy. After successfully processing a task, you receive a virtual golden star. My first participation was in 2020. At the time, I thought I’d try to do this directly in the Rust programming language, which I hadn’t used before. By the way, this article is not only intended to shed light on an interesting Advent of Code task, but also to highlight the flexibility of my Rust library Exmex, which is discussed below as part of the solution. ...

November 11, 2021

Sequences of partial reductions in Elm

In my previous article I derived the calculation for passive income from invested assets and demonstrated a small calculator. It struck me that there was a slight delay in the case of high annual numbers. The cause of the delay lies in my implementation of the formula $$ \sum_{k = 1}^{m-1} \frac {1} {\prod_ {t = 1} ^ {k} w_t}. $$ As described in more detail in the article mentioned above, $ w_t $ denotes the relative market value in the month $ t $ and $ m $ the total number of months. ...

June 1, 2021

First steps towards dynamic web apps in Elm

Some time ago I was able to listen to the episode We’re Teaching Functional Programming Wrong of the podcast Corecursive. Richard Feldman tells of a purely functional language with which you can actually create dynamic web pages. It is not even necessary to understand what a Monad is. Since I consider object-oriented programming to be overrated*The talk [Free your Functions](https://www.youtube.com/watch?v=WLDT1lDOsb4) by Klaus Iglberger highlights some practical aspects my point of view., I had a closer look at this approach. In this post I will report on my first steps, hurdles and progress in Elm and focus on aspects that were unfamiliar to me having an imperative background. ...

April 19, 2021

Modular Image Processing Algorithms Revisited - Functional Style

In the blog post from August 2014 we compared the modularization of image processing algorithms with template based policies against the dynamic polymophism based stragety pattern in terms of speed. We will compare the run-time of (almost) the same pixel-wise operations on images again. But this time, we consider different functional modularization techniques. More precisely, we will pass function pointers, lambdas, and derived functors to algorithms with corresponding argument types such as a templated one and std::function among others. ...

June 18, 2017

Strategy vs. Policy for Image Processing Algorithms

I was wondering for quite a while how much speed-up one can roughly expect by a policy*Modern C++ Design, A. Alexandrescu, 2001 based design of algorithms in C++ using templates compared to the strategy*Design Patterns, E. Gamma, R. Helm, R. Johnson, and J. Vlissides, 1995 pattern using polymorphism. Thereby, I am especially interested in algorithms for image processing. Strategy vs. Policy in C++ Both, policy and strategy, can be used to modularize algorithms. For more details I refer to the textbooks listed below. To compare both approaches exclusively in terms of speed, I created a simple example. I consider the binary operations addition and subtraction of two images pixel-wise and in place for all image pixels i. For the polymorphism based strategy pattern I use an abstract super class. ```cpp template struct BinaryOperator { virtual t_data sBinOperation(t_data a, t_data b) = 0; }; template struct Add : BinaryOperator { t_data sBinOperation(t_data a, t_data b) { return a+b; } }; template struct Subtract : BinaryOperator { t_data sBinOperation(t_data a, t_data b) { return a-b; } }; Further, I have a class that iterates over the image and applies the binary operation pixel-wise. The binary operation is contained as a polymorphic member. ```cpp template <class t_data> struct ImBinaryOperatorStrategy { BinaryOperator<t_data> *binOp; ImBinaryOperatorStrategy(BinaryOperator<t_data> *binOp):binOp(binOp){} void imBinOperation(t_data* im1, t_data* im2, int width, int height, int step1, int step2) { for (int y=0; y<height; y++) for (int x=0; x<width; x++) im1[x+y*step1] = binOp->sBinOperation(im1[x+y*step1],im2[x+y*step2]); } }; For the policy based design with templates, I use the classes Add and Subtract (but not their base class BinaryOperator) and the following class to operate on images. In contrast to the strategy pattern, the binary operation is not a member but passed as template parameter. ...

August 29, 2014