-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
_.constant Function #402
Comments
This is a very interesting function -- but I'm afraid that I fail entirely to see the use-case where this would be helpful. Instead of filtering a collection by Provide some real-world code that makes use of this idiom, and we'll reopen the ticket. |
There is an analogue in Clojure core: constantly I admit the usage is a little subtle, but I'll try to demonstrate. Consider _.isUndefined(filterFunc) ? collection : _.filter(collection, filterFunc);
_.select(collection, filterFunction || function() { return true; })
_.select(collection, filterFunction || _.constant(true)) Graphics // api: object.position = function(time) { return coords }
circle.position = _.constant( [10, 10] )
// api: image.fill( function(x, y) { return color })
image.fill( _.constant( black ) ); Testing/Stubbing textDisplay.source = { getText: _.constant("text") } |
So, mainly for passing constants to APIs that expect functions. Nice. Still, I think it would be clearer for most readers, and not much longer, to simply write the function:
|
That is correct, though the same can be said for I also think the word |
This would also be useful if you need a function that returns a particular
I agree that there isn't a huge difference. |
It would be nice to have a function like
_.constant = function(x) { return function() { return x; }}
I have often needed something like
_.constant(true)
or_.constant(false)
when filtering collections with a configurable iterator.The text was updated successfully, but these errors were encountered: