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

WISH: /CASE and /TAIL refinements for REPLACE #18

Open
9214 opened this issue May 22, 2018 · 12 comments
Open

WISH: /CASE and /TAIL refinements for REPLACE #18

9214 opened this issue May 22, 2018 · 12 comments
Labels

Comments

@9214
Copy link
Contributor

9214 commented May 22, 2018

Original issue: red/red#2845

Title.

alphabet: {AaBbCc}
replace alphabet #"a" 1
probe alphabet

Results in

"1aBbCc"

And as of now it's impossible to replace the lowercase a, because /case refinement is absent. For the sake of compatability, I propose to implement /tail too, thus making replace identical to R2 version:

REFINEMENTS:
     /all -- Replace all occurrences
     /case -- Case-sensitive replacement
     /tail -- Return target after the last replacement position
@Palaing
Copy link

Palaing commented Oct 20, 2018

Rebol's version of replace (source replace) seems to work fine in Red. It has /case, /tail and even allows a function as third argument.

@endo64
Copy link

endo64 commented Oct 21, 2018

@Palaing In Red's version there is also a deep refinement which is very useful. Red's version also supports parse rules. So Rebol's version is a bit limited.

I've added case refinement and will do PR,

@endo64
Copy link

endo64 commented Oct 21, 2018

Here is my PR, @9214 @greggirwin can you please check? Note that didn't add tail yet. red/red#3573

@endo64
Copy link

endo64 commented Oct 27, 2018

This can be closed now as /case added to replace. We can create another one for /tail.

@hiiamboris
Copy link

Don't wanna make a separate wish for this...
I'd like to also have /only refinement for replace on blocks. It would then treat the replacement block as a single value.

My example:

#macro LEN3?: func [waht [path! word! block!]] [
	replace/deep [((length? waht) / 3)] 'waht reduce [waht]
]

Here I have to use reduce [waht] to do what could have been done by replace/deep/only.

@greggirwin
Copy link
Contributor

Adding /only support to replace makes a lot of sense. We don't have a lot of tests for replace right now, so step 1, IMO, is to make sure we have good coverage for the current behavior. Then it seems like it could use a review, to see if we can simplify things a bit, and add comments. It's got a lot of subtlety in there already, and this will add more. system/words/all is used enough that a local shortcut also makes sense to me.

@hiiamboris
Copy link

Another point is deep replacement vs using a parse rule: ☝️ June 7, 2019 9:33 PM

@greggirwin
Copy link
Contributor

Looking at the original ticket, /tail is a tough call. I don't recall ever using it, but there is no good way to get that info otherwise. Only needed for advanced processing, but then is it useful with /deep or /all? What are the top use cases or examples?

@hiiamboris
Copy link

R2:

>> replace/all/tail "acbcd" "c" "-"
== "d"

with /deep it should probably error out, and if one day somebody finds a meaning for that - we add it.

@greggirwin
Copy link
Contributor

I understand how it works, but how/where has anyone used it in the real world? In a search of R2 code here, I found zero instances of it. And that includes all Rebol SDKs, IOS worlds, and a lot of other code.

@hiiamboris
Copy link

And what about replace without /all refinement - what is the use case for it? Does just replace make more sense than replace/tail? The latter at least I can imagine being used in a loop, even intermittent with find/tail maybe. And will it hurt to return /tail by default when /all is not used?

@greggirwin
Copy link
Contributor

The most common use case for replace is replace/all, but reversing the logic and having a /first refinement (or other name) would a) be a huge Rebol incompatibility, and b) not fit quite as nicely with other uses of single vs /all behavior. Tough decision, even then, because I've tripped over that myself more than once.

I also think the vast majority of replace calls don't chain the return value. Only a few cases turned up where "replace/all replace/all" was found. The common pattern is strictly imperative. Call it, then call it again.

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

No branches or pull requests

6 participants