-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Impure function mistake #50
Comments
Hi! In this case, it's available via the outer closure as a dependency - just like |
In a common manner and in Chapter 6 too: const host = 'api.flickr.com';
const path = '/services/feeds/photos_public.gne';
const query = t => `?tags=${t}&format=json&jsoncallback=?`;
const url = t => `https://${host}${path}${query(t)}`; The url is impure? Because it use the host and path constants! |
@marcelobart "Will various executions of my program in various environments will change the value of The answer is no, |
Hummm, make sense! If we look only, and really only, to the url function, I think it's okay to consider it an impure fn. But in a application context, the url and host always exists, make the whole scope pure. Is that right? Thanks for the explanation! :D |
Hmmm... not exactly sure what you mean by " if we export the function to make a unit test, without host and path (and query) it will break." but I believe you have a false sense of what closures are in JavaScript. const host = 'api.flickr.com';
const path = '/services/feeds/photos_public.gne';
const query = t => `?tags=${t}&format=json&jsoncallback=?`;
const url = t => `https://${host}${path}${query(t)}`;
exports.url = url; This is a perfectly valid (and I'd even say, recommended) way of exposing the |
No, I know what closures are, (@marcelobart here, :P)! I mean that, if we start to "componentize" the code, and does not put all the necessary functions and consts (in this case: host, path and query) inside the same closure, we will get an error. I understand that, to url function works as pure, the three "dependencies" must be pure/not mutable. I don't say that it is wrong, but when I think in "functional way" of writing things, I think to a more functionitization approach of everything, hahaha. :P (I don't know if this way of think is right in fp). // Simplified version!
// using, for example (ramda)
const host = R.always('api.flickr.com');
const path = R.always('/services/feeds/photos_public.gne');
// and composing/converging it to create url fn
const url = R.converge((h, p) => `https://${h}${p}`, [host, path]);
// url() === "https://api.flickr.com/services/feeds/photos_public.gne" |
Hi!
Its duplicating of my email via gitbook.
In Chapter 6 following function seems to be impure:
As I've understood from previous chapters
img
is impure because it relies on$
which isnt directly passed via arguments. But in the text there is nothing aboutimg
impurity so some folks can assume its pure.Am I right?
The text was updated successfully, but these errors were encountered: