2

How can this rule

a -> {1, 2, 3}

be transformed into this

{a -> 1, a -> 2, a -> 3}

by way of a function?

Answers 3175 and 10524 only address extracting values from the right-hand side, so (as far as I can tell) this question is distinct.

higgy
  • 23
  • 4
  • 1
    I don't really understand what you mean by "without referring to the symbol/string on the left-hand side explicitly in the code". Can you give an example? It would be best to have an example input and an example output. – Szabolcs Nov 14 '14 at 20:30

1 Answers1

4

Use Thread

Thread[ a -> {1, 2, 3} ]

and

Join @@ Thread /@ {a -> {1, 2, 3}, b -> {4, 5, 6}}

You might also be interested in this.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • How would this be done without knowing that "a" is "a". In your answer, you refer to "a" in your code. I'll update the question to make it clearer. A related question would be: How do you extract the left-hand side of a rule efficiently? – higgy Nov 14 '14 at 20:27
  • @higgy a -> b is just an alternate notation for Rule[a,b], so you can extract a using First, Part, Extract, etc. See here: http://reference.wolfram.com/language/tutorial/PartsOfExpressions.html – Szabolcs Nov 14 '14 at 20:29
  • If extracting the left-hand side is a necessary step in all possible solutions to this question, then edit your answer I and will accept it. I'm also curious as to whether there are solutions that don't require extracting the left-hand side first, i.e., something more direct. – higgy Nov 14 '14 at 20:34
  • @higgy You don't need to know the first part of the rule to apply Thread to it. If you have x = a -> {1,2,3} then you can just use Thread[x], no need to know what's inside x, other than that it's a rule with a list on the RHS. Can you give an example input and a corresponding example output? I thought a -> {1, 2, 3} and {a -> 1, a -> 2, a -> 3} could be considered such. Thread takes you from one to the other. – Szabolcs Nov 14 '14 at 20:35
  • 1
    @higgy No, it's not necessary. I think we're having a communication breakdown. Why don't you join chat? – Szabolcs Nov 14 '14 at 20:36
  • In chat. Any way to freeze this question until after we've chatted? – higgy Nov 14 '14 at 20:40
  • This answers my question. I forgot that x -> y could be expressed as Rule[x,y], and therefore be used with Thread. – higgy Nov 14 '14 at 21:17