12
$\begingroup$

It's convenient to abbreviate often-used function names, eg Dim = Dimension.

Is it possible to abbreviate long context symbols, e.g. Combinatorica when the context is not inserted in the context path, like with

Block[{$ContextPath}, Needs["Combinatorica`"]]

Com = Combinatorica doesn't work, eg: Com`Star[6] is fixed by the interpreter. Is there a way to abbreviate it?

$\endgroup$
6
  • $\begingroup$ One of the actual strengths of the Mathematica language design is that it uses spelled-out, meaningful names rather than verbose but possibly cryptic names. $\endgroup$
    – murray
    Commented Jul 15, 2012 at 0:00
  • $\begingroup$ @murray, I aint got time for that $\endgroup$ Commented Jul 15, 2012 at 6:13
  • $\begingroup$ My long experience with such things, going back to APL, is that it takes more time to recall the correct terse, cryptic abbreviation than to type a longer but descriptive name. Of course this is predicated upon good touch-typing skills. $\endgroup$
    – murray
    Commented Jul 15, 2012 at 13:43
  • $\begingroup$ In exploratory data analysis there's much testing of properties of data structures that is then deleted, so not only do I abbreviate, eg, Dimension, but I use lower case 'dim' because speed is of the essence. What do you think of that? $\endgroup$ Commented Jul 15, 2012 at 14:12
  • $\begingroup$ @alancalvitti Are you aware of the Cmd-K shortcut (on a mac) to autocomplete? So Dim Cmd-K will give you Dimension $\endgroup$
    – rm -rf
    Commented Jul 15, 2012 at 14:27

2 Answers 2

13
$\begingroup$

I am not sure these are the best ways but they should work. You could do what you did with Dimensions for all the symbols in Combinatorica`

For example, running this

replaceAndLoad[context_String -> toContext_String] := 
 Block[{$ContextPath},
  Needs[context];
  Scan[ToExpression[
     toContext <> StringReplace[#, context ~~ sym__ :> sym] <> 
      "=" <> #] &, Names[context <> "*"]]
  ]

Now, do

replaceAndLoad["Combinatorica`" -> "Com`"]

You could also overload MakeExpression to change Com to Combinatorica at the parsing stage... But I would probably do something different and basic, like an InputAlias or InputAutoReplacement. With these you could even replace them with a framed box in a special color that means "context combinatorica", or a tooltip, or simply an Interpretation that writes Com`stuff but is interpreted as Combinatorica`...

$\endgroup$
10
$\begingroup$

This question is not too distantly related to my own on StackOverflow:

Exposing Symbols to $ContextPath

Leonid provides an interesting approach there that could be adapted to your problem.

Rojo provides a solution that creates "proxy symbols" but these are not 100% equivalent. Information no longer works correctly for example:

?? Com`KSubsets

Mathematica graphics

This only gives the name of the Symbol it points to an not the usage message or the definition.

I prefer using $PreRead for situations like this (e.g. here). Although it only works interactively it has the advantage of using the actual symbols rather than proxies, so all the functions like Unprotect, Information, etc. work correctly.

Block[{$ContextPath}, Needs["Combinatorica`"]]

$PreRead = # /. 
    name_String?(StringMatchQ[#, "Com`*"] &) :> 
     With[{nn = StringReplace[name, "Com`" -> "Combinatorica`"]}, 
      nn /; NameQ[nn]] &;

Now Information et al work correctly:

?? Com`KSubsets
$\endgroup$
10
  • $\begingroup$ Very nice Mr.Wizard - maybe I should have waited to accept. I had no time to test these issues $\endgroup$ Commented Jul 15, 2012 at 6:15
  • $\begingroup$ @alancalvitti as you may know you can always change the Accept later, but it is probably a good idea to wait a bit longer before placing it. Also I'm not claiming that this method is superior and I was specifically looking for an alternative to it when I posted the SO question referenced. Nevertheless I find it to have the best characteristics when it is applicable. $\endgroup$
    – Mr.Wizard
    Commented Jul 15, 2012 at 6:18
  • $\begingroup$ Just when I thought I finally got a grip on Mma you made me aware that it was just an illusion. :-( $\endgroup$ Commented Jul 15, 2012 at 7:01
  • $\begingroup$ @ndroock1 I have the same feeling sometimes. I'm curious though, what specifically makes you say that? $\endgroup$
    – Mr.Wizard
    Commented Jul 15, 2012 at 7:04
  • 1
    $\begingroup$ @alancalvitti, Remember that there's only one $PreRead across the kernel, so if you want to add another context abbreviation or use it in any other way at the same time, you have to be careful $\endgroup$
    – Rojo
    Commented Jul 15, 2012 at 14:13

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.