5
$\begingroup$

When I join file names, it's inconvenient to use FileNameJoin. The workaround is to use <> instead, however <> is not exactly like FileNameJoin.

I tried to define an operator . It just doesn't work. If I can define it, I can use it to construct file name like this, "dir1" "dir2" <> "file".

In:

x_ < /> y_ := FileNameJoin[{x, y}]

Out:

Syntax::sntxf: "x_<" cannot be followed by "/>y_".

This is what I do, however I got an error. How can I fix it?

$\endgroup$
6
  • $\begingroup$ it's possible to define new operators using the Notations` package (see other answers for details). But in your case maybe you just want an alias, say FNJ and then you can use x~FNJ~y?The notations all work from box forms, which can cause issues if you embed them in a package. $\endgroup$
    – b3m2a1
    Commented May 31, 2017 at 16:27
  • $\begingroup$ @MB1965, I prefer </>, because it's like <>, but it can add /(path separator) automatically. Thanks, I will try it with Notations`. $\endgroup$
    – webcpu
    Commented May 31, 2017 at 16:50
  • 3
    $\begingroup$ Perhaps you could choose an operator without built-in meanings from the tutorial Operators without Built-in Meanings $\endgroup$
    – Carl Woll
    Commented May 31, 2017 at 17:40
  • 1
    $\begingroup$ E.g., TildeTilde = FileNameJoin@*List: i.sstatic.net/XT6Jg.png $\endgroup$
    – Michael E2
    Commented May 31, 2017 at 17:43
  • 1
    $\begingroup$ Possibly of interest: (27081), (31375) $\endgroup$
    – Mr.Wizard
    Commented May 31, 2017 at 23:52

2 Answers 2

5
$\begingroup$

Suppose you were to choose TildeTilde as Michael E2 suggests. Then you don't need the Symbol Package since has interpretation as an operator already built-in.

TildeTilde[names___] := FileNameJoin[{names}]

Then

TildeTilde[$HomeDirectory, "Desktop", "my_file.dat"]

gives me

"/Users/oldmg/Desktop/my_file.dat"

and more to the point,

$HomeDirectory ≈ "Desktop" ≈ "my_file.dat"

"/Users/oldmg/Desktop/my_file.dat"

There is an input alias for . You can type Esc+~+~+Esc to get . Input aliases seem be defined for most (maybe all?) of the operators with no built-in meaning.

$\endgroup$
5
  • 1
    $\begingroup$ No need to add an alias for \[TildeTilde], it already works. $\endgroup$
    – Carl Woll
    Commented May 31, 2017 at 22:10
  • $\begingroup$ @CarlWoll. +1 I should have checked the docs. It looks like all the unassigned operator symbols have input aliases. I'll update my answer. $\endgroup$
    – m_goldberg
    Commented May 31, 2017 at 22:32
  • 2
    $\begingroup$ Note that you can use WolframLanguageData (or EntityValue) to find out what the keyboard shortcuts are, e.g., WolframLanguageData["TildeTilde", "KeyboardShortcuts"] $\endgroup$
    – Carl Woll
    Commented May 31, 2017 at 23:06
  • $\begingroup$ @CarlWoll. That seems like more trouble than consulting the Documentation Center. $\endgroup$
    – m_goldberg
    Commented May 31, 2017 at 23:11
  • $\begingroup$ However, if you wanted to verify that all operators in a list have an input alias, it would be a lot easier to use WolframLanguageData than the Documentation Center. $\endgroup$
    – Carl Woll
    Commented May 31, 2017 at 23:15
3
$\begingroup$

I favor the method m_goldberg shows but an alternative is Rojo's SubscriptBox method from Is it possible to define custom compound assignment operators like ⊕= similar to built-ins +=, *= etc?

MakeExpression[
  RowBox[arg : {_, PatternSequence[SubscriptBox["<>", "/"], _] ..}], 
  StandardForm
] := 
  MakeExpression @ RowBox[{"FileNameJoin", "[", "{", ##, "}", "]"}] & @@ 
    Riffle[arg[[;; ;; 2]], ","]

Now:

enter image description here

To enter this operator in Windows type < > (Ctrl+-) /

$\endgroup$
2
  • $\begingroup$ How to type <>/ ? I tried to use the result of SubscriptBox["<>", "/"] // InputForm, however it doesn't work. $\endgroup$
    – webcpu
    Commented Jun 1, 2017 at 8:01
  • 1
    $\begingroup$ @UnchartedWorks I added the exact key presses needed in Windows. $\endgroup$
    – Mr.Wizard
    Commented Jun 1, 2017 at 19:59

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.