Skip to content
This repository was archived by the owner on May 29, 2023. It is now read-only.
This repository was archived by the owner on May 29, 2023. It is now read-only.

Have you considered creating interfaces for Algebraic Structures? #10

@jrsinclair

Description

@jrsinclair

This is broader than just Maybe, but I'm not sure if you can create issues for a whole group. Anyhow, I've been working on an implementation of Task, and found myself wanting a liftA2() function. It struck me that there's at least three ways to approach how I declare the function signature:

Option 1: Nailed-down types

function liftA2(callable $f, Task $a1, Task $a2) {
    return $a1->map(function ($a1Val) use ($f) {
        return function($x) use ($a1Val) {
            return $f($a1Val, $x);
        }
    })->ap($a2);
}

Option 2: No type checking

function liftA2(callable $f, $a1, $a2) {
    return $a1->map(function ($a1Val) use ($f) {
        return function($x) use ($a1Val) {
            return $f($a1Val, $x);
        }
    })->ap($a2);
}

Option 3: Interface type checking

function liftA2(callable $f, Functor $a1, Applicative $a2) {
    return $a1->map(function ($a1Val) use ($f) {
        return function($x) use ($a1Val) {
            return $f($a1Val, $x);
        }
    })->ap($a2);
}

Option 1 works, but isn't terribly flexible. Option 2 also works, until someone tries to pass in something without a ->ap() or ->map() method. I could manually check for the given method with reflection, but it seems a pain. Option 3 would mean that PHP did the checking for me, and would allow me to re-use liftA2() across different types. But it would also mean that I have to make sure that all the types I want to use it with implement that interface.

I haven't actually tried it, so I don't know if there's good reasons not to. Is this something you've thought about?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions