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.append #184

Merged
merged 1 commit into from
Apr 28, 2016
Merged

Conversation

gilligan
Copy link
Contributor

This adds S.append as suggested by #145

So this is my first contribution and I just naively implemented it as a -> [a] -> [a].

@svozza
Copy link
Member

svozza commented Apr 19, 2016

We could restrict a to Semigroup, perhaps?

@davidchambers
Copy link
Member

Thanks, @gilligan!

Let's move the code and tests to the List section. It'd feel at home between dropLast and find, I'd say.

def('append',
{},
[a, $.Array(a), $.Array(a)],
function(x, xs) { return xs.concat(x); });
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 need to wrap x here, currently we have S.append([3, 4], [[1], [2]]); //=> [[1], [2], 3, 4]

Copy link
Member

Choose a reason for hiding this comment

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

Let's use xs.concat([x]), to handle nested arrays correctly:

> [1, 2].concat(3)
[1, 2, 3]

> [1, 2].concat([3])
[1, 2, 3]

> [[1], [2]].concat([3])
[[1], [2], 3]

> [[1], [2]].concat([[3]])
[[1], [2], [3]]

Let's also include a test case to cover this.

Copy link
Member

Choose a reason for hiding this comment

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

Beaten to the punch! Good spotting, Kevin. :)

@gilligan
Copy link
Contributor Author

@svozza I guess semigroup could make sense yes.
@davidchambers sure, will update the PR tomorrow.

@davidchambers
Copy link
Member

davidchambers commented Apr 19, 2016

We could restrict a to Semigroup, perhaps?

I don't think so, Stefano. The function should work forall a. The container must support concat, of course, but that's ensured by our use of $.Array.

@gilligan
Copy link
Contributor Author

@davidchambers right.. so S.append(() => {}, []) should of course work as well.

@svozza
Copy link
Member

svozza commented Apr 19, 2016

I don't think so, Stefano. The function should work forall a. The container must support concat, of course, but that's ensured by our use of $.Array.

Do we not want this to work on strings too?

@davidchambers
Copy link
Member

Do we not want this to work on strings too?

I'm not sure this is necessary: S.concat('foo', '!') behaves as S.append('!', 'foo') presumably would. One thing that worries me about supporting strings here is that S.append('!') would do different things depending on the type of the second argument:

> S.append('!', 'abc')
'abc!'

> S.append('!', ['a', 'b', 'c'])
['a', 'b', 'c', '!']

There's also the question of whether S.append('bar', 'foo') is a type error, since it'd append more than one character to 'foo'. Deferring to S.concat is a reasonable solution to these conundrums.

@gilligan gilligan force-pushed the add-append-function branch 3 times, most recently from 0504a2d to c6ed504 Compare April 20, 2016 17:48
@gilligan
Copy link
Contributor Author

@davidchambers

  • changed to concat([x])
  • added unit test / doc test to append list to list of lists
  • moved implementation list section
  • moved test to list section

anything else?

@@ -3032,6 +3065,8 @@
[$.Array($.String), $.String],
compose(R.join(''), R.map(concat(_, '\n'))));



Copy link
Member

Choose a reason for hiding this comment

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

👾

@gilligan gilligan force-pushed the add-append-function branch 7 times, most recently from 6da9370 to 3002ecb Compare April 25, 2016 19:06
//# append :: a -> [a] -> [a]
//.
//. Takes a value of any type and a list of values of that type, and
//. returns the result of appending the value to the list.
Copy link
Member

Choose a reason for hiding this comment

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

In light of #180, we need to make a few changes to preserve consistency:

  • change the signature to a -> Array a -> Array a;
  • s/a list/an array/;
  • s/the list/the array/; and
  • move the function to the Array section (at the very beginning, let's say).

Much has changed in the few days since you submitted this pull request. I realize I've already asked you to move this function once! There won't usually be this much friction, I promise. :)

@davidchambers
Copy link
Member

It looks as though your tests disappeared in the rebase!

@gilligan gilligan force-pushed the add-append-function branch 2 times, most recently from ebdfd02 to 2804380 Compare April 26, 2016 08:08
@gilligan
Copy link
Contributor Author

@davidchambers ugh.. tests added again. This whole PR took much longer than I would have anticipated ;)

@@ -2274,6 +2274,7 @@
return n < 0 || negativeZero(n) ? Nothing() : slice(0, -n, xs);
});


Copy link
Member

Choose a reason for hiding this comment

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

Is this newline intentional?

@gilligan gilligan force-pushed the add-append-function branch from 2804380 to f913cc1 Compare April 26, 2016 10:34
@Avaq
Copy link
Member

Avaq commented Apr 26, 2016

Looks good to me, though I haven't kept up with the discussions leading up to this point. I'll leave the final call to @davidchambers.

@@ -2292,6 +2292,24 @@
R.pipe(R[name], Just, R.filter(R.gte(_, 0))));
};

//# append :: a -> Array a -> Array a
Copy link
Member

Choose a reason for hiding this comment

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

This is still in the List section. This function is Array-specific so belongs in the Array section. Let's do this:

   //. ### Array

+  //# append :: a -> Array a -> Array a
+  //.
+  //. Takes a value of any type and an array of values of that type, and
+  //. returns the result of appending the value to the array.
+  //.
+  //. ```javascript
+  //. > S.append(3, [1, 2])
+  //. [1, 2, 3]
+  //.
+  //. > S.append([3, 4], [[1], [2]])
+  //. [[1], [2], [3, 4]]
+  //. ```
+  S.append =
+  def('append',
+      {},
+      [a, $.Array(a), $.Array(a)],
+      function(x, xs) { return xs.concat([x]); });
+
   //# find :: (a -> Boolean) -> Array a -> Maybe a

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow i'm close to giving up x__X - The one huge file is driving me bonkers. I don't think I've ever needed so many attempts to get something so trivial done. Sorry.

Copy link
Member

Choose a reason for hiding this comment

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

Even with separate files we'd need to specify the readme order somewhere, but a list of filenames in the makefile would certainly be easier to read. ;)

@gilligan gilligan force-pushed the add-append-function branch from f913cc1 to 3c7aa32 Compare April 26, 2016 15:41
@gilligan
Copy link
Contributor Author

Are we good to go now? :-) fingers crossed

@@ -2292,6 +2292,7 @@
R.pipe(R[name], Just, R.filter(R.gte(_, 0))));
};


Copy link
Member

Choose a reason for hiding this comment

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

👾

@davidchambers
Copy link
Member

Delete the stray line and I'll merge. :)

@gilligan gilligan force-pushed the add-append-function branch from 3c7aa32 to f36353f Compare April 27, 2016 18:40
@davidchambers
Copy link
Member

🌳 Thanks, Tobias!

@davidchambers davidchambers merged commit 281f647 into sanctuary-js:master Apr 28, 2016
@gilligan gilligan deleted the add-append-function branch April 28, 2016 06:13
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.

5 participants