DEV Community

lray138
lray138

Posted on

Never Say Final.. ha.. the last.. or...

$class_names = wrap($data)
        ->tryProp('page_config')
        ->bind(function (Lst $x) {
            return $x->filter(function ($y) {
                return $y['_type'] === 'header';
            })
                ->tryHead();
        })
        ->bind(function ($x) {
            return $x->tryProp('header_attrs');
        })
        ->bind(function ($x) {
            return $x->filter(function ($y) {
                return $y['_type'] === 'class';
            })
                ->tryHead();
        })
        ->bind(function ($x) {
            return $x->tryProp('class');
        })
        ->getOrElse('');
Enter fullscreen mode Exit fullscreen mode

This is the repeating pattern:

->tryProp('page_config')
        ->bind(function (Lst $x) {
            return $x->filter(function ($y) {
                return $y['_type'] === 'header';
            })
                ->tryHead();
        })
Enter fullscreen mode Exit fullscreen mode

after we create and apply a a prop equals function we have this:

->bind(fn (Lst $x) 
            => $x->filter(propEquals('_type', 'header'))
                ->tryHead())
Enter fullscreen mode Exit fullscreen mode

well, this intrduced the rabbit hole and you can spend yyears trying to find the right way, then AI comes in and it's a whole new ballgame...

    $class_names = wrap($data)
        ->tryProp('page_config')
        ->bind(fn(Lst $x) => $x->filter(propEquals('_type', 'header'))->tryHead())
        ->bind(fn(Kvm $x) => $x->tryProp('header_attrs'))
        ->bind(fn(Lst $x) => $x->filter(propEquals('_type', 'class'))->tryHead())
        ->bind(fn(Kvm $x) => $x->tryProp('class'))
        ->getOrElse('');
Enter fullscreen mode Exit fullscreen mode

I would settle on something like this he above for now... and would lean toward ... you get to the crux with the "tryHead"

Top comments (0)