Comments (2)
@gcanti Would that be a start (I'm hesitating between 'promap' or 'dimap')?
export interface StaticProfunctor<F extends HKT2S> {
lmap<A, B, C>(fa: (a: A) => B, m: HKT2<B, C>[F]): HKT2<A, C>[F]
rmap<B, C, D>(fb: (c: C) => D, m: HKT2<B, C>[F]): HKT2<B, D>[F]
promap<A, B, C, D>(fa: (a: A) => B, fb: (c: C) => D, m: HKT2<B, C>[F]): HKT2<A, D>[F]
}
export interface FantasyProfunctor<F extends HKT2S, B, C> {
lmap<A, B, C>(fa: (a: A) => B): HKT2<A, C>[F]
rmap<B, C, D>(fb: (c: C) => D): HKT2<B, D>[F]
promap<A, D>(fa: (a: A) => B, fb: (c: C) => D): HKT2<A, D>[F]
}
insight on how you would encode partial type application in order to follow the spec?
Module must match the Profunctor signature for some type T, support Functor algebra for all types U created by setting the first parameter of T to an arbitrary concrete type (for example type U = T<number, a>)
I have this but it enforces nothing (besides 'D'):
export function toFunctor<F extends HKT2S, B, C, D>(m: FantasyProfunctor<F, B, C>): FantasyFunctor<F, C> {
return ({
map(f: (a: C) => D): HKT<D>[F] {
return m.rmap(f);
}
});
}
Another approach:
export interface FantasyProfunctor<F extends HKT2S, B, C> {
lmap<A, B, C>(fa: (a: A) => B): HKT2<A, C>[F]
rmap<B, C, D>(fb: (c: C) => D): HKT2<B, D>[F]
promap<A, D>(fa: (a: A) => B, fb: (c: C) => D): HKT2<A, D>[F]
toFunctor<D>(): FantasyFunctor<F, C>; // this could be the solution but we can only fix 'D' parameter in the implemented instance
}
So this generates an error because 'D' is a phantom and not bound to anything.. I can't see how to enforce something properly here..
Or we can simply do something like that for FantasyLand:
export interface FantasyProfunctor<F extends HKT2S, B, C> extends FantasyFunctor<F, C> {
lmap<A, B, C>(fa: (a: A) => B): HKT2<A, C>[F]
rmap<B, C, D>(fb: (c: C) => D): HKT2<B, D>[F]
promap<A, D>(fa: (a: A) => B, fb: (c: C) => D): HKT2<A, D>[F]
map<D>(f: (a: C) => D): HKT<D>[F] // no escape from the Functor
map<D>(f: (a: C) => D): HKT<D>[F] & HKT2<B, D>[F] // would that be acceptable?
}
When thinking about StaticLand, it's the same story as Either, the first type param would become any.
Waiting for your feedback..
Also, not sure what interesting instances should be implemented.. (Arrows ?)
from fp-ts.
I'm hesitating between 'promap' or 'dimap'
Yeah, purescript uses dimap
https://github.com/purescript/purescript-profunctor/blob/master/src/Data/Profunctor.purs#L23, I would stick with static-land though (as done so far), so promap
import { HKT2, HKT2S } from './HKT'
import { StaticFunctor, FantasyFunctor } from './Functor'
import { identity } from './function'
export interface StaticProfunctor<F extends HKT2S> extends StaticFunctor<F> {
promap<A, B, C, D>(ab: (a: A) => B, cd: (c: C) => D, fbc: HKT2<B, C>[F]): HKT2<A, D>[F]
}
export interface FantasyProfunctor<F extends HKT2S, B, C> extends FantasyFunctor<F, C> {
promap<A, D>(ab: (a: A) => B, cd: (c: C) => D): HKT2<A, D>[F]
}
lmap
and rmap
can be derived (like lift
in Functor
)
export function lmap<F extends HKT2S, A, B, C>(profunctor: StaticProfunctor<F>, f: (a: A) => B, fbc: HKT2<B, C>[F]): HKT2<A, C>[F] {
return profunctor.promap<A, B, C, C>(f, identity, fbc)
}
// this is just the functor map
export function rmap<F extends HKT2S, A, B, C>(profunctor: StaticProfunctor<F>, f: (a: B) => C, fab: HKT2<A, B>[F]): HKT2<A, C>[F] {
return profunctor.map(f, fab)
}
insight on how you would encode partial type application in order to follow the spec?
extends StaticFunctor<F>
and extends FantasyFunctor<F, C>
should be enough I guess
Also, not sure what interesting instances should be implemented
Theoretically functions ->
(though we need a wrapper in TypeScript, see below), profunctor optics, other examples here https://github.com/purescript/purescript-profunctor/tree/master/src/Data/Profunctor and here https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/profunctors
Functions as profunctors
declare module './HKT' {
interface HKT<A> {
'->': Fun<any, A>
}
interface HKT2<A, B> {
'->': Fun<A, B>
}
}
export const URI = '->'
export type URI = typeof URI
export class Fun<B, A> implements FantasyProfunctor<URI, B, A> {
readonly _B: B
readonly _A: A
readonly _URI: URI
constructor(public readonly value: (b: B) => A) {}
run(b: B): A {
return this.value(b)
}
map<C>(f: (a: A) => C): Fun<B, C> {
return this.promap<B, C>(identity, f)
}
promap<C, D>(cb: (c: C) => B, ad: (a: A) => D): Fun<C, D> {
return new Fun<C, D>(c => ad(this.run(cb(c))))
}
}
from fp-ts.
Related Issues (20)
- Reproduce `Promise.all` behaviour HOT 2
- What is the idiomatic way to do pattern matching in the fp-ts ecosystem HOT 7
- Type signature of RTE.tapReaderIO is wrong
- Either.Do: `left` to be structurally similar to `right` HOT 1
- 🐠 Give `map` the same overloaded signature as `flatMap` HOT 2
- record.modifyAt doesn't infer value type at given key HOT 4
- lookup types incorrect HOT 4
- [Question (feature?)] Clean way to define a Semigroup over a (partially) partial type HOT 1
- `readonlyNonEmptyArray.union()` should accept `ReadonlyArray` as second argument
- Why does `Json.stringify` return an `Either`? HOT 2
- Directory import is not supported resolving ES modules HOT 6
- Add installation instructions to the docs HOT 1
- Is the signature of TaskEither.tapError as intended? HOT 1
- Add tapNoneIO for Option, TaskOption and IOOption and tapErrorIO for Either, TaskEither and IOEither
- Comment-related warnings when building with Vite HOT 3
- Improving option.getOrElse type inference HOT 1
- How can I do recursion in an array and make the isShow flag true?
- Sequencing is incompatible with array.map
- Misplaced/useless PURE comments with latest vite/rollup HOT 1
- [ReadonlyArray.ts] `flatten` can trigger `RangeError: Maximum call stack size exceeded`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fp-ts.