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

Type Safe Ramda Functions #145

Closed
24 of 32 tasks
svozza opened this issue Feb 3, 2016 · 18 comments
Closed
24 of 32 tasks

Type Safe Ramda Functions #145

svozza opened this issue Feb 3, 2016 · 18 comments

Comments

@svozza
Copy link
Member

svozza commented Feb 3, 2016

As mentioned in #141, let's start try to get a list of typesafe functions we want from Ramda. I've put together a list of way too many functions that we can whittle down here as people chip in with their views.

Something I'm not clear on though is how we deal with functions that accept both arrays and string. Do we need a new type?

@davidchambers
Copy link
Member

I think we can tick the box beside R.nth since we already have S.at.

Is there a reason you included R.when but not R.unless?

Something I'm not clear on though is how we deal with functions that accept both arrays and string. Do we need a new type?

This should be possible. For example:

concat :: Semigroup a => a -> a -> a

@svozza
Copy link
Member Author

svozza commented Feb 4, 2016

I think we can tick the box beside R.nth since we already have S.at.

Ah yes, of course.

Is there a reason you included R.when but not R.unless?

Oops, no, just an oversight. Fixed.

@jethrolarson
Copy link

I assume that your concat would ensure that both arguments are the same type, right?

@davidchambers
Copy link
Member

I assume that your concat would ensure that both arguments are the same type, right?

That's right, @jethrolarson:

const $ = require('sanctuary-def');

const def = $.create(true, $.env);

//    a :: Type
const a = $.TypeVariable('a');

//    Semigroup :: TypeClass
const Semigroup = $.TypeClass(
  'sanctuary/Semigroup',
  x => x != null && typeof x.concat === 'function'
);

//    concat :: Semigroup a => a -> a -> a
const concat =
def('concat',
    {a: [Semigroup]},
    [a, a, a],
    (x, y) => x.concat(y));

concat('abc', 'def');
// => 'abcdef'

concat([1, 2, 3], [4, 5, 6]);
// => [1, 2, 3, 4, 5, 6]

concat('abc', [1, 2, 3]);
// ! TypeError: ‘concat’ expected a value of type String as its second argument; received [1, 2, 3]

concat(['a', 'b', 'c'], [1, 2, 3]);
// ! TypeError: ‘concat’ expected a value of type (Array String) as its second argument; received [1, 2, 3]

@svozza
Copy link
Member Author

svozza commented Feb 28, 2016

So, I'm trying to implement S.length and this issue of types that work on lists and strings won't be resolved by relying on the Semigroup type. If we restrict S.length in this way then that means that the function should work on Either types, which of course don't have a length property. It also means that it won't operate on the Function type. If we could treat strings as lists of characters then we could just use the Functor type class but that's not an option here.

@jethrolarson
Copy link

Yeah, semigroup doesn't give you index or length. Array-like does but
that's not mathy :)

On Sun, Feb 28, 2016, 3:42 AM Stefano Vozza [email protected]
wrote:

So, I'm trying to implement S.length and this issue of types that work on
lists and strings won't be resolved by relying on the Semigroup type. If
we restrict S.length in this way then that means that the function should
work on Either types, which of course don't have a length property. It
also means that it won't operate on the Function type. If we could treat
strings as lists of characters then we could just use the Functor type
class but that's not an option here.


Reply to this email directly or view it on GitHub
#145 (comment).

@svozza
Copy link
Member Author

svozza commented Feb 28, 2016

Ha. Yes, I was hoping not to go down that route...

@joneshf
Copy link
Member

joneshf commented Feb 29, 2016

Array-like can mostly be approximated by Foldable because you can define toArray: https://github.com/fantasyland/fantasy-land/#foldable

So you could have length. Though i'm not exactly sure how to type it with sanctuary-def:

// length : Foldable f => f a -> NonNegativeInteger
function length(x) {
  return x.reduce((state, _) => state + 1, 0);
}

Are there any examples in sanctuary-def of typeclasses with higher kinded types? I see the test for Monad, but it's a test of the bad case.

@davidchambers
Copy link
Member

One straightforward solution is a "Countable" type class which checks whether a given value has a valid length property. I like where @joneshf is going with Foldable, though.

Are there any examples in sanctuary-def of typeclasses with higher kinded types?

I believe higher-kinded types to be doable, but sanctuary-def doesn't yet support them.

This was referenced Apr 11, 2016
@gilligan gilligan mentioned this issue Apr 19, 2016
@svozza
Copy link
Member Author

svozza commented Apr 24, 2016

Something that was mentioned in the Gitter room was how this issue is a good gateway for people looking to contribute for the first time. It got me thinking that it might be cool to do what they do on the nodejs project and split some of this function list into individual issues and label them with the tag 'good first contribution' like this. What do people think?

@davidchambers
Copy link
Member

Great idea, Stefano!

@gilligan
Copy link
Contributor

yeah these are totally "low hanging fruits" and I really like that suggestion Stefano ;)

@davidchambers
Copy link
Member

I'll close this issue. We've added many of the important functions, and #216 will add a handful more. Don't hesitate to open an issue if there's a particular Ramda function you'd like sanctified. :)

@futpib
Copy link
Contributor

futpib commented Oct 31, 2017

@davidchambers, do you have something like R.fromPairs?

@davidchambers
Copy link
Member

Sanctuary doesn't yet provide such a function, @futpib. Are you interested in working on a pull request to add it?

@futpib
Copy link
Contributor

futpib commented Oct 31, 2017

@davidchambers #454 poof

@Fl4m3Ph03n1x
Copy link

Why was R.modulo removed?

@davidchambers
Copy link
Member

Why was R.modulo removed?

Because sanctuary-int provides both rem and mod. :)

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

No branches or pull requests

7 participants