magnars / dash.el

A modern list library for Emacs
GNU General Public License v3.0
1.67k stars 137 forks source link

group/groupby and partitioning functions (naming) in general #49

Open Fuco1 opened 11 years ago

Fuco1 commented 11 years ago

There's the group/groupby from Data.List, which is pretty useful. It's not yet present in dash and it generalizes -partition-by. The docs from hackage:

The group function takes a list and returns a list of lists such that the concatenation of the result is equal to the argument. Moreover, each sublist in the result contains only equal elements. For example,

 group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"]

It is a special case of groupBy, which allows the programmer to supply their own equality test.

There already is -group-by in dash but with different semantics. I would advice to alias the function and deprecate the old name, then add the proposed functions with that name (after some time, so people can migrate their code).

The simple group could be omitted since it's just groupBy (==). -partition-by from dash can be implemented as (haskell code)

partitionBy f = groupBy ((==) `on` f)

Speaking about function names, I would strongly push towards regularizing the names of functions based on the haskell API. There isn't any API in elisp, so we can name it whatever, and having the same API as haskell would be benefitial for haskell programmers (and others from languages that inspired their APIs by haskell, which is a lot), while not being any drawback for elisp programmers who would need to learn some API anyway.

For example, -separate in haskell is partition, while -partition has completely differnent semantics in dash. -split-with is called span (span the predicate over longest prefix). The current -partition-* could probably be called more precisely too, something to do with sublists.

Also the usage of -with and -by is inconsistent in dash. Sometimes they take functions (a -> b) and sometimes predicates (a -> a -> Bool). Suffix -by should always take predicates or comparators and -with unary functions.

magnars commented 11 years ago

You have several valid points going here. But before I digest them fully, let me start by saying that the API and naming is based on Clojure. So it seems like getting names to line up with both wont work.

Fuco1 commented 11 years ago

Oh, okey then. Clojure is probably better role model for lisp dialect then :P