Skip to content

[#29] Add fromFoldableWithIndex #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

pete-murphy
Copy link
Contributor

@pete-murphy pete-murphy commented Oct 19, 2022

Description of the change

Add fromFoldableWithIndex and tests. Fixes #29.


Checklist:

  • Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
  • Linked any existing issues or proposals that this pull request should close
  • Updated or added relevant documentation
  • Added a test for the contribution (if applicable)

@pete-murphy pete-murphy force-pushed the pm/29/add-from-foldable-with-index branch from 25fa7a3 to c17f1cc Compare October 19, 2022 21:55
Comment on lines 156 to 162
log "fromFoldableWithIndex & key collision"
do
let numsMap = M.fromFoldable [Tuple 0 "zero", Tuple 1 "what", Tuple 1 "one"]
nums = O.fromFoldableWithIndex show numsMap
quickCheck (O.lookup "0" nums == Just "zero" <?> "invalid lookup - 0")
quickCheck (O.lookup "1" nums == Just "one" <?> "invalid lookup - 1")
quickCheck (O.lookup "2" nums == Nothing <?> "invalid lookup - 2")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, this isn't actually testing key collision on the object, since the M.fromFoldable handles that. I'll update.

@@ -221,6 +222,14 @@ fromFoldable l = runST do
ST.foreach (A.fromFoldable l) \(Tuple k v) -> void $ OST.poke k v s
pure s

-- | Create an `Object a` from an indexed foldable collection, using the
-- | specified function for converting the index to `String`
Copy link
Contributor

Choose a reason for hiding this comment

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

This should also state something about key collisions using the later values.

s <- OST.new
forWithIndex_ l \k v -> OST.poke (f k) v s
pure s

Copy link
Contributor

Choose a reason for hiding this comment

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

Thoughts on adding a variant fromFoldableWithIndex' that assumes the k is already a String?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm now thinking that makes sense as the default API, based on the two use cases1 cited in the issue. They both say that Object.fromFoldableWithIndex would be useful specifically in the case of FoldableWithIndex k f ~ Map String.

fromFoldableWithIndex :: forall f a. FoldableWithIndex String f => f a -> Object a

I think non-String keys would probably be the less common case, and in that case you might as well also throw in a function for combining values on conflict (instead of defaulting to overriding earlier values, which could be undesirable)

fromFoldableWithIndexWith :: forall f k v. FoldableWithIndex k f => (k -> String) -> (v -> v -> v) -> f v -> Object v

basically support the (probably) common case with fromFoldableWithIndex and then give more granular control with fromFoldableWithIndexWith (not sure about that naming 😄 )

Footnotes

  1. https://discord.com/channels/864614189094928394/865617619464749081/887326007105814548
    https://discord.com/channels/864614189094928394/867149806178664478/1032345440248668242

Copy link
Member

Choose a reason for hiding this comment

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

I think we can skip fromFoldableWithIndexWith because you could mapKeys (or equivalent) the input first.

It would be more efficient to convert while doing this, but you could say the same about accepting mapping functions in fromFoldables in general if we wanted to start going down that route.

Copy link
Contributor Author

@pete-murphy pete-murphy Oct 21, 2022

Choose a reason for hiding this comment

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

I suppose

fromFoldableWithIndexWith
  :: forall f k a b
   . FoldableWithIndex k f
  => (k -> a -> Tuple String b)
  -> (b -> b -> b)
  -> f a
  -> Object b

would be the most general variation, but I don't know if that's too complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be more efficient to convert while doing this, but you could say the same about accepting mapping functions in fromFoldables in general if we wanted to start going down that route.

Didn't see your comment til after I posted mine, that makes sense to skip fromFoldableWithIndexWith. So would just adding

fromFoldableWithIndex :: forall f a. FoldableWithIndex String f => f a -> Object a

make sense?

Copy link
Contributor

@JordanMartinez JordanMartinez left a comment

Choose a reason for hiding this comment

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

LGTM otherwise.

@pete-murphy pete-murphy force-pushed the pm/29/add-from-foldable-with-index branch from 7d5b8b6 to 489634e Compare October 21, 2022 16:00
Copy link
Member

@garyb garyb left a comment

Choose a reason for hiding this comment

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

I think we keep it simple for now 👍

Can add extra variants if we find we need them later!

@JordanMartinez JordanMartinez merged commit adbc107 into purescript:master Oct 24, 2022
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.

Add fromFoldableWithIndex
3 participants