8000 Is really necessary specify "No parts of <...> value should be checked"? · Issue #303 · fantasyland/fantasy-land · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Is really necessary specify "No parts of <...> value should be checked"? #303
Open
@xgbuils

Description

@xgbuils

Hi!

After reading #210, I still don't understand why these kind of law are needed. The topic starts talking about "No parts of b return value should be checked. But, finally, it concludes that the implementations that perform this kind of checks are breaking other laws.

For example:

class Maybe {
    static of(val) {
      return new Just(val);
    }
}

class Just extends Maybe {
    constructor(val) {
        super()
        this.val = val;
    }
    map(f) {
        const r = f(this.val)
        return r === null ? new Nothing() : Maybe.of(r);
    }
}

class Nothing extends Maybe {
    map(f) {
        return this;
    }
}

This implementation breaks No parts of f's return value should be checked. However, it is also breaking the composition law:

const f = x => x === 2 ? null : x;
const g = x => x === null ? 2 : x;

Maybe.of(2).map(f).map(g) // Nothing {}
Maybe.of(2).map(x => g(f(x))) // Just {val: 2}

Then, I wonder if No parts of f's return value should be checked is a really necessary law. For me it's a weird law because is talking about the implementation. For example these two implementations of Maybe:

class Just extends Maybe {
    ...
    map(f) {
        const r = f(this.val);
        return Maybe.of(r);
    }
}

and

class Just extends Maybe {
    ...
    map(f) {
        const r = f(this.val)
        return r === null ? Maybe.of(r) : Maybe.of(r);
    }
}

behave the same and are identity and composition law compliant. However, the second breaks the No parts of f's return value should be checked and is not functor lawful. It doesn't make sense for me.

Cheers!

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

      0