DEV Community

lray138
lray138

Posted on

Whoopsies (aka semantic intent)

I caught myself about to make a mistake:

  $t = tryCarbonPostMeta("options", $p->ID)
                ->bind(function(Lst $l) {
                    return $l->filter(function(array $k) {
                        return $k['price'] !== '';
                    })
                    ->tryHead();
                })
                ->bind(function(Kvm $options) use ($p) {
                    dump($options);
                    die;
                    return tryPartial("/site/menu-item-priced-options.php", [
                        "post" => $p
                    ]);
                });
Enter fullscreen mode Exit fullscreen mode

tryHead did allow me a valid way to use bind here, but I realized, I was locking myself into one option.

blah blah blah

the finished chain looks like this:

return tryCarbonPostMeta("options", $p->ID)
                ->bind(function(Lst $l) use ($data) {
                    return $l->filter(function(array $k) {
                        return $k['price'] !== '';
                    })
                    ->pipe(function(Lst $l) use ($data) {
                        return $l->tryHead()
                            ->fold(function() use ($data) {
                                return tryPartial("/site/partials/menu-item.php", $data);
                            }, function(Kvm $x) use ($data, &$l){
                                return tryPartial("/site/partials/menu-item-priced-options.php", wrap($data)
                                    ->tap(function($x) use (&$l) {
                                        if($x->prop('price')->get() !== '') {
                                            $l = $l->unshift([
                                                'description' => $x->prop('desc')->get(),
                                                'price' => $x->prop('price')->get()
                                            ]);
                                        }
                                        return $x;
                                    })
                                    ->set('optionsHtml', 
                                        $l->map(fn($x) => tryPartial("/site/partials/item-option.php", $x)->getOrElse(Str::of('')))
                                        ->join(tryPartial("/site/partials/or-line.php", [])->getOrElse(Str::of('')))
                                    )->get()

                                );
                            });
                    });
                })
                ->getOrElse(Str::of(''))
                ;
Enter fullscreen mode Exit fullscreen mode

I would say this chunk would be the end of the line of the functional journey I started many years ago...

culminating in tap vs. pipe, map vs bind, etc...

weeeee.....

Top comments (0)