Coder Social home page Coder Social logo

php-try's People

Contributors

asm89 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

php-try's Issues

Some API suggestions

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?

flatMap vs map

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.

Consider adding a forAll() method

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.