asm89 / php-try Goto Github PK
View Code? Open in Web Editor NEWTry type for PHP
License: MIT License
Try type for PHP
License: MIT License
I've some suggestions for the API after trying this a bit. Basically, I'm wondering what you think about something like this:
Attempt::call($someCallable)
->forAll($onSuccess)
->always($always) // Finally semantics (called regardless of success/failure)
->throwIfFailed()
;
This would be equivalent to:
try {
$rs = $someCallable();
$onSuccess($rs);
} finally {
$always();
}
Not sure if this is already supported in another way, but I haven't found anything. What do you think?
I looked at the source code, but I don't currently understand the difference between flatMap and map? Why do you have both? Don't they both end up using attempt?
I read somewhere regarding Scala that flatMap is basically map plus concatenation of the results, whereas map is just function on each element of the sequence. But your examples of flatMap doesn't show how a sequence comes into to play and why it needs to be concatenated, since the result of divide is single value, not a sequence.
I'd like to suggest to add a forAll()
method. The semantics of this are similar to the map()
method except that you pass a callable that has side-effects (modifies something outside its scope by reference for example).
$items = array();
Attempt::call($callable)->forAll(function($v) use (&$items) { $items[] = $v[0]; });
scala has a similar method (foreach
- we cannot use that name in PHP); scala has also a forAll()
method, but that has different semantics. I've gone with forAll()
for the Option type, but maybe you want to use something else.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.