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

Add a function to replace matches with a callback #3

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ryanmiville
Copy link

closes #2

I've gone back-and-forth between the names replace_match and replace_map, but I don't think either are great.

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, thank you!

I'm not sure about the name still. replace_map sounds like it's removing a "map", or mapping over something after it has been replaced.

Is there a verb we could use?

Replacement1 = fun(Content, Submatches) ->
Submatches1 = gleam@list:map(Submatches, fun gleam@string:to_option/1),
Replacement({match, Content, Submatches1})
end,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use the same submatches function above?

Copy link
Author

@ryanmiville ryanmiville Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use it and it didn't work. The function signature for the replacement is fn((binary(), [binary()]) -> iodata() | unicode:charlist()) so the submatches are just [binary()].

}
let regexMatch = new RegexMatch(match, List.fromArray(submatches));
return replacement(regexMatch);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a copy/paste of the code above?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very similar, but not identical. I originally pulled it out into its own function to reuse, but when I did, my test failed. I didn't dig in to understand why the above code iterated backwards, but when I iterated forwards, the test passed.

I'm not opposed to trying to find a way to unify the two, but I didn't want to go altering existing code that works without discussing it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at it again, I bet I didn't account for the n - 1 in the scan implementation since the match array in scan has the whole match string as the first element, whereas this array only holds the submatches.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a shame there's such similar code here! Can they not be unified?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they can. I'll take a stab at it later today.

@ryanmiville
Copy link
Author

I'll keep thinking on the name, but I don't have any good ideas at the moment. I tried looking at other languages without overloading for inspiration, but only saw ReplaceFunc in Go, which didn't feel very gleam-y to me.

@lpil
Copy link
Member

lpil commented Dec 10, 2024

match_map was suggested in discord, what do you think?

@ryanmiville
Copy link
Author

match_map was suggested in discord, what do you think?

That seems good to me.

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

Successfully merging this pull request may close these issues.

Add a function to replace matches with a callback
2 participants