Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This library does partial application, not currying #12

Open
timfennis opened this issue Jun 14, 2019 · 1 comment
Open

This library does partial application, not currying #12

timfennis opened this issue Jun 14, 2019 · 1 comment

Comments

@timfennis
Copy link

Like the title says, the examples in the documentation are examples of partial application. Currying is when a function with 4 arguments is turned in to a function with 1 argument that produces a function with 1 argument that produces a function with 1 argument that produces a function with 1 argument that produces the result.

https://en.wikipedia.org/wiki/Currying

@mathiasverraes
Copy link

the examples in the documentation are examples of partial application

Agreed.

Currying is when a function with 4 arguments is turned in to a function with 1 argument that produces a function with 1 argument that produces a function with 1 argument that produces a function with 1 argument that produces the result.

Agreed.

This library does partial application, not currying

I'd say it does both, and it uses the same curry function for both.

As I was writing a test to explore the behaviour, I also discovered that partial application requires an additional () when you pass all the arguments. Not a big deal, but still a bug imho.

<?php

use PHPUnit\Framework\TestCase;
use function Cypress\Curry\curry;

final class CurryTest extends TestCase
{
    /** @test */
    public function curry()
    {
        $f = fn($a, $b, $c) => $a + $b + $c;
        $curried = curry($f);

        $this->assertIsCallable($curried);
        $this->assertIsCallable($curried(1));
        $this->assertIsCallable($curried(1)(2));
        $this->assertIsCallable($curried(1)(2));

        $this->assertEquals(6, $curried(1)(2)(3));
    }

    /** @test */
    public function partial_application()
    {
        $f = fn($a, $b, $c) => $a + $b + $c;

        $this->assertIsCallable(curry($f, 1));
        $this->assertIsCallable(curry($f, 1, 2));

        // I would expect this:
        $this->assertEquals(6, curry($f, 1, 2, 3));

        // But we must add a () at the end, which I feel is a bug:
        $this->assertIsCallable(curry($f, 1, 2, 3));
        $this->assertEquals(6, curry($f, 1, 2, 3)());
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants