Comments (7)
any updates here?
from npm-ramda.
export const FileLens = file => lensPath(['fileStats', file]); export const StateForFileLens = (f) => compose(lensPath(['stats']), FileLens(f)); export const GoalsForFileLens = (f) => compose(lensPath(['goal']), FileLens(f));
app/stats/state/lens.ts(5,48): error TS2345: Argument of type 'ManualLens<{}, {}>' is not assignable to parameter of type '(v: {}) => {}'.
Type 'ManualLens<{}, {}>' provides no match for the signature '(v: {}): {}'.
app/stats/state/lens.ts(6,48): error TS2345: Argument of type 'ManualLens<{}, {}>' is not assignable to parameter of type '(v: {}) => {}'.
I'm getting this same issue.
from npm-ramda.
any updates on this? Do you have any suggestions on going forward @tycho01 @davidlibland ? I just learned about Lenses and want to use it in a typescript project with ramda. I'll see if i can contribute
from npm-ramda.
Thanks for spotting this. This seems related to our compose
definition, which currently only handles functions. Looks like we'll need to upgrade it to deal with lenses as well.
Edit: whoops. Lenses are functions. Looks like I'd need to check further.
from npm-ramda.
Presumably Lens should have a type something along the lines of the following:
interface Functor<A> {
fmap<B>(f: ((_: A) => B)): B;
}
type pCoalgebra<A, B> = ((g: A) => Functor<B>);
type Lens<S, T, A, B> = ((f: pCoalgebra<S, T>) => pCoalgebra<A, B>);
This functional form (rather than being defined directly in terms of getters and setters) is what allows them to compose nicely. Moreover it's frequently nice to use a lens in this format, consider this example:
const Result = require("folktale/result");
const {lensProp, ifElse, map} = require("ramda");
const regEx = /\(\d{3}\)\s\d{3}-\d{4}/
const validatePhoneNumberString = ifElse(
(s) => regEx.test(s),
Result.of,
() => Result.Error("Invalid Phone Number")
);
const contacts = [
{name: "Honest Abe", phone: "(123) 456-7890"},
{name: "Bob Fraud", phone: "not my number"}
];
const phoneLens = lensProp("phone");
# Here we apply the Lens directly as a function to lift a functor map
# from the lens focus to the whole context:
const validateContacts = map(phoneLens(validatePhoneNumberString))
const validatedContacts = validateContacts(contacts);
>>> [
Result.Ok({ value: { name: "Honest Abe", phone: "(123) 456-7890" } }),
Result.Error({ value: "Invalid Phone Number" })
]
(I don't believe you can write the equivalent code so directly in terms of getters and setters).
Moreover, this is how they are defined in Ramda, cf. the docstring:
/**
* Returns a lens for the given getter and setter functions. The getter "gets"
* the value of the focus; the setter "sets" the value of the focus. The setter
* should not mutate the data structure.
*
* @func
* @memberOf R
* @since v0.8.0
* @category Object
* @typedefn Lens s a = Functor f => (a -> f a) -> s -> f s
* @sig (s -> a) -> ((a, s) -> s) -> Lens s a
* @param {Function} getter
* @param {Function} setter
* @return {Lens}
* @see R.view, R.set, R.over, R.lensIndex, R.lensProp
* @example
*
* var xLens = R.lens(R.prop('x'), R.assoc('x'));
*
* R.view(xLens, {x: 1, y: 2}); //=> 1
* R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2}
* R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2}
*/
from npm-ramda.
@davidlibland feel like doing a PR?
from npm-ramda.
@seanyu4296: feel free! if the snippets above might do it, add it, see if it works in a test and we're good to go!
from npm-ramda.
Related Issues (20)
- npm-ramda motivation HOT 4
- R.Path - Typescript error for an array of objects
- `prop` does not work with optional keys HOT 1
- curry type
- `pipeWith` is missing
- Using groupBy with path always fails typecheck
- mergeDeepRight returns incompatible if objects contain array props HOT 1
- Add dissoc(prop: number)
- `transduce` and `into` are declared with `any` and for narrow use cases in general
- R.evolve is not deriving type from curried evolver
- Generics lost on currying for higher-order functions HOT 1
- type into in typescript
- How should we type lift?
- How to type pathOr?
- Expected 0 arguments, but got 1 HOT 3
- Evolve typings
- Typescript 4: Solve a lot of our problems
- Ramda 0.27 HOT 1
- Arity limitation for R.compose's rightmost function?
- Adding support for type guards to `partition` function.
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 npm-ramda.