Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Written by: Arun Pandian M•Published 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%2F20261129%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261129T123208Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=13c03cd6bbea6041ac0688f11c80a132abeb62b654d917ebd5b1babd2dfe3638a4a8a77d4ef27d122275fea098e096c013e5bf98b01f8272a0ebd4e56ac290f4b419d7c681e92db7b8c65bac62a3ed49d013e8de16af097ce1b267f383473a2aa35ed7cc3e6fd0aee8415cebc34de586600e091c6d1668734b349418ef98e9e2cc41c5feeef01be499ccfd7c0392eb0f567048ee0b56e31e35f9c12d87f699b8aaf294e928ce233f45c7288f2035965b244551f89015051f795187ccdb1deecda0732973eff1dadb1be33867ea21f5e24916cea33328ee88a7b197b7fcba1b703b5397f72b32c43bebced887fbf9ba85e2e2fae6905ac78cf864e36b7111ae58

    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%2F20261129%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261129T123208Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=80269fb995230e0ad260e5c05de9d099799a9baf096642744001667e2d9318fb89f14e07619d31a8711cc1924f79413c683e0590f0b750dc63eb21d682458de320ba5755e4f1d6e23c8245bdc53783e4d9b2d165b99e3be7f3e04217d56046322d298ffcd2e19d0296f1b4e00def92bb58fdd8ef78db8fdb8fd91c963df3f1b3e5e6bc8e8419871dc8f6254696e4bb1d2dc7d4de03138e4ccc443420b2a2e584507c736d55c95e88ffd0fb90758ab45505a2f5d2f84a9bead357aaf5cdfa165aa6eb99c5d748999764ec3a0a156df908878394b6ded33a2aa6bebd495c9ea6e2f587b8d0273da014e99615564e7b0e67aba9c0a05475c0ce72c09a2f22eefbdf

    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