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

Published: (April 23, 2026 at 10:21 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Original Code

$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('');

Repeating Pattern

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

Refactored Using propEquals

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

Final Refactored Version

$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('');
0 views
Back to Blog

Related posts

Read more »

Introduction to Solana (Web 3)

What is a Blockchain A blockchain is a decentralized, distributed, and typically immutable ledger that is transparent on a public network, allowing anyone to v...