Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Written by: Arun Pandian MPublished on: Mar 23, 2026

Products — When Two Independent Things Become One Structure

In programming, we combine values all the time.

But rarely do we ask a deeper question:

Is there a correct way to combine things?

Not just a convenient way. Not just a common pattern. But a way that is universally right.

Category theory answers this using something called a universal construction. Instead of defining a structure directly, it defines it by how it relates to everything else.

And when we ask:

“What is the best way to combine two things?”

This perspective leads us to a fundamental idea:

The Product.

A product is not just a pair of values. It is the *most universal* way of combining them.

To make this concrete, consider something simple:

Latitude and longitude. Individually, they are just numbers. But when combined correctly, they represent a precise location on Earth. And when combined incorrectly… everything breaks.

So what does it mean to combine them correctly?

That’s exactly what the idea of a Product captures.

The Mathematical Idea

In category theory, a product of A and B is:

An object (A, B) with two projections:
π₁ : (A, B) → A  
π₂ : (A, B) → B

These just mean:

  • take first value
  • take second value
  • The Pattern (What really matters)

    We don’t define product by structure.

    We define it by behavior:

          c
         / \
        p   q
       /     \
      A       B
    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/product.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260624%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260624T111512Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=52f422de91345564fcd12fa56d97cc0fd01ab3a4d197b8e30a2939c216aac6ba49692bd09836abc82ccae3ddd6688175a224051b84cf99d83a46af610cb80712add9bd25945f5263d6026c9f3d86615134d50c532ef46c183829e2735926855274ce7b1c8aa9689931509761eb1b1119c04bd2c5f6b0df07b2d3a0e8e1abf7e4b3ec3370a14cbcdf087ecfa243746f079cfdf31443d61655efda3f173c8a7433e42e8f0182b2ce9beb36a182aafe266e72a9a53302303e73065ab88526ee706a089148c8bd318e349fa3af508c702fb6db94ad7d3c373a69736813f0a772743bbefea6c75a34a4086f390e28d8eebdc73d3aa7f87603229148d14bc88675c4d4

    Key Idea

    If you have:

    p : C → A  
    q : C → B

    Then there must exist a unique function:

    m : C → (A, B)

    Factorizer (The Heart of Product)

    This function is called the factorizer:

    m(x) = (p(x), q(x))

    Kotlin Implementation

    fun <C, A, B> factorizer(
        p: (C) -> A,
        q: (C) -> B
    ): (C) -> Pair<A, B> = { x ->
        Pair(p(x), q(x))
    }

    Real Example — Latitude & Longitude

    Domain

    data class Location(
        val latitude: Double,
        val longitude: Double
    )

    Projections

    val getLatitude: (Location) -> Double = { it.latitude }
    val getLongitude: (Location) -> Double = { it.longitude }

    Type matches, meaning is wrong(Compiles!)

    val wrong = { loc: Location ->
        Pair(getLatitude(loc), getLatitude(loc)) // bug
    }

    Output:

    (12.97, 12.97)

    Here Longitude lost, Type system didn’t help

    Correct (Factorizer)

    val correct = factorizer(getLatitude, getLongitude)
    
    println(correct(Location(12.97, 77.59)))
    // (12.97, 77.59)

    Why Factorizer Matters

    It enforces laws:

    fst(m(x)) = p(x)  
    snd(m(x)) = q(x)

    Any function that breaks this is not a product

    Without ProductWith Product
    Many ways to combineOnly ONE valid way
    Easy bugsGuaranteed correctness
    No structureMathematical guarantee

    Real Use Case — Parallel Data Fetch

    A screen needs to show user info along with their posts.

    Define Combine

    fun <C, A, B> combine(
        f: (C) -> A,
        g: (C) -> B
    ): (C) -> Pair<A, B> = { x ->
        Pair(f(x), g(x))
    }

    Use combine

    val fetchAll = combine(
        ::fetchUser,
        ::fetchPosts
    )
    
    val result = fetchAll(userId)

    We are combining two independent computations into one.

    Parallel Version

    fun <C, A, B> combineAsync(
        f: suspend (C) -> A,
        g: suspend (C) -> B
    ): suspend (C) -> Pair<A, B> = { x ->
        coroutineScope {
            val a = async { f(x) }
            val b = async { g(x) }
            Pair(a.await(), b.await())
        }
    }
    val fetchAll = combineAsync(
        ::fetchUser,
        ::fetchPosts
    )
    
    val result = fetchAll(userId)

    When values are independent but needed together, they form a Product.

    https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/product_factorization.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260624%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260624T111512Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=39c2988882be2ad8e639da4d253e45828bb288505acf414d7a23fde0b6db70b7ad30833cdf959fb92f0b0d08f7635c0c369693d36ed34ed885ad7c2bae61aa1263dabd89737064ce058ea3256c5174d1ca853ec673c152c6a7009bef7d1a00dd8db2e518cba11109e09db9117fa9ed5b19cebf2fa4a10543f88bfcecdb776bd9a851e617fb6e066f9f379601fa3ee49cf55e0b0c44eb5d13179dcbdff6526ab9fe778714f10bf595b3083b2e56fc3b1fe6e0b7faefce32e6fc7b36283a14c81c55e34e0352b566c6c06d4161581aaf9ed1db2f69720e0378de552cd5b2eda4c14651ad26bdfc4cc72c8437360d282a68fcfc9231160f63d172d99f482c8be77b

    Final Takeaway

    A product is not just a pair.

    It is:

    A structure where every valid combination must pass through one unique path

    One-line Conclusion

    Product is not about combining values — it’s about guaranteeing the only correct way to combine them
    #MathForDevelopers#FunctionalProgramming#BuildInPublic#CategoryTheory#FPFoundations#ProgrammingConcepts#EngineeringMindset#KotlinFP#SoftwareDesign#LearnInPublic#TypeTheory#ComputerScience#TheoreticalCS#ProductType#DataModeling
    LAMBDA BRICKS