python-effect / effect

effect isolation in Python, to facilitate more purely functional code
372 stars 16 forks source link

Add Sequence intent. #46

Closed tomprince closed 9 years ago

radix commented 9 years ago

Hey Tom, glad to see you're still interested in Effect :)

I can see how this offers something beyond manually chaining effects -- managing the list of results is pretty tedious in Python, but I have two questions:

  1. practically, when do you want something like this?
  2. do you understand foldM? I haven't grokked it myself, but I keep getting the feeling that maybe there is some useful combination of Effects with folds that is just beyond my understanding, and this is one of the situations that trigger that feeling. I haven't yet had the time to sit down and think about it for the requisite while, though.
radix commented 9 years ago

to @radix, for future reference: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.50.2961

tomprince commented 9 years ago
  1. For the use case I have (https://github.com/ClusterHQ/flocker/pull/1267) I don't actually need the result. But I just need to run a bunch of things (in this case, remote SSH commands) in sequence.
  2. Well, this is just sequence. Haskell's foldl is just python's reduce; foldM is just the case where the reducing function has side-effects. I think sequence can be expressed in terms of foldM
sequence = foldM (\x y -> return (x ++ [y])) []

but I don't think that that is simpler.

radix commented 9 years ago

@tomprince

Could you check out #51 and #52?

I think this is a nicer formulation. Basically I just split up the "folding over effects" part of your PR from the "accumulating into a list" part. The error-handling is generalized to all folds, such that it raises a FoldError with the accumulator-so-far (not necessarily a list) and the original exception. I've also kept the optimization of appending to a list instead of concatenating lists.

I also dropped the intent. I can't think of a good reason to have an intent for it because tests would still just need to assert that each effect is performed in turn (usually with SequenceDispatcher).

radix commented 9 years ago

closing since #52 was merged. Thanks for the contribution.