2

When passing around functions the term "predicate" is often used for a [short] function that returns a boolean. Is there a term for a function that selects a single field on an object?

For example let's say we build a custom sort function, but this sort is generic so it takes an argument for the user to specify which field to sort on. Such a function might look something like:

sort(itemsToSort, functionThatGetsFieldToSortOn)

The items might be an array of objects with a filename property.

[{ filename: 'foo' }]

If I were to sort based on filename with the function I might invoke a call like:

sort(items, a => a.filename)

This second argument...is there a special name for this type of function that selects a property?

I'm trying to figure out a good way to name this second argument.

7

This is usually called a "getter".

In your sort() example, you actually need a "comparator", something that compares two items of a given type and tells you how they compare (less, equal, greater).

  • 1
    You can write a sort that takes a unary function and uses less than on the results, just as easily as one that takes a binary comparator – Caleth Jan 4 '18 at 23:29
  • @Caleth: You can do that when your setter is returning a simple key with an obvious ordering that is native to the programming language in use. Most languages do not provide obvious orderings on composite keys. Using a comparator allows you to specify any ordering you want, be it obvious, non-obvious, or even bizarre, on any key you want, simple, composite, or computed on-the-fly as a function of one or more fields and/or one or more other things. – John R. Strohm Jan 5 '18 at 1:45
  • I didn't say getter, I said unary function. In the general case you can have "any ordering you want, be it obvious, non-obvious, or even bizarre, on any key you want, simple, composite, or computed on-the-fly" with that, too – Caleth Jan 5 '18 at 11:31
  • a => a.filename is a unary free function. Foo::filename may be a getter, and may also be usable as a unary function (directly or as foo => foo.filename() or somesuch) – Caleth Jan 5 '18 at 11:46
  • @Caleth: You are assuming in general the existence of a mapping from whatever simple, composite, or ad hoc on-the-fly key type you chose to (a subset of) the real numbers (i.e, something that the language knows how to compare), where that mapping preserves the desired ordering of the keys. It is not clear that such a mapping will always exist. – John R. Strohm Jan 5 '18 at 16:09
4

A function that selects something from an object is often called a selector. Microsoft uses that term for the argument of the Select-function: https://msdn.microsoft.com/de-de/library/bb548891(v=vs.110).aspx. Other examples are the name keySelector for the argument of the GroupBy-function: https://msdn.microsoft.com/de-de/library/bb534501(v=vs.110).aspx

If you want to indicate what the selector is for, give it a more specific name, like keySelector, propertySelector oder fieldSelector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.