Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am struggling with levels and #, if I understand this I think I will be able to grasp the entire concept.

 Apply[#1 &, {envelope[1, order["sku1", 5, 10, 4.98]], 
  envelope[2, order["sku2", 3, 67, 98]]}, {1}]
{1,2}

This makes perfect sense.

Keeping everything els equal but changing the slot to #2:

{order["sku1", 5, 10, 4.98], order["sku2", 3, 67, 98]}

This makes also perfect scence.

Then I am going back to #1 but now at level 2

{envelope[1, "sku1"], envelope[2, "sku2"]}

Why do I get this output I was only expecting Sku1 and Sku2.

I also wonder why Depth in this case equals 4, but if I put level 3 I get the entire list?

I know this is very simple but I have been struggling with seeing the logic.

I have spent quite a lot of time trying to grasp this so please try make it as clear as possible. And If you do not understand the question or think it is poorly explained give me a comment before downgrading the question.

share|improve this question
    
Duplicate of "Levels: How do they work?" –  rm -rf Sep 25 '13 at 1:17
    
@rm-rf I think it is closely related. Although convolved, OP's problem seems to more about Apply than the Levels which is well answered by Murta. –  Kuba Sep 25 '13 at 6:17

1 Answer 1

up vote 3 down vote accepted

It's easier to understand using Map. Using:

list ={envelope[1,order["sku1",5,10,4.98]],envelope[2,order["sku2",3,67,98]]}

if you do Map[f,list,{2}], you get:

{envelope[f[1],f[order[sku1,5,10,4.98]]],envelope[f[2],f[order[sku2,3,67,98]]]}

The trick here is that Apply when used in an Atom is the Atom itself.

f @@ 1

1

So with Apply[# &, list, {2}] you get:

{envelope[1,sku1],envelope[2,sku2]}

Levels are nicely visualized using TreeForm:

enter image description here

This post has a much more deeper and illustrative Level explanation.

share|improve this answer
    
Murta you talked about Atom, what does that actually mean? I understand Map, but Apply I do not. –  ALEXANDER Sep 25 '13 at 4:29
    
@ALEXANDER ref/AtomQ, guide/Atomic Elements of Expressions –  Kuba Sep 25 '13 at 6:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.