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%2F20261106%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261106T175304Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=7789152638a21e6234cc8cc81d0bc05f4d26face7403d2991ffe7548803a867e6037f94700dfda857c9e194c30eee4e64406d85c8d8422a7a213538a819ca9438c63a8390628641fa64892665db2a67a2820f526f819efcbe2907bc175698b5ffa24d02c5989c1648602affcfb9fb5fcee7de00d376cbf661ba02f8e1826894aab36d5bab279b62a1a7c780cb7ba4365e0a1417b0fb7cfabde0bf429c4c63bce60d8a0f6f8bcbe567690867faa5c5e3823f56e9001fb93f5857e3a773872df24cb0bbe2e43db5b19d822772b9befde096ecb6e0334c869a5b86ea6909b2c3a23d4300f2a12ffa83c359f39524f87d392d37dd105f337c9cd602dc85337a63700

    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%2F20261106%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20261106T175304Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=11ea2e68511469d1101ae6ce9a896c2810ebb9929e65f6a813240b6486917cf9cb0c7b34a0b73c37a96288d2904075161fef757d76600c68544e54107c5af80b86c82c0e8ee0a2a81469e9d2c25697ad6ffa09131872481c72ede2ff780bb6b37db349c3406beb1c04c3a5c8a06a848cb64fc625f01f3a7fb9b3d91f4520284c864d0aeb10eb02be200a79ca78481f53bb5cee26b5bba9606a896d13779de28433b89363b3348ec2fc5f30325fde6ca61f90616ee9bc8330403fc1c014e6ba4a9827628c051c5929940b7af8d91f3f20afa2e13a0c84ac691e221086d2685bbb547c89fff08abca4578564f71c909ac97ab314af3400b102d706857a466491a3

    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