34
$\begingroup$

As of version 10, Mathematica sports operator forms for many functions like Map, SortBy, Select which allow you to curry one or more of the arguments. Like

m = Map[myFunction];

which creates a new function m which automatically maps myFunction to any list that's passed to it.

As the question title says, is there a comprehensive list of which functions support this now, or do I just have to recheck every function I'm using from now on, in case this feature was added (and is useful in my current problem)?

Edit: I just found this list buried in the docs, but it's not complete (e.g. GroupBy is missing).

$\endgroup$
22
$\begingroup$

From version 10.3 you can use WolframLanguageData.

WolframLanguageData[EntityClass["WolframLanguageSymbol", "Curryable"]]

As of 11.0, this appears to be the most reliable solution:

enter image description here

Unfortunately, this method is not perfect either: at least TuringMachine is missing from it.

Hope this helps.

$\endgroup$
  • 1
    $\begingroup$ I'm accepting this because it seems like a much more idiomatic solution (and is a lot faster). However, I wanted to note that it's missing CellularAutomaton and SubstitutionSystem. $\endgroup$ – Martin Ender Dec 2 '15 at 7:33
  • 1
    $\begingroup$ As well as TuringMachine, actually. It's interesting that these all belong to the same group of functions. $\endgroup$ – Martin Ender Dec 2 '15 at 13:06
  • $\begingroup$ Quick 10.4 update: the list now also includes StringFreeQ, StringMatchQ, StringPosition, StringReplace and StringReplacePart. The above three functions are still missing. Furthermore, thanks to the 10.4 update, I've noticed that we're also missing function which are curryable via different names. 10.4 added UnequalTo which returns a curried Unequal. Likewise, there's 10.3's EqualTo which curries Equal. I don't know if any other examples like that exist, but they would definitely be worth mentioning in a comprehensive list of these. $\endgroup$ – Martin Ender Mar 8 '16 at 15:34
  • $\begingroup$ After some more testing, I'm switching the checkmark back to ybeltukov's answer. Weirdly enough it still seems more reliable. The only ones it doesn't report that are included here are AssociationThread and Entity and as far as I can tell they have no operator forms in the sense that the others have. $\endgroup$ – Martin Ender Mar 8 '16 at 15:44
  • $\begingroup$ 11.0 update: checkmark goes back to this one. AssociationThread and Entity have been removed from the list. It also includes FoldList and AlphabeticOrder which are missing from ybeltukov's solution. While CellularAutomaton and SubstitutionSystem have been added to this, neither method currently returns TuringMachine, which is slightly odd. $\endgroup$ – Martin Ender Sep 15 '16 at 8:37
33
$\begingroup$

Edit: version 11.0 update. Now it is superset of TypeSystem`$OperatorForms. It now also includes operator forms that have different names than the functions they're currying, like EqualTo.

One can find these functions in the documentation by keywords "operator form"

Select[Names@"*", StringMatchQ[ToString@ToExpression[# <> "::usage"], 
    "*operator form*"] &]
{"AllTrue", "AnyTrue", "Append", "Apply", "AssociationMap", \
"Between", "Cases", "CellularAutomaton", "ContainsAll", \
"ContainsAny", "ContainsExactly", "ContainsNone", "ContainsOnly", \
"Count", "CountDistinctBy", "CountsBy", "DatabinAdd", "Delete", \
"DeleteCases", "DeleteDuplicatesBy", "EqualTo", "Extract", \
"FeatureDistance", "FirstCase", "FirstPosition", "Fold", "FreeQ", \
"GreaterEqualThan", "GreaterThan", "GroupBy", "Insert", "KeyDrop", \
"KeyExistsQ", "KeyFreeQ", "KeyMap", "KeyMemberQ", "KeySelect", \
"KeySortBy", "KeyTake", "KeyValueMap", "LessEqualThan", "LessThan", \
"Lookup", "Map", "MapAt", "MapIndexed", "MatchQ", "MaximalBy", \
"MemberQ", "Merge", "MinimalBy", "NoneTrue", "Position", "Prepend", \
"Replace", "ReplaceAll", "ReplacePart", "Scan", "Select", \
"SelectFirst", "SortBy", "StringCases", "StringContainsQ", \
"StringDelete", "StringEndsQ", "StringFreeQ", "StringMatchQ", \
"StringPosition", "StringReplace", "StringReplacePart", \
"StringStartsQ", "SubstitutionSystem", "TakeLargest", \
"TakeLargestBy", "TakeSmallest", "TakeSmallestBy", "UnequalTo"}

Unfortunately, this list is missing at least AlphabeticOrder, FoldList and TuringMachine.

$\endgroup$
  • 6
    $\begingroup$ I believe you meant Select[StringMatchQ[ToString@ToExpression[# <> "::usage"], "*an operator form of*"] &][Names@"*"] $\endgroup$ – Daniel Lichtblau Sep 18 '14 at 21:57
  • $\begingroup$ Oddly enough, my V10 documentation centre doesn't seem to be up to date, although I just installed it last week. E.g. the AssociationMap documentation doesn't contain the operator form yet. Any clue how I can update this? (Searching for "update documentation" etc just leads to results of updating data in Mathematica programs.) $\endgroup$ – Martin Ender Sep 19 '14 at 7:33
  • $\begingroup$ @MartinBüttner It was added in recent V10.0.1 update $\endgroup$ – ybeltukov Sep 19 '14 at 10:56
  • $\begingroup$ @ybeltukov Ah I see. Thanks for the quick answer, btw! $\endgroup$ – Martin Ender Sep 19 '14 at 12:46
  • 2
    $\begingroup$ "V10 documentation centre doesn't seem to be up to date" - what a shock. $\endgroup$ – alancalvitti Dec 2 '15 at 1:24
22
$\begingroup$

There is a global variable that purports to contain a list of operator forms, TypeSystem`$OperatorForms. I say "purports", because the list is missing some of the forms found by @ybeltukov's method. But to its credit, it correctly identifies the operator form of ReplaceAll which is mentioned in neither a usage message nor the documentation, and also correctly omits StringCases which has no operator form despite the usage message claiming otherwise:

Needs["TypeSystem`"]

$typeSystem = SymbolName /@ TypeSystem`$OperatorForms;

$usages =
  Select[
    Names@"*"
  , StringMatchQ[ToString@ToExpression[#<>"::usage"],"*an operator form of*"]&
  ];

$usages ~Complement~ $typeSystem
(* {AssociationMap,CountDistinctBy,DeleteDuplicatesBy,MapAt,Merge,StringCases} *)

$typeSystem ~Complement~ $usages
(* {ReplaceAll} *)

There are three other operators which exhaustively partition TypeSystem`$OperatorForms into the categories "left", "right" and "3-2":

TypeSystem`$LeftOperatorForms

(* {Map,KeyMap,Apply,Scan,MapIndexed} *)

TypeSystem`$RightOperatorForms

(* {Prepend,Append,GroupBy,CountsBy,Count,Position,FirstPosition,SortBy,KeySelect,
    Select,SelectFirst,Delete,Extract,Replace,ReplacePart,ReplaceAll,Cases,
    DeleteCases,FirstCase,AnyTrue,AllTrue,NoneTrue,KeyExistsQ,KeySortBy,
    KeyTake,KeyDrop,MaximalBy,MinimalBy,MatchQ,FreeQ,MemberQ} *)

TypeSystem`$ThreeTwoOperatorForms

(* {Insert} *)

These categories appear to identify the position(s) of the curried arguments.

The results shown here are from version 10.0.1.

$\endgroup$
  • $\begingroup$ I think it'll be good to note that one has to load the TypeSystem package for this to work. $\endgroup$ – RunnyKine Sep 29 '14 at 1:21
  • $\begingroup$ @RunnyKine Good idea. Done. $\endgroup$ – WReach Sep 29 '14 at 1:25

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.