-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Feature/filterable #485
Feature/filterable #485
Conversation
Thanks @raveclassic for this PR. I'm on the fence with this: when preparing the 1.0.0 release I removed I think we should discuss such type classes one by one. For example let's consider Also export interface Compactable<F> {
readonly URI: F
readonly compact: <A>(fa: HKT<F, Option<A>>) => HKT<F, A>
readonly separate: <A, B>(fa: HKT<F, Either<A, B>>) => Separated<HKT<F, A>, HKT<F, B>>
} So is Another option would be to publish a separate library, let's say /cc @sledorze how you feel about this? Do you know of equivalent abstractions in Scala (cats, scalaz)? |
…tions (experimental)
@gcanti Thanks for response! About Anyway I'd also like to discuss it first :) |
@raveclassic That's awesome! I didn't know that, thanks for explaining. So // Compactable.ts
import { Applicative } from 'fp-ts/lib/Applicative'
import { Either } from 'fp-ts/lib/Either'
import { Functor } from 'fp-ts/lib/Functor'
import { HKT } from 'fp-ts/lib/HKT'
import { Traversable, traverse } from 'fp-ts/lib/Traversable'
export type Separated<A, B> = {
readonly left: A
readonly right: B
}
export interface Compactable<F> {
readonly URI: F
readonly separate: <A, B>(fab: HKT<F, Either<A, B>>) => Separated<HKT<F, A>, HKT<F, B>>
}
export function partitionMap<F>(
F: Compactable<F> & Functor<F>
): <A, L, R>(fa: HKT<F, A>, f: (a: A) => Either<L, R>) => Separated<HKT<F, L>, HKT<F, R>> {
return (fa, f) => F.separate(F.map(fa, f))
}
export function wilt<F, T>(
F: Applicative<F>,
T: Traversable<T> & Compactable<T>
): <A, L, R>(ta: HKT<T, A>, f: (a: A) => HKT<F, Either<L, R>>) => HKT<F, Separated<HKT<T, L>, HKT<T, R>>> {
const traverseFT = traverse(F, T)
return (ta, f) => F.map(traverseFT(ta, f), T.separate)
} (overloadings omitted for clarity) |
Yeah, that was my first attempt :) But very quickly I realized that such default implementation isn't performant enough for Array and Set. Even more they already have their own implementation which duplicates API. Looks like a place for I'm pushing my latest changes now (defaults in Filterable), take a look, what do you think? (I'm just porting purescript-filterable, some helpers may be removed) |
Well... Actually I don't like what's going on because such approach with defaults bloats API massively and brings in a lot of confusion from which method to start implementing Witherable. Need some time to think it over. |
Not sure I understand why we can't just add If both Then every module can choose either to use the default implementation (of |
@gcanti But there's no way to require some data structure to implement Ok, I'll clean up this PR and leave only useful things in |
After some thoughts it still feels intuitively incorrect to me to move What about implementations. I do agree that forcing to define all methods ( |
There are several topics involved, let's try to discuss one by one Performance
We could do this, but what about New type classes
You are right, Maybe I just need a plan to get the big picture:
|
IMO this makes sense in the long-term. As fp-ts continuosly evolves gaining new features it seems correct to provide wider API and to tune performance. However we could omit these typeclasses for Looks like a plan. I'd also add the following:
|
Also I'd move all generalizable functions from Array to Filterable (Yeah, there's still a lot of work here) |
Ok, so you want to start from
The next version will be 1.7.0 (current active milestone)
Overall looks good. Could you please split this plan in chunks, sending multiple PRs? Let's start from
Not sure what you mean, let's discuss this later though, one thing at a time. |
Yeah, exactly
Should I change all
Sure, I've already split branches, going to open PR with |
This PR will be split into parts, closing. |
This PR adds Compactable/Filterable/Witherable typeclasses (based on https://github.com/LiamGoodacre/purescript-filterable) as well as some implementations for Array/Option/Either/StrMap/Set.
Also Set now implements Witherable (Functor/Compactable/Filterable/Traversable/Foldable) (please, check it carefully, there's some API duplication, private though, because I don't get why Sets accept additional instances for map etc.).
There're also some helpers added, take a look.