Skip to content

Implement partial function application - #113

Closed
thekid wants to merge 6 commits into
masterfrom
feature/partial-application
Closed

Implement partial function application#113
thekid wants to merge 6 commits into
masterfrom
feature/partial-application

Conversation

@thekid

@thekidthekid commented Jun 27, 2021

Copy link
Copy Markdown
Member

Examples

This code:

class Person {
publicfunction__construct(publicint$id, publicstring$name) { }
}
// Returns an array of three Handle instances$handles= array_map(fn($_) => newHandle($_), [STDIN, STDOUT, STDERR]);
// Use time() function in production, specialized testing clock for unit tests$time= $test ? newTestingClock() : '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($_) => newPerson(...$_))
->each(fn($_) => Console::writeLine('> ', $_))
;

...can be rewritten to:

class Person {
publicfunction__construct(publicint$id, publicstring$name) { }
publicstaticfunctionfrom($record) { returnnewself(...$record); }
}
// Returns an array of three Handle instances$handles= array_map(newHandle(?), [STDIN, STDOUT, STDERR]);
// Use time() function in production, specialized testing clock for unit tests$time= $test ? newTestingClock() : 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

thekid commented Jun 27, 2021

Copy link
Copy Markdown
MemberAuthor

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

class Person {
publicfunction__construct(publicint$id, publicstring$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
Copy Markdown
MemberAuthor

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
Copy Markdown
MemberAuthor

RFC was declined, closing

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

Copy link
Copy Markdown

RFC was declined, closing

it would have been still a nice feature

Sign up for freeto 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

@thekid@faizanakram99