Skip to content

Implement partial function application #113

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

Closed
wants to merge 6 commits into from

Conversation

thekid
Copy link
Member

@thekid thekid commented Jun 27, 2021

Examples

This code:

class Person {
  public function __construct(public int $id, public string $name) { }
}

// Returns an array of three Handle instances
$handles= array_map(fn($_) => new Handle($_), [STDIN, STDOUT, STDERR]);

// Use time() function in production, specialized testing clock for unit tests
$time= $test ? new TestingClock() : 'time';

// Executes HTTP request, then returns a function to read the given input stream
$in= $http->get()->in();
$read= fn() => Streams::readAll($in);

// Creates instances from database records and writes them to the console
Sequence::of($conn->query('select * from person'))
  ->map(fn($_) => new Person(...$_))
  ->each(fn($_) => Console::writeLine('> ', $_))
;

...can be rewritten to:

class Person {
  public function __construct(public int $id, public string $name) { }
  public static function from($record) { return new self(...$record); }
}

// Returns an array of three Handle instances
$handles= array_map(new Handle(?), [STDIN, STDOUT, STDERR]);

// Use time() function in production, specialized testing clock for unit tests
$time= $test ? new TestingClock() : time(...);

// Executes HTTP request, then returns a function to read the given input stream
$read= Streams::readAll($http->get()->in(), ...);

// Creates instances from database records and writes them to the console
Sequence::of($conn->query('select * from person'))
  ->map(Person::from(?))
  ->each(Console::writeLine('> ', ?))
;

See also

Depends on xp-framework/ast#26

@thekid
Copy link
Member Author

thekid commented Jun 27, 2021

If we allowed unpacking of placeholders via ...?, we could further simplify the sequence example:

class Person {
  public function __construct(public int $id, public string $name) { }
}

// Creates instances from database records and writes them to the console
Sequence::of($conn->query('select * from person'))
  ->map(new Person(...?))
  ->each(Console::writeLine('> ', ?))
;

@thekid
Copy link
Member Author

thekid commented Jun 27, 2021

When we pass around objects as parameters, the placeholder would have to be able to replace the object in something like the following:

$filtered= Sequence::of(...)->filter(fn($user) => $user->isActive());
$filtered= Sequence::of(...)->filter(?->isActive());

Hrm...

@thekid
Copy link
Member Author

thekid commented Jul 2, 2021

RFC was declined, closing

@thekid thekid closed this Jul 2, 2021
@thekid thekid deleted the feature/partial-application branch July 2, 2021 20:37
@faizanakram99
Copy link

RFC was declined, closing

it would have been still a nice feature

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

Successfully merging this pull request may close these issues.

2 participants