Lenses can be chained:my $lens = Optical::Bench::BeamSplitter.new; $a =< $lens >= $b; # Think of $a "passing through" $lens into $b. $a = 'foo bar'; is-deeply $b, [ 'foo', 'bar' ]; $b.[1] = 'moo'; is $a, 'foo moo';
If that behavior is a little too magical, use the '<<' and/or '>>', and then the changes only go in one direction, or none at all (but they'll still happen on binding.)my $lens2 = Optical::Bench::BeamSplitter.new('-'); $a =< $lens >=< $lens2 >= $b; $a = 'foo-bar baz-qux'; is-deeply $b, [ [ 'foo', 'bar' ], [ 'baz', 'qux' ] ];
If you're having trouble coming up with a use for this, envision database work, where you have a transform (like, say, converting timestamps) you need to do on the way in and out of the database:$a << $lens >= $b; $a = 'foo bar'; is-deeply $b, [ 'foo', 'bar' ]; # ok 1 $b.[0] = 'moo'; is-deeply $b, [ 'moo', 'bar' ]; is $a, 'foo bar'; # ok + 2, ok 3
Put this anywhere in your method, and you can rest assured that the mySQL date will be formatted correctly when it gets saved. With sufficient black magic, it might even work like:my $lens = O::B::mySQL.new('Mmm dd, yyy'); $created-date =< $lens >= $created-date-mySQL; is $created-date, 'Feb 3, 2016'; is $created-date-mySQL, '2016-02-03T09:50+02:00';
so you could drop that into your Dancer'ish Controller, or maybe even directly in your database Model, once there's an ORM thing for Perl 6.$post.<created-date> =< O::B::mySQL::Date.new >= $params.<created>;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Focussing Haskell into Perl 6
by DrForr (Beadle) on Feb 04, 2016 at 13:24 UTC | |
by Laurent_R (Canon) on Feb 05, 2016 at 18:44 UTC |