Skip to content
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

Add S.fromPairs #454

Merged
merged 1 commit into from
Nov 1, 2017
Merged

Add S.fromPairs #454

merged 1 commit into from
Nov 1, 2017

Conversation

futpib
Copy link
Contributor

@futpib futpib commented Oct 31, 2017

No description provided.

@futpib futpib mentioned this pull request Oct 31, 2017
32 tasks
@davidchambers
Copy link
Member

Looks very good! I'll leave a few comments, but there's not much to fault here. :)

index.js Outdated
//.
//. ```javascript
//. > S.fromPairs([['a', 1], ['b', 2], ['c', 3]])
//. {b: 2, a: 1, c: 3}
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to write {b: 2, a: 1, c: 3} rather than {a: 1, b: 2, c: 3}?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy-pasted from S.pairs. I guess it's a hint on the arbitrary order of things? Should we: leave it as is, sort only fromPairs, or sort both pairs and fromPairs examples?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, wait, I see that S.pairs example is sorted explicitly with .sort(). I'll just sort the keys in fromPairs example then.

Copy link
Member

Choose a reason for hiding this comment

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

👍

index.js Outdated
//. {b: 2, a: 1, c: 3}
//. ```
function fromPairs(pairs) {
return Z.reduce(function(m, pair) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to name the first parameter strMap.

index.js Outdated
@@ -3778,6 +3778,24 @@
S.pairs =
def('pairs', {}, [$.StrMap(a), $.Array($.Pair($.String, a))], pairs);

//# fromPairs :: Array (Pair String a) -> StrMap a
//.
//. Returns a new string map formed by a list of key-value pairs.
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 this is a slight improvement:

Returns a string map containing the key–value pairs specified by the given array.

Note: Please use an en dash in key–value pairs. :)

eq(S.fromPairs([['a', 1], ['b', 2], ['c', 3]]), {a: 1, b: 2, c: 3});

eq(S.fromPairs([['x', 1], ['x', 2]]), {x: 2});
});
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Please delete line 16 and add an empty line between lines 17 and 18.

@davidchambers
Copy link
Member

This looks great to me! Please squash the commits. I'll leave this pull request open for a couple of days to give other Sanctuary contributors an opportunity to comment.


eq(S.fromPairs([]), {});
eq(S.fromPairs([['a', 1], ['b', 2], ['c', 3]]), {a: 1, b: 2, c: 3});
eq(S.fromPairs([['x', 1], ['x', 2]]), {x: 2});
Copy link
Member

Choose a reason for hiding this comment

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

I just realized that this would also make an excellent doctest. It neatly demonstrates the second sentence of the description. Would you mind adding this to the ```javascript block as well?

index.js Outdated
}, {}, pairs);
}
S.fromPairs =
def('fromPairs', {}, [$.Array($.Pair($.String, a)), $.StrMap(a)], fromPairs);
Copy link
Member

Choose a reason for hiding this comment

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

Since the function uses Z.reduce, maybe this signature could be expanded to:

Foldable f => f (Pair String a) -> StrMap a

Copy link
Member

Choose a reason for hiding this comment

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

Good idea! Let's replace array with [Foldable][] in the description while we're at it. :)

index.js Outdated
def('fromPairs',
{f: [Z.Foldable]},
[f($.Pair($.String, a)), $.StrMap(a)],
fromPairs);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Please indent the three lines above so that all four arguments to def are aligned. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider this #457

index.js Outdated
//# fromPairs :: Foldable f => f (Pair String a) -> StrMap a
//.
//. Returns a string map containing the key–value pairs specified by
//. the given [Foldable][]. If a key appears in multiple pairs, the rightmost
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Let's make the lengths of the two lines above more even by moving the first word of the second line to the end of the first line. :)

@davidchambers
Copy link
Member

The changes look good, @futpib! I'll merge this pull request as soon as you've made the two minor formatting changes.

@futpib
Copy link
Contributor Author

futpib commented Nov 1, 2017

I really suspect you are kidding me at this point.

@davidchambers
Copy link
Member

Thank you for your patience, @futpib. 😄

@davidchambers davidchambers merged commit 16001f5 into sanctuary-js:master Nov 1, 2017
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