Skip to content

Argument order rules of thumb #378

Open
@practicalli-johnny

Description

@practicalli-johnny

https://groups.google.com/u/0/g/clojure/c/iyyNyWs53dc?pli=1

  1. Variable arg lists have to go last;
  2. The "operand," i.e. the object primarily being "operated upon,"
    goes last, to make partial application easier and nested expressions
    easier to read;
  3. If a function/macro takes a collection, the collection goes last.

Skimming through the API, it seems like most of the built-in functions
fit these rules, with a few exceptions:
conj contains? find get nth nthrest select-keys subs subvec with-meta
set/project

Hmmm... Good thing those weren't the rules or assoc/dissoc couldn't
have become variadic. I know it might seem arbitrary, but it's
(usually :) not.

A better characterization (but not a rule, no promises) might be that
the sequence functions take the seq last, and the collection functions
take the collection first.

I think with #() the partial application story is always easy - no
matter what the argument order you often want to bind args other than
the first.
Rich

I think the tradition is more general: abstract process which need missing process passed in(seems all high order functions under this category) should have missing process comes first as it's parameter.
This tradition not only limited to sequence parameter.

Also can we consider that put some special argument in some special position.

The main problem data, which often be the criteria of function dispatch, similar to OO's method target object, can as first parameter or after the missing process parameter?
So we can have a consistent parameter order model:
missing-process-param*, main-problem-data-param, additional-param*

With this model maybe we get better use of "->" to focus on & thread main problem:
E.g.
(->
main-problem-data
concrete-func-1
(concrete-func-2 additional-param)
((partial abstract-func-1 missing-process-1 missing-process-2))
((partial abstract-func-2 missing-process) additional-param))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions