Code Monkey home page Code Monkey logo

Comments (7)

bstro avatar bstro commented on August 23, 2024 4

any updates here?

from npm-ramda.

austbot avatar austbot commented on August 23, 2024 1

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.

seanyu4296 avatar seanyu4296 commented on August 23, 2024 1

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.

KiaraGrouwstra avatar KiaraGrouwstra commented on August 23, 2024

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.

davidlibland avatar davidlibland commented on August 23, 2024

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.

KiaraGrouwstra avatar KiaraGrouwstra commented on August 23, 2024

@davidlibland feel like doing a PR?

from npm-ramda.

KiaraGrouwstra avatar KiaraGrouwstra commented on August 23, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.