3

I'm trying to do something like below, but it doesn't work, since StringForm requires each item from parts to be given individually, and not in a list:

parts = ToString /@ {1, 2, 3};
(* this should be StringForm["`` `` ``", 1, 2, 3] and 
   not StringForm["`` `` ``", {1, 2, 3}] *)
StringForm["`` `` ``", parts]

I think this could be done with Flatten and Apply, but can I avoid using Apply? (looks ugly)

Kuba
  • 136,707
  • 13
  • 279
  • 740
Meh
  • 1,637
  • 2
  • 13
  • 17
  • How about: parts = Sequence @@ ToString /@ {1, 2, 3} – RunnyKine Oct 14 '13 at 02:49
  • @RunnyKine Yep, that was exactly what I was looking for. I read about Sequence and tried using it, but I missed the @@ part – Meh Oct 14 '13 at 03:15
  • @RunnyKine I notice you used @@ which is shorthand for Apply, which the OP wanted to avoid. – Sjoerd C. de Vries Oct 14 '13 at 07:17
  • @SjoerdC.deVries I think OP meant Apply[f, expr] form. – Kuba Oct 14 '13 at 07:25
  • @kuba In that case, StringForm["`` `` ``", ##]& @@ parts would be better IMHO. No need for Sequence. -- Oh, forget it, I just saw you already did that. – Sjoerd C. de Vries Oct 14 '13 at 11:44
  • @SjoerdC.deVries I agree, well OP accepted an answer with Apply even though was asking to avoid it so it's hard to be sure what it is about :) (my guess was that he want to avoid explicitly written Apply) – Kuba Oct 14 '13 at 11:47
  • Yes, I wanted to avoid to explicitly write Apply – Meh Oct 14 '13 at 15:07

2 Answers2

3

For StringForm you don't have to use ToString on arguments.

One way to avoid Apply, which was introduced to me by rm-rf, was to use Operate:

parts = {1, 2, 3};
StringForm["`` `` ``", Operate[Sequence &, parts]]
1 2 3

Also, if you want to use Apply in slightly different way than RunnyKine:

parts = {1, 2, 3};

StringForm["`` `` ``", ##] & @@ parts
1 2 3
Kuba
  • 136,707
  • 13
  • 279
  • 740
2

How about:

 parts = Sequence @@ ToString /@ {1, 2, 3}
RunnyKine
  • 33,088
  • 3
  • 109
  • 176