DEV Community

Iven Marquardt
Iven Marquardt

Posted on • Edited on

1 1

More Type Safe and Descriptive Compositions in an Untyped Setting

Functional programming is about composition. Besides normal function composition there is a rich variety of composition types. Let us take a look at functions of shape <A>(_: A) => Boolean. In math such functions are called predicates and come from logic.

At first sight it looks like we cannot compose predicates. However, they form a monoid, which is an algebraic structure that formally describes the process of combining things. Combining two predicates is the composition we are looking for:

const record = (type, o) =>
  (o[type.name || type] = type.name || type, o);

const Pred = pred => record(Pred, {pred});

const predAppend = tp => tq =>
  Pred(x => tp.pred(x) && tq.pred(x));

const predEmpty = () => Pred(_ => true);

const numLte_ = y => x => x <= y;

const numGte_ = y => x => x >= y;

const isEven = x => (x & 1) === 0;

const numBetween = (x, y) =>
  predAppend(
    Pred(numLte_(y)))
      (Pred(numGte_(x)));

const main = predAppend(
  numBetween(5, 10))
    (Pred(isEven));

main.pred(6); // true
main.pred(7); // false
main.pred(12); // false
Enter fullscreen mode Exit fullscreen mode

run code

Using the monoid we can build arbitrarily complex rule engines out of simple predicates.

Pred seems to be only a simple object wrapper but it adds type safety to our code, because we cannot use predicates in lieu of normal functions anymore or in a scenario, where booleans are meant to be combined with the || operator. Moreover it makes our code more descriptive, because predicates are denoted explicitly.

As functional programmers we do this not only with Pred but also with numerous other types. This has an SYSTEMIC EFFECT on our code, especially for a larger code base. Imagine how type safe and declarative it gets.

Read more on functional programming in Javascript in my online course.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay