37
$\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$
0

3 Answers 3

23
$\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$
6
  • 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$ Dec 2, 2015 at 7:33
  • 1
    $\begingroup$ As well as TuringMachine, actually. It's interesting that these all belong to the same group of functions. $\endgroup$ Dec 2, 2015 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$ Mar 8, 2016 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$ Mar 8, 2016 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$ Sep 15, 2016 at 8:37
34
$\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$
7
  • 6
    $\begingroup$ I believe you meant Select[StringMatchQ[ToString@ToExpression[# <> "::usage"], "*an operator form of*"] &][Names@"*"] $\endgroup$ Sep 18, 2014 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$ Sep 19, 2014 at 7:33
  • $\begingroup$ @MartinBüttner It was added in recent V10.0.1 update $\endgroup$
    – ybeltukov
    Sep 19, 2014 at 10:56
  • $\begingroup$ @ybeltukov Ah I see. Thanks for the quick answer, btw! $\endgroup$ Sep 19, 2014 at 12:46
  • 2
    $\begingroup$ "V10 documentation centre doesn't seem to be up to date" - what a shock. $\endgroup$ Dec 2, 2015 at 1:24
23
$\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$
2
  • $\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, 2014 at 1:21
  • $\begingroup$ @RunnyKine Good idea. Done. $\endgroup$
    – WReach
    Sep 29, 2014 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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