Skip to content
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

Make reverse more DWIM-y #1748

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

davidfetter
Copy link
Contributor

Strings and numbers go backward.
Booleans flip.
Objects switch keys and values.

@coveralls
Copy link

coveralls commented Oct 25, 2018

Coverage Status

Coverage remained the same at 84.457% when pulling 0b5da6e on davidfetter:expand_reverse into 0bc7708 on stedolan:master.

@leonid-s-usov
Copy link
Contributor

leonid-s-usov commented Oct 25, 2018

Reversing strings totally makes sense!

Boolean - it's weird. I'd not include it though, you have not to reverse bools in a predictable and typesafe way.

Numbers just don't make sense. If that's really what you want, I'd rather that you make it explicit in your code .number | tostring | reverse. The issue is that reversing an arbitrary valid number's string representation will probably give you an error trying to convert it back. Consider -1, 3e+3. And in any case, it's pretty silly unless it's a very specific requirement which doesn't deserve a builtin

@pkoppstein
Copy link
Contributor

@davidfetter - Some observations:

a) there is no reason to define $in in the body of the def;

b) for strings, explode | reverse | implode is much faster (approx 2 times according to my measurements) and also significantly more economical with memory than using split/join;

c) In my opinion, the proposed def is inappropriate for objects as the proposed operation is a kind of inversion or transposition. A more appropriate def (perhaps even the DWIM definition) would reverse the order of keys. In addition, this def makes it very easy for users to ignore all the issues that key/value inversion can present. If someone wants to invert keys and values in some fashion, they should be aware of all the issues, and be responsible for their resolution.

d) I would go even further than @leonid-s-usov and argue that the domain of reverse should not be extended beyond arrays and strings, mainly to avoid bloat and duplication of function. The simplicity of explode|reverse|implode could even be taken as an argument against the wisdom of an extension to strings :-)

@leonid-s-usov
Copy link
Contributor

leonid-s-usov commented Oct 25, 2018 via email

@davidfetter
Copy link
Contributor Author

I've trimmed the extension back just to strings and removed fat from the code.

The behavior for other types is compatible with the current behavior.

@pkoppstein
Copy link
Contributor

pkoppstein commented Oct 29, 2018

@davidfetter - The trimmed down def has three bugs in it (e.g. it still assumes $in has been defined).

Also, to avoid the second type check, I'd suggest:

def reverse:
  def r: [.[length - 1 - range(0;length)]];
  if type == "string" then explode | r | implode else r end;

If you wanted to propose the behavior on JSON objects that reverses the order of keys:

def reverse:
  def r: [.[length - 1 - range(0;length)]];
  if type == "string" then explode | r | implode
  elif type == "object" then to_entries | r | from_entries
  else r
  end;

By the way, please note that it will be up to the maintainers to decide whether and when to include your contribution.

@davidfetter davidfetter force-pushed the expand_reverse branch 2 times, most recently from 02bb1f2 to 3e7fa84 Compare October 29, 2018 14:14
@davidfetter
Copy link
Contributor Author

I really appreciate your patient help here.

Strings now work. Other types' current behavior preserved.
@nicowilliams
Copy link
Contributor

Sure, why not.

@itchyny
Copy link
Contributor

itchyny commented Jun 25, 2023

Related issue: #412.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants