절대 최종이라고 말하지 마세요.. 하.. 마지막.. 혹은...

발행: (2026년 4월 23일 PM 11:21 GMT+9)
1 분 소요
원문: Dev.to

Source: Dev.to

원본 코드

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

반복되는 패턴

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

propEquals를 사용한 리팩터링

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

최종 리팩터링 버전

$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 조회
Back to Blog

관련 글

더 보기 »

Show Dev: 클릭할 수 없는 Idle Tycoon 게임

내가 지금까지 출시한 가장 어리석은 게임은 클릭해도 아무 일도 일어나지 않는 타이쿤 게임을 만든 것이다. 업그레이드 버튼도 없고, 프레스티지 루프도 없으며, “슬랩 더 쿠키” 도파도 없다.