Re: [julia-users] Unexpected append! behavior

2014-12-11 Thread Mike Innes
Think of append!(X, Y) as equivalent to X = vcat(X, Y). You called append!
twice, so X gets Y appended twice.

julia X = [1,2]; Y = [3,4];

julia X = vcat(X,Y)
[1, 2, 3, 4]

In your example you went ahead and did this again:

julia X = (X = vcat(X, Y))
[1, 2, 3, 4, 3, 4]

But if you reset X, Y via the first statement and *then* call X =
append!(X, Y), it works as you would expect.

julia X = [1,2]; Y = [3,4];

julia X = append!(X, Y) # same as X = (X = vcat(X, Y))
[1, 2, 3, 4]

On 11 December 2014 at 07:51, Alex Ames alexander.m.a...@gmail.com wrote:

 Functions that end with an exclamation point modify their arguments, but
 they can return values just like any other function. For example:

 julia x = [1,2]; y = [3, 4]
 2-element Array{Int64,1}:
  3
  4

 julia append!(x,y)
 4-element Array{Int64,1}:
  1
  2
  3
  4

 julia z = append!(x,y)
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 julia z
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 julia x
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 The append! function takes two arrays, appends the second to the first,
 then returns the values now contained by the first array. No recursion
 craziness required.

 On Thursday, December 11, 2014 1:11:50 AM UTC-6, Sean McBane wrote:

 Ivar is correct; I was running in the Windows command prompt and couldn't
 copy and paste so I copied it by hand and made an error.

 Ok, so I understand that append!(X,Y) is modifying X in place. But I
 still do not get where the output for the second case, where the result of
 append!(X,Y) is assigned back into X is what it is. It would make sense to
 me if this resulted in a recursion with Y forever getting appended to X,
 but as it is I don't understand.

 Thanks.

 -- Sean

 On Thursday, December 11, 2014 12:42:45 AM UTC-6, Ivar Nesje wrote:

 I assume the first line should be

  X = [1,2]; Y = [3,4];

 Then the results you get makes sense. The thing is that julia has
 mutable arrays, and the ! at the end of append! indicates that it is a
 function that mutates it's argument.




Re: [julia-users] Unexpected append! behavior

2014-12-11 Thread Sean McBane
Ah, I see the error in my thinking now. Knowing what the ! signifies makes 
it make a lot more sense.

Thanks guys for putting up with a newbie. This was probably one of those 1 
in the morning questions that I should have waited to look at the next day 
before asking for help; it seems obvious now.

-- Sean

On Thursday, December 11, 2014 3:26:12 AM UTC-6, Mike Innes wrote:

 Think of append!(X, Y) as equivalent to X = vcat(X, Y). You called append! 
 twice, so X gets Y appended twice.

 julia X = [1,2]; Y = [3,4];

 julia X = vcat(X,Y)
 [1, 2, 3, 4]

 In your example you went ahead and did this again:

 julia X = (X = vcat(X, Y))
 [1, 2, 3, 4, 3, 4]

 But if you reset X, Y via the first statement and *then* call X = 
 append!(X, Y), it works as you would expect.

 julia X = [1,2]; Y = [3,4];

 julia X = append!(X, Y) # same as X = (X = vcat(X, Y))
 [1, 2, 3, 4]

 On 11 December 2014 at 07:51, Alex Ames alexande...@gmail.com 
 javascript: wrote:

 Functions that end with an exclamation point modify their arguments, but 
 they can return values just like any other function. For example:

 julia x = [1,2]; y = [3, 4]
 2-element Array{Int64,1}:
  3
  4

 julia append!(x,y)
 4-element Array{Int64,1}:
  1
  2
  3
  4

 julia z = append!(x,y)
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 julia z
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 julia x
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 The append! function takes two arrays, appends the second to the first, 
 then returns the values now contained by the first array. No recursion 
 craziness required.

 On Thursday, December 11, 2014 1:11:50 AM UTC-6, Sean McBane wrote:

 Ivar is correct; I was running in the Windows command prompt and 
 couldn't copy and paste so I copied it by hand and made an error.

 Ok, so I understand that append!(X,Y) is modifying X in place. But I 
 still do not get where the output for the second case, where the result of 
 append!(X,Y) is assigned back into X is what it is. It would make sense to 
 me if this resulted in a recursion with Y forever getting appended to X, 
 but as it is I don't understand.

 Thanks.

 -- Sean

 On Thursday, December 11, 2014 12:42:45 AM UTC-6, Ivar Nesje wrote:

 I assume the first line should be 

  X = [1,2]; Y = [3,4]; 

 Then the results you get makes sense. The thing is that julia has 
 mutable arrays, and the ! at the end of append! indicates that it is a 
 function that mutates it's argument.




Re: [julia-users] Unexpected append! behavior

2014-12-11 Thread Mike Innes
Happy to help

On 11 December 2014 at 17:04, Sean McBane seanmc...@gmail.com wrote:

 Ah, I see the error in my thinking now. Knowing what the ! signifies makes
 it make a lot more sense.

 Thanks guys for putting up with a newbie. This was probably one of those 1
 in the morning questions that I should have waited to look at the next day
 before asking for help; it seems obvious now.

 -- Sean

 On Thursday, December 11, 2014 3:26:12 AM UTC-6, Mike Innes wrote:

 Think of append!(X, Y) as equivalent to X = vcat(X, Y). You called
 append! twice, so X gets Y appended twice.

 julia X = [1,2]; Y = [3,4];

 julia X = vcat(X,Y)
 [1, 2, 3, 4]

 In your example you went ahead and did this again:

 julia X = (X = vcat(X, Y))
 [1, 2, 3, 4, 3, 4]

 But if you reset X, Y via the first statement and *then* call X =
 append!(X, Y), it works as you would expect.

 julia X = [1,2]; Y = [3,4];

 julia X = append!(X, Y) # same as X = (X = vcat(X, Y))
 [1, 2, 3, 4]

 On 11 December 2014 at 07:51, Alex Ames alexande...@gmail.com wrote:

 Functions that end with an exclamation point modify their arguments, but
 they can return values just like any other function. For example:

 julia x = [1,2]; y = [3, 4]
 2-element Array{Int64,1}:
  3
  4

 julia append!(x,y)
 4-element Array{Int64,1}:
  1
  2
  3
  4

 julia z = append!(x,y)
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 julia z
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 julia x
 6-element Array{Int64,1}:
  1
  2
  3
  4
  3
  4

 The append! function takes two arrays, appends the second to the first,
 then returns the values now contained by the first array. No recursion
 craziness required.

 On Thursday, December 11, 2014 1:11:50 AM UTC-6, Sean McBane wrote:

 Ivar is correct; I was running in the Windows command prompt and
 couldn't copy and paste so I copied it by hand and made an error.

 Ok, so I understand that append!(X,Y) is modifying X in place. But I
 still do not get where the output for the second case, where the result of
 append!(X,Y) is assigned back into X is what it is. It would make sense to
 me if this resulted in a recursion with Y forever getting appended to X,
 but as it is I don't understand.

 Thanks.

 -- Sean

 On Thursday, December 11, 2014 12:42:45 AM UTC-6, Ivar Nesje wrote:

 I assume the first line should be

  X = [1,2]; Y = [3,4];

 Then the results you get makes sense. The thing is that julia has
 mutable arrays, and the ! at the end of append! indicates that it is a
 function that mutates it's argument.





[julia-users] Unexpected append! behavior

2014-12-10 Thread Sean McBane
Hi all,

I'm an engineering student with interests in numerical simulation of, well, 
just about anything, and stumbled across Julia and have been experimenting 
a bit.

I have no idea if this question has been addressed before, but I'm seeing a 
behavior that's not quite what I'd expect from the append! method. At the 
prompt, if I type

 X = [1,2]; Y = [1,2];
 append!(X,Y)
4-element Array(Int64,1):
  1
  2
  3
  4

This is the output that I expect to see. But if I try to store this result 
back in X:

 X = append!(X,Y)
6-element Array(Int64,1):
  1
  2
  3
  4
  3
  4

This is counterintuitive to me. It looks as though the append is recursing 
and adding Y onto the end of the new X obtained from appending... but only 
once. If I'm causing a recursion by this call, why doesn't it continue?

Thanks to anyone who can enlighten me.

-- Sean


Re: [julia-users] Unexpected append! behavior

2014-12-10 Thread John Myles White
Hi Sean,

I'm really confused by the output you're showing.

  X = [1,2]; Y = [1,2];
  append!(X,Y)
 4-element Array(Int64,1):
   1
   2
   3
   4

Do you really get this output? That seems like a huge bug if so. But I don't 
see that at all, which is what I'd expect.

 -- John



Re: [julia-users] Unexpected append! behavior

2014-12-10 Thread Ivar Nesje
I assume the first line should be 

 X = [1,2]; Y = [3,4];

Then the results you get makes sense. The thing is that julia has mutable 
arrays, and the ! at the end of append! indicates that it is a function that 
mutates it's argument.

Re: [julia-users] Unexpected append! behavior

2014-12-10 Thread Sean McBane
Ivar is correct; I was running in the Windows command prompt and couldn't 
copy and paste so I copied it by hand and made an error.

Ok, so I understand that append!(X,Y) is modifying X in place. But I still 
do not get where the output for the second case, where the result of 
append!(X,Y) is assigned back into X is what it is. It would make sense to 
me if this resulted in a recursion with Y forever getting appended to X, 
but as it is I don't understand.

Thanks.

-- Sean

On Thursday, December 11, 2014 12:42:45 AM UTC-6, Ivar Nesje wrote:

 I assume the first line should be 

  X = [1,2]; Y = [3,4]; 

 Then the results you get makes sense. The thing is that julia has mutable 
 arrays, and the ! at the end of append! indicates that it is a function 
 that mutates it's argument.