Skip to content

Add partitionWith function#321

Merged
chshersh merged 2 commits intokowainik:masterfrom
rektrex:add_partitionWith
Aug 16, 2020
Merged

Add partitionWith function#321
chshersh merged 2 commits intokowainik:masterfrom
rektrex:add_partitionWith

Conversation

@rektrex
Copy link
Copy Markdown
Collaborator

@rektrex rektrex commented Aug 13, 2020

Resolves #319

Checklist:

HLint

  • I've changed the exposed interface (add new reexports, remove reexports, rename reexported things, etc.).
    • I've updated hlint.dhall accordingly to my changes (add new rules for the new imports, remove old ones, when they are outdated, etc.).
    • I've generated the new .hlint.yaml file (see this instructions).

General

  • I've updated the CHANGELOG with the short description of my latest changes.
  • All new and existing tests pass.
  • I keep the code style used in the files I've changed (see style-guide for more details).
  • I've used the stylish-haskell file.
  • My change requires the documentation updates.
    • I've updated the documentation accordingly.
  • I've added the [ci skip] text to the docs-only related commit's name.

Copy link
Copy Markdown
Contributor

@hint-man hint-man bot left a comment

Choose a reason for hiding this comment

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

There is no place for me here... I will choose the truth I like.

Copy link
Copy Markdown
Contributor

@chshersh chshersh left a comment

Choose a reason for hiding this comment

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

Nice start! I have a few suggestions on how to improve the code and documentation, but overall a good addition 👍

([2,4,6,8,10],[1,3,5,7,9])
-}
partitionWith :: (a -> Either b c) -> [a] -> ([b], [c])
partitionWith = (partitionEithers .) . map
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.

Let's write slightly more pointful implementation. It will be more readable and maintainable in long-term.

Suggested change
partitionWith = (partitionEithers .) . map
partitionWith f = partitionEithers . map f

:}

>>> partitionWith foo [1 .. 10]
([2,4,6,8,10],[1,3,5,7,9])
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.

Let's add @since annotation to new function, since we already planned the milestone for the new version.

Suggested change
([2,4,6,8,10],[1,3,5,7,9])
([2,4,6,8,10],[1,3,5,7,9])
@since 0.8.0.0

Comment on lines +68 to +70
foo x
| even x = Left x
| otherwise = Right x
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.

Let's change example a little bit. To understand better, why partitionEithers might be useful, it's better to give an example where return types are different. For example, I propose to implement the following function:

divideEvenOrShow :: Int -> Either Int String
divideEvenOrShow n
    | even n = Left $ n `div` 2
    | otherwise = Right $ "Odd: " <> show n

Copy link
Copy Markdown
Contributor

@chshersh chshersh left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

@chshersh chshersh added the new Bring something new into library (add function or type or interface) label Aug 16, 2020
@chshersh chshersh merged commit 9047f09 into kowainik:master Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new Bring something new into library (add function or type or interface)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add 'partitionWith'

2 participants