Giter VIP home page Giter VIP logo

Comments (11)

CrossEye avatar CrossEye commented on June 7, 2024

At the very least, this difference should be documented. It probably wouldn't be hard to create a third version of map that worked the same was as the native Array.prototype.map for sparse arrays, but first I'd like to know a little more about use-cases. Why would you want this behaviour?

If the answer is simply to be more consistent with Array.prototype.map, that's probably not enough. In some very important sense, Ramda's fundamental data type is a list. The only comfortable way to work with lists in Javascript is through arrays.1 The fact that arrays can also be used to house other sorts of structures is from our point of view mostly an unfortunate implementation accident.

I'm curious if you think this should match the native map more for reasons of consistency or if you have real use cases for mapping over sparse arrays.

1 But for a very fun alternative see @buzzdecafe's cons, car, cdr gist, which builds up LISP-style lists from pure functions.

from ramda.

megawac avatar megawac commented on June 7, 2024

https://twitter.com/jashkenas/status/464121883961851904

from ramda.

buzzdecafe avatar buzzdecafe commented on June 7, 2024

chrome and ff already no longer "compact" the input array. tried it in the console, the above examples output

[undefined, 2, 3]

btw, i don't recall: do we have a fn to compact an array with undef's in it?

from ramda.

CrossEye avatar CrossEye commented on June 7, 2024

Well, I do get different behaviour from Ramda.map and Array.prototype.map. Ramda includes an 0 property in the output with value undefined. I just am not sure how important that is, especially given the tweet @megawac noted.

from ramda.

buzzdecafe avatar buzzdecafe commented on June 7, 2024

ah, i see, my examples were crazy weird js behavior; the '0' property is omitted but the length of the output array is still 3. Simplest impl:

R.map.sparse = function(fn, list) { return list.map(fn); };

which should play nice with functors et al.

from ramda.

megawac avatar megawac commented on June 7, 2024

Unless you're worried about compat

Sent from my HTC

----- Reply message -----
From: "Michael Hurley" [email protected]
To: "CrossEye/ramda" [email protected]
Cc: "Graeme Yeates" [email protected]
Subject: [ramda] R.map doesn't support Sparse Arrays like the standard map (#96)
Date: Fri, Jun 27, 2014 8:37 AM
ah, i see, my examples were crazy weird js behavior; the '0' property is omitted but the length of the output array is still 3. Simplest impl:

R.map.sparse = function(fn, list) { return list.map(fn); };

which should play nice with functors et al.


Reply to this email directly or view it on GitHub.

from ramda.

buzzdecafe avatar buzzdecafe commented on June 7, 2024

@megawac i agree with @CrossEye, compatibility alone is not a design goal.

from ramda.

CrossEye avatar CrossEye commented on June 7, 2024

@buzzdecafe: that would of course have to be curried if we chose to use it.

@megawac: what compat issues concern you?

But my main concern is that I have yet to see an actual reason to include it. That our map isn't a drop-in replacement for the one on Array.prototype should be clear. The callback function has a different signature, supplied only the element and not the index and the entire list as well. So the question is whether a third version of map is warranted. I'm waiting for a rationale. @cscalfani: any suggestions?

from ramda.

megawac avatar megawac commented on June 7, 2024

Sparse arrays should be avoided, theres plenty of articles about why a dictionary should be used to cover sparse array cases. Anyway, map is an es5 method, if you do want to support sparse arrays I recommend you create a composable each helper for normal and slow (sparse) cases. Just needs to have an extra if (i in arr) check. Seeing you don't suport context in your iterator functions es5 parity clearly isnt realistic

from ramda.

cscalfani avatar cscalfani commented on June 7, 2024

I think that support for sparse arrays as the default in the standard is a TERRIBLE idea. I agree that at least the documentation should note the lack of sparse array support. I can imagine people using these functions as drop-ins for the standard functions.

So @CrossEye, I cannot provide any reason that Ramda should support them and only made the suggestion for an alternative function if standard support was desirable, which appears, from this thread, to be non-existent.

from ramda.

buzzdecafe avatar buzzdecafe commented on June 7, 2024

ok, i think we all agree--we gotta document the difference in R.map from Array.prototype.map w/r/t sparse arrays, but otherwise, we're not going to support it until someone provides some convincing use cases

from 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.