giogonzo / fp-ts-ramda

Ramda functions reimplemented in fp-ts
https://giogonzo.github.io/fp-ts-ramda/
MIT License
138 stars 9 forks source link

endsWith implementation #11

Closed mfirry closed 5 years ago

mfirry commented 5 years ago

I've tried a simple definition along these lines:

import { takeLast } from './takeLast'

export function endsWith<A>(suffix: Array<A>): (list: Array<A>) => boolean;
export function endsWith<A>(suffix: Array<A>, list: Array<A>): boolean;
export function endsWith<A>(suffix: Array<A>, list?: Array<A>): any {
  if (list === undefined) {
    return function(list: Array<A>) {
      return endsWith(suffix, list);
    };
  } else {
    return takeLast(suffix.length, list) === suffix;
  }
}

(also lurking on https://github.com/ramda/ramda/blob/v0.26.1/source/endsWith.js#L27) But it doesn't seem to work. Any hint?

gcanti commented 5 years ago

It needs an instance of Eq I guess, like clamp needs an instance of Ord

import { getEq } from 'fp-ts/lib/Array';
import { Eq } from 'fp-ts/lib/Eq';
import { takeLast } from './takeLast';

function _endsWith<A>(E: Eq<A>): (suffix: Array<A>, as: Array<A>) => boolean {
  const eqArray = getEq(E);
  return (suffix, as) => eqArray.equals(suffix, takeLast(suffix.length, as));
}

export function endsWith<A>(
  E: Eq<A>
): {
  (suffix: Array<A>): (as: Array<A>) => boolean;
  (suffix: Array<A>, as: Array<A>): boolean;
};
export function endsWith<A>(E: Eq<A>): (suffix: Array<A>, oas?: Array<A>) => any {
  const _endsWithE = _endsWith(E);
  return (suffix, oas) => {
    if (oas === undefined) {
      return (as: Array<A>) => _endsWithE(suffix, as);
    } else {
      return _endsWithE(suffix, oas);
    }
  };
}
mfirry commented 5 years ago

Super cool. Right now I'm fighting with this https://gist.github.com/mfirry/3065083ca0891ebd74f7ce75df10ad34 but I'll continue with this. Grazie Giulio.

gcanti commented 5 years ago

I think you can fix that by running: npm run prettier-write

giogonzo commented 5 years ago

I'm fighting with this

@mfirry If you work in VSCode, I can also suggest to install the prettier extension and configure "Format on save" so that prettier works transparently.

It needs an instance of Eq I guess, like clamp needs an instance of Ord

Yes, looking at the Ramda signatures, this is sometimes clear, e.g. in the case of clamp:

Ord a => a → a → a → a

and sometimes less so, like in the case of endsWith: the Eq instance in Ramda is never annotated in the docs, but the library always implicitly dispatches to R.equals internally, which is kind of an Eq<unknown> instance.

mfirry commented 5 years ago

I think you can fix that by running: npm run prettier-write

Yeah it's just a) me not using prettier right now b) error being cryptic. Thanks for the heads up.