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%2F20261015%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261015T093120Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=0b3f8a441dce8badafbc793e6772962c05a5f78b0cf9b5d66e68013d390483f582a35a2e87b1848243e6e8fa015014d1da84b5cccd3ea97e5d6217634bd616892c2736c552add55b2d6eb6dc406401ab635ec651991518ffbb2e407cfa4aad362477436e29545c3cdc59cb8e77f99ccd2e579ccce041069b14e6a85c784c03dfa7cc786fd956379a3319dc27972179fa8de143429f2c04e156c89b213264a554b610252bcbef3899bd16501ca98da1139eb9643b41d18795ec947c757613ca08415f995a48e819b23b2d892f65ea5ac7945cd11d27e5e6c7136f41138be25cc05d0aecdcf069c5ed650f0444cdd62dad23d08b842a9ef802e59482296f36f667

    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%2F20261015%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261015T093120Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=245208c382b51e183963fb7fa3b47ffcedaa692b038750bdd01c1479619e0d9c678e75a68e5934394f1b220ea4ab22711572a260eef8dc3b6cdadf3d31d5a03fa860dbefd9959f82fc26a8f44ecf5697d5528f2c3bf78c0f7f0e4f954cb4cf8279eb718574efe1f9634b1c26291854df78207f188c6527e25b5b7956a770f0d60b8ce9fc2ff3bc8d4cb3f5cfee06f8e8538eba244a0acb6f34c07672fc29dab6c5b7c1f146654a2b121df06ee1bf9b925bc4b81788d8ed66fe29067c4c7dd24ddb9aea84d0f632c8a7c6370b251e6c45adf66f0257b2660a5b4eacd888f18224a37e6d0b4b8677d856117fbd923e7a8e5fe474c3d3c80aff497ed582c64d06e7

    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