ngs-lang / ngs

Next Generation Shell (NGS)
https://ngs-lang.org/
GNU General Public License v3.0
1.46k stars 41 forks source link

Implement replace(Str, Pfx, Str) #555

Open ilyash-b opened 2 years ago

ilyash-b commented 2 years ago

Old incorrect behavior:

ngs -pi '"/abc".replace(Pfx("/"), "GG")'
Arr of size 4
  [0] = /
  [1] = a
  [2] = b
  [3] = c
Rukopet commented 2 years ago

I`m on it

ilyash-b commented 2 years ago

Bonus tasks: in addition to Pfx, this could also be implemented for MaybePfx, Sfx, MaybeSfx, Ifx, MaybeIfx.

Note that Pfx('something') actually returns MustPfx('something')

The difference between Must... and Maybe... is when the prefix/suffix are not present:

ilyash-b commented 2 years ago

Went through issues. This issue is actually a specific case of #301

Rukopet commented 1 year ago

@ilyash-b Maybe it's better not to create extra keywords for Pfx, MustPfx and etc., but to use function arguments?

str.replace(str, str, bool throw_exception, int max_replaces)'

Or we can provide 2 functions for string object

str.replace(str, str, max_replaces)'
str.strict_replace(str, str, max_replaces) # will throw exception if subseq. not found

What you think?

ilyash-b commented 1 year ago

Maybe it's better not to create extra keywords for Pfx, MustPfx and etc., but to use function arguments?

Pfx and friends are not keywords (nor parameter names). They are types. Semantically meaningful types. This design is aligned with the overall design of the language.

throw_exception

Not aligned with everything else. No other method takes such parameter (unless I missed.. but they really shouldn't).

max_replaces - nice idea, need to think about this

ilyash-b commented 1 year ago

str.replace ... str.strict_replace

The overall intention in NGS is to have small amount of verbs (method names) and rely heavily on multiple-dispatch.

ilyash-b commented 1 year ago

What do you think, @Rukopet ?