evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.04k stars 120 forks source link

Add Built-Ins `fgroupsof` and `fwindowsof` #844

Open pardouin opened 2 months ago

pardouin commented 2 months ago

Not sure how to contribute exactly but I just had this cool idea inspired by fmap/map. Typically when using groupsof or windowsof with strings, it's pretty common that you actually want to yield strings and not tuples of chars. So for example fgroupsof(3, "ABCDEFGH") would yield "ABC", "DEF", "GH".

evhub commented 2 months ago

Hmmm... this isn't really a Functor operation, so the name fgroupsof doesn't make a ton of sense, but I do see the use case.

If you just want a simple implementation for now, these should work pretty well:

def fgroupsof(n, obj) =
    range(0, len(obj), n) |> map$(start -> obj[start:start+n])

def fwindowsof(n, obj) =
    range(0, len(obj) - n + 1) |> map$(start -> obj[start:start+n])
pardouin commented 2 months ago

Thanks for your answer, the names were indeed poorly chosen. I know how to implement these, just wanted to know if the release of a built-in was an option. Either new dedicated functions, or maybe an optionnal kwarg to preserve the type for the already existing functions? It's not very important, just an idea :-)