-
-
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
Add _.toggle/_.transition/_.fsm to underscore #2850
Comments
@sktguha Thanks for reaching out. The _.mixin({toggle: _.negate(_.identity)});
flag = _.toggle(flag);
// or
flag = _(flag).toggle(); Regarding |
Ok thanks for the explanation regd toggle. I guess then it is already available. |
Thanks. Yes, I can see the practical value in that and I think there is no function yet in Underscore that makes this easy. To make it as general as possible, I would suggest that this function accepts arbitrary mappings rather than only pairs of alternative values: var changeTrafficLight = getTogglingfunction({
red: green,
yellow: red,
green: yellow
});
var currentState = 'red';
currentState = changeTrafficLight(currentState); // 'green'
currentState = changeTrafficLight(currentState); // 'yellow'
currentState = changeTrafficLight(currentState); // 'red' again
In order to support all value types instead of only strings, it should maybe (also) support a pair of arrays and/or an array of pairs à la var matchStart = /<[^/][^>]*>/, matchEnd = /<\/[^>]+>/;
var toggleParsingMode = getTogglingfunction([
[matchStart, matchEnd],
[matchEnd, matchStart]
]);
var parsingMode = matchStart; // match SGML opening tags
parsingMode = toggleParsingMode(parsingMode); // match SGML closing tags from now on This reminds me a bit of a finite state machine à la Machina. We could take this even further in that direction by allowing callers of var changeTrafficLight = getTogglingfunction([
['red', null, function(signal) {
return signal === 'carWaiting' ? 'green' : 'red';
}],
['yellow', 'red'],
['green', null, function(signal) {
return signal === 'pedestrianWaiting' ? 'yellow' : 'green';
}]
]);
var currentState = 'green';
currentState = changeTrafficLight(currentState); // still 'red'
currentState = changeTrafficLight(currentState, 'pedestrianWaiting'); // still 'red'
currentState = changeTrafficLight(currentState, 'carWaiting'); // 'green'
currentState = changeTrafficLight(currentState); // still 'green'
currentState = changeTrafficLight(currentState, 'carWaiting'); // still 'green'
currentState = changeTrafficLight(currentState, 'pedestrianWaiting'); // 'yellow' Although if people want this kind of functionality, maybe they should just use Machina. I'm only thinking out loud. Some things to think about:
|
Wow that quite a comprehensive feature request. You have raised very good points. I am not sure how the interface should be yet. Do you have any additional thoughts on this ? |
Other than what I already wrote, no not really. I'm not in a hurry, though. Are you? |
Hi,
I want to propose to add a new function called toggle to underscore.
At simple level it would toggle the value given to it. i.e if it is passed true it will return false and if it is passed false it will return true.
ex-
flag = _.toggle(flag)
// if flag was true then it will get set to false
Also additionally we can have another function that if passed custom arguments then it will return a new function which can be used to toggle with those arguments
For ex -
bitToggler = getTogglingfunction(0,1)
Now if we do
bit = bitToggler (bit)
// If bit had value of 0 it will be set as 1
The text was updated successfully, but these errors were encountered: