Skip to content

Accessor shorthand #10

Description

@klimick
Property fetch
use Fp4PHP\ArrayList;
use function Fp4PHP\Combinator\pipe;
use const Fp4PHP\Combinator\accessor as it;

$persons = [
    new Person(id: 1, name: 'person#1'),
    new Person(id: 2, name: 'person#2'),
    new Person(id: 3, name: 'person#3'),
];

// Shorthand version
$names = pipe($persons, ArrayList\map(it->name));

// Classic version
$names = pipe($persons, ArrayList\map(static fn (Person $p) => $p->name));
ArrayDimFetch
use Fp4PHP\ArrayList;
use function Fp4PHP\Combinator\pipe;
use const Fp4PHP\Combinator\accessor as it;

$persons = [
    ['id' => 1, 'name' => 'person#1'],
    ['id' => 2, 'name' => 'person#2'],
    ['id' => 3, 'name' => 'person#3'],
];

// Shorthand version
$names = pipe($persons, ArrayList\map(it['name']));

// Classic version
$names = pipe($persons, ArrayList\map(static fn (array $p) => $p['name']));
MethodCall
use Fp4PHP\ArrayList;
use function Fp4PHP\Combinator\pipe;
use const Fp4PHP\Combinator\accessor as it;

$persons = [
    new Person(id: 1, name: 'person#1'),
    new Person(id: 2, name: 'person#2'),
    new Person(id: 3, name: 'person#3'),
];

// Shorthand version
$names = pipe(
    $persons,
    ArrayList\map(it->getName()),
);

// Classic version
$names = pipe(
    $persons,
    ArrayList\map(static fn (Person $p) => $p->getName()),
);

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