Sitelet https://github.com/purescript/purescript-parallel/pull/18
Skip to content

Add parOneOf - #18

Merged
paf31 merged 4 commits into
purescript:masterfrom
natefaubion:par-oneof
Aug 18, 2017
Merged

paf31 merged 4 commits into
purescript:masterfrom
natefaubion:par-oneof

Conversation

@natefaubion

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread src/Control/Parallel.purs Outdated
parOneOf
:: forall a t m f
. Parallel f m
=> Plus f

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it useful to have Alternative here, since we already have Applicative anyway? Seems like the additional laws might be nice to have.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just providing the minimal set of constraints required to get this to compile.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guessed so, but I'm wondering if it's helpful to have the generality. We'd need something which has Plus and Applicative but not Alternative. If we assume Alternative, then we get two extra laws that we can use to think about how this function interoperates with <*>.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the function doesn't take advantage of those laws, then what does it even mean?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, parOneOf [] <*> x = parOneOf [] if you have Alternative, but not necessarily otherwise. Also parOneOf (xs <> ys) <*> x = (parOneOf xs <*> x) <> (parOneOf ys <*> x) if you have Alternative.

@natefaubion natefaubion Aug 17, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, my point was just that this function only takes advantage of Plus, and if anyone using it needs the additional laws, they can request an Alternative constraint. But that's also like saying functions should be using Bind and Applicative instead of Monad, which is silly. So I'll change it 😛

Comment thread src/Control/Parallel.purs
-> m Unit
parSequence_ = parTraverse_ id

parOneOf

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but could you please add a comment?

@natefaubion

Copy link
Copy Markdown
Contributor Author

Addressed feedback and added parOneOfMap.

@kritzcreek

kritzcreek commented Aug 18, 2017 •

Copy link
Copy Markdown
Member

I think these kind of functions could really benefit from examples. Is my intuition right here?

parOneOf 
  [ delay 10 (log "A") *> pure (Just "A")
  , log "B" *> pure Nothing
  ]

> prints B
> prints A
> Just "A"

@natefaubion

Copy link
Copy Markdown
Contributor Author

I think these kind of functions could really benefit from examples. Is my intuition right here?

The reason I'm adding them is so I can have nicer looking examples in the Aff docs.

In any case, the behavior of parOneOf depends on the Alternative instance, so I don't think it's worth while having examples for Aff here. In that specific case, that's not quite right, as the Alternative instance for ParAff is a race, and will kill the losing threads. So it will log "B" and return Nothing, canceling the delay in the other thread.

@paf31

paf31 commented Aug 18, 2017

Copy link
Copy Markdown
Contributor

oneOfMap is merged now, in case you'd like to update this (and the f-t dependency, of course).

@natefaubion

Copy link
Copy Markdown
Contributor Author

Done

Comment thread src/Control/Parallel.purs Outdated
=> Functor t
=> t (m a)
-> m a
parOneOf = sequential <<< oneOf <<< map parallel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oneOf <<< map here too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good catch.

@paf31
paf31 merged commit c2c4692 into purescript:master Aug 18, 2017
@natefaubion

Copy link
Copy Markdown
Contributor Author

FORGOT AN EXPORT HERE TOO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants