Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Additions, improvements, and optimizations for StrMap #11

Merged
merged 5 commits into from
Oct 19, 2014

Conversation

dylex
Copy link
Contributor

@dylex dylex commented Oct 11, 2014

It's unfortunate that the foldM implementation can't properly short-circuit, but otherwise is general enough to support a number of other operations. I've added more efficient JS implementations for select functions, and simplified/generalized some already there.

In a separate commit I've added a basic Data.StrMap.ST mutable interface to JS records. This part probably still needs test cases, so could be left off for now.

All changes should be fully backwards-compatible.

toList :: forall a. StrMap a -> [Tuple String a]

union :: forall a. StrMap a -> StrMap a -> StrMap a

unions :: forall a. [StrMap a] -> StrMap a

unsafeIndex :: forall a. StrMap a -> String -> a
Copy link
Contributor

Choose a reason for hiding this comment

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

The current convention is putting such functions in an Unsafe submodule, though the intro of nullary type classes will change that convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like so?

… foldMap, Semigroup instance

Since we know that all StrMap objects have prototype Object, we know
they have no additional enumerable keys, so hasOwnProperty checks are
unnecessary.  Add efficient JS implementations for keys and values.  Add
standard Monoid and Monad fold functions, and use them internally.
Minor other changes to eliminate unnecessary function layers and
variables.  Fix and update tests.

fromList :: forall a. [Tuple String a] -> StrMap a
fromList = foldl (\m (Tuple k v) -> insert k v m) empty
fromList = foldl (\m (Tuple k v) -> _unsafeInsert m k v) (_cloneStrMap empty)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could all these private unsafe helper functions be refactored to use your new ST machinery without affecting performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, some of them, yeah, that's a great idea. Do you have any suggestions for avoiding the module dependency cycle? Put them all in one big module or make an Internal module and re-export?

@jdegoes
Copy link
Contributor

jdegoes commented Oct 12, 2014

Looking pretty good to me (see final comments above), probably @paf31 and / or @garyb should review, too, since they're the original authors.

@garyb
Copy link
Member

garyb commented Oct 12, 2014

This one is all @paf31, I just did some housekeeping. Looks good to me on the whole though.

@paf31
Copy link
Contributor

paf31 commented Oct 12, 2014

@jdegoes If you're happy with all the changes, then it looks good to me, and I'll merge.

Eliminate some duplicate code in _unsafe helper functions.  Move all the
ST functions that involve StrMap out of the ST module to do this, and
make the ST module interface more directly parallel the STArray one
(including an unsafe peek).  It would be nice to have more ST
operations, but it's not clear the best way to provide them safely
without redundant code (the ones that were there before violated Eff
semantics).
@jdegoes
Copy link
Contributor

jdegoes commented Oct 12, 2014

:shipit:

garyb added a commit that referenced this pull request Oct 19, 2014
Additions, improvements, and optimizations for StrMap
@garyb garyb merged commit c81af7c into purescript-deprecated:master Oct 19, 2014
@garyb
Copy link
Member

garyb commented Oct 19, 2014

Thanks 👍, and great avatar @dylex ;) only just noticed the reference.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants