I wrote this code as a first pass (add a tuple to one of a list’s lists depending on it’s value).
def split({:C,x}, [cl, di, he, sp]), do: [slot({:C,x},cl), di, he, sp]
def split({:D,x}, [cl, di, he, sp]), do: [cl, slot({:D,x},di), he, sp]
def split({:H,x}, [cl, di, he, sp]), do: [cl, di, slot({:H,x},he), sp]
def split({:S,x}, [cl, di, he, sp]), do: [cl, di, he, slot({:S,x}, sp)]
then it “worried” me that I was deconstructing a value that I had to use, suspecting it was creating a new tuple … so I rewrote it to this
def split2(card, [cl,di,he,sp]) do
case card do
{:C,_} -> [slot(card, cl), di, he, sp]
{:D,_} -> [cl, slot(card, di), he, sp]
{:H,_} -> [cl, di, slot(card, he), sp]
{:S,_} -> [cl, di, he, slot(card, sp)]
end
end
Is one preferable to the other? Does it make a difference?
I’ll admit the impetus for this is a probably misunderstanding on my part about functional programming and “value re-use” (is that a term) a-la new_list = [ new_term | old_list] … not creating a new copy of old_list.
Having started as a C programmer, I find it hard not to worry about how stuff works/where data lives “under the hood” … I suspect the answer is “don’t think like that” …
Jon