Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Tue Apr 05, 2011 8:29 pm UTC

I prefer using the command line tool anyways.

Surely I'm not alone?
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
diabolo
Posts: 72
Joined: Fri Aug 08, 2008 4:17 pm UTC
Location: france

Re: Coding: Fleeting Thoughts

Postby diabolo » Tue Apr 05, 2011 8:41 pm UTC

MHD wrote:This is signature material... Only I'm pretty sure it's over 255 chars.

But it looks like each BBCode tag gets counted as only 1 char. For example if you take exactly 255 chars of text and put it in a quote tag (adding 15 chars) you get a warning that your signature contains 257 chars.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Apr 05, 2011 8:47 pm UTC

Sure, I just prefer to waste my time coding, not futzing setting up the dev environment. Plus I actually like the features of IDEs (though, generally not Eclipse very much).

It's almost scary that Eclipse may still have the longest startup time of any program running off my SSD (a few seconds). :shock:

User avatar
khakipuce
Posts: 33
Joined: Thu Apr 08, 2010 9:24 am UTC
Location: UK
Contact:

Re: Coding: Fleeting Thoughts

Postby khakipuce » Tue Apr 05, 2011 9:04 pm UTC

I Use Eclipse 8-9 hours a day, best thing since sliced bread - I only start it up once each day so the start up time is not a big overhead - but the tools and plugins it has are the difference between a hammer and a fully kitted engineering workshop. You can do with one click stuff you would never dare to do any other way (refactoring, for example).

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Apr 05, 2011 9:10 pm UTC

khakipuce wrote:I Use Eclipse 8-9 hours a day, best thing since sliced bread - I only start it up once each day so the start up time is not a big overhead - but the tools and plugins it has are the difference between a hammer and a fully kitted engineering workshop. You can do with one click stuff you would never dare to do any other way (refactoring, for example).
Oh, undoubtedly, IDEs are awesome, I'm just not particularly partial to Eclipse unless I don't have another viable option.

*Uses Visual Studio for work*

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Tue Apr 05, 2011 10:54 pm UTC

Xeio wrote:Sure, I just prefer to waste my time coding, not futzing setting up the dev environment.

the Android dev guide wrote:

Code: Select all

android create project 
--target <target_ID> 
--name <your_project_name> 
--path path/to/your/project 
--activity <your_activity_name> 
--package <your_package_namespace> 


No futzing required. BTW, all the android plugin for Eclipse does is execute that command. The difference is that you get to wait an extra few seconds for it to start up.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Wed Apr 06, 2011 10:22 am UTC

FT as I played around with SVN for the first time (instead of Git) - how come my repositories are so small? I hotcopied a backup and the size of the whole thing is.. smaller... than the actual project. What's going on? I get if it stores diffs, but what happens if the only working copy gets damaged? Shouldn't it store the whole thing, at least, somewhere?

I guess it's not really a fleeting thought, (more like a "wtf is this" thought), but I guess it doesn't deserve a thread just for musing-ness.
In Minecraft, I use the username Rirez.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Coding: Fleeting Thoughts

Postby Sc4Freak » Wed Apr 06, 2011 11:02 am UTC

I guess svn might be compressing the files, but I don't know if it does that by default.

And svn stores a bunch of stuff in the hidden .svn folders when you check out. These folders contain caches and metadata, so if you want to revert your working copy you don't need to contact the server.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Wed Apr 06, 2011 3:45 pm UTC

Haskell question: Best way of converting a Bytestring to [Word64]? My first thought was a fold, but all the list-like functions for Bytestrings go by Word8 chunks so that seems pretty awful to do.

Basically my DES encryption functions work on [Word64], and I'm trying to write a little file encryption utility using them.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Coding: Fleeting Thoughts

Postby Pesto » Wed Apr 06, 2011 4:52 pm UTC

We use svn at work, and I use git as my subversion client (git-svn).

FT: If you were to use perforce in a subversion client, would you call it perversion?

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Coding: Fleeting Thoughts

Postby Berengal » Wed Apr 06, 2011 5:19 pm UTC

Rippy wrote:Haskell question: Best way of converting a Bytestring to [Word64]? My first thought was a fold, but all the list-like functions for Bytestrings go by Word8 chunks so that seems pretty awful to do.

Basically my DES encryption functions work on [Word64], and I'm trying to write a little file encryption utility using them.

Not sure if this will work, but...

Code: Select all

import Foreign (unsafePerformIO)
import Foreign.Ptr (plusPtr)
import Foreign.ForeignPtr (castForeignPtr, withForeignPtr)
import Foreign.Marshal.Array (peekArray)
import Data.ByteString (ByteString)
import Data.Word (Word64)
import Data.String
import qualified Data.ByteString.Internal as BSI

toWord64List :: ByteString -> [Word64]
toWord64List bs =
  let (ptr, offset, len) = BSI.toForeignPtr bs
      ptr64 = castForeignPtr ptr
      offset64 = offset `div`8 -- Should probably check that offset is divisible by 8 as well
      len64 = len `div` 8
  in unsafePerformIO $ withForeignPtr ptr64 $ \ptr64' -> do
    peekArray len64 (ptr64' `plusPtr` offset64)
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Wed Apr 06, 2011 7:20 pm UTC

That is a treasure trove of new haskell functions. After 15 minutes of hoogling, it looks pretty straightforward, I'm sure it'll work. Thanks!

On a different, only tangentially programming-related topic, I am so pissed at Microsoft SQL Server right now. The business wants this massive query (>50 columns, >50000 rows) in spreadsheet format. No problem, just save as .csv as import into Excel. Crap, the address column has commas which screws things up, and SQL Server can't save with another delimiter. Okay, save as .txt and fiddle with the columns in Excel. Nope, SQL Server gives an out-of-memory error trying to save it. No problem, seen this before, we'll just copy to clipboard and paste it in notepad. ...No, too big to copy to clipboard. Well shit, I've got time, we'll painfully highlight 10000 rows at a time, pasting each piece into notepad, then import into Excel afterwards. It works, except... after a few times, the copy/paste operations silently stops working. Okay, let's try 4000 fucking rows at a time. Finally, it's working! 32000 rows copied and... SQL Server has crashed. God. Damnit. Must've been some fluke, let's try again. 24000 rows copied and... SQL Server has crashed.

...

FFFFFFFUUUUUUUUUUUU

User avatar
Xanthir
My HERO!!!
Posts: 5423
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Apr 06, 2011 8:22 pm UTC

The CSV format has a well-defined way to handle fields which may contain commas. It sucks that MSSQL won't export conformant csv. ;_;
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Coding: Fleeting Thoughts

Postby TheChewanater » Thu Apr 07, 2011 3:08 am UTC

FT:

Code: Select all

= [[0, 0], [0, 0]]
= [[0] * 2] * 2

print a 
#[[0, 0], [0, 0]]
print b #[[0, 0], [0, 0]]

a[0][0] = 1
b
[0][0] = 1

print a 
#[[1, 0], [0, 0]]
print b #[[1, 0], [1, 0]]    


It seems like using * on an array makes the duplicated elements references to the old elements instead of copies. WTF.

Of course, I'd still rather use Python for this project than Java. Unfortunately, it must be in Java; I was only using Python for prototyping.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
hotaru
Posts: 1045
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Coding: Fleeting Thoughts

Postby hotaru » Thu Apr 07, 2011 3:18 am UTC

FT: it'd be really nice if this board had syntax highlighting for more languages than just php.

Code: Select all

factorial product enumFromTo 1
isPrime n 
factorial (1) `mod== 1

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Thu Apr 07, 2011 3:51 am UTC

A similar python array problem:

Code: Select all

>>> a=[0,0]
>>> b=a
>>> c=a
>>> b[0]=1
>>> print b
[1,0]
>>> print c
[1,0]
>>> print a
[1,0]

Surely they should all refer to different arrays that just have the same initial values? I recently found this out the hard way when it messed up a program of mine. Has this been changed in python 3? I'm using 2.6.1.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Coding: Fleeting Thoughts

Postby Sc4Freak » Thu Apr 07, 2011 4:58 am UTC

In Python, I believe that assignment never "copies", it only binds a name to an object. Things like integers appear to have "value semantics" (as opposed to "reference semantics") because they're immutable. But since lists are mutable, you can use the variable "a" to change the contents of the list, and see the results by printing "b" or "c".

In many languages, arrays follow reference semantics anyway, so it's not particularly uncommon. (eg. Java, C#, C++)

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Thu Apr 07, 2011 5:18 am UTC

Hmm, alright, didn't realize. Is there a simple way to do what I want?
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Apr 07, 2011 5:30 am UTC

There's two simple ways to do a shallow copy of a list:

Code: Select all

b = a[:] # slice notation, but getting the whole list
b = [i for i in a]
If you have a list of lists, you can do:

Code: Select all

b = [[j for j in i] for i in a]
b = [i[:] for i in a]
For higher dimensions it's similar.

If you want a deep copy (ie copy the list, and also copy all of the elements of it, with no aliasing) take a look at the copy module.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Thu Apr 07, 2011 6:38 am UTC

I don't think I understand the difference between shallow and deep copy. The wiki article seems to say that with a shallow copy modifying one will modify the other because they point to the same memory block. But with a deep copy that won't happen because the data is actually copied over into a different memory block. Yet I find that both the copy and deepcopy functions seem to do the latter:

Code: Select all

>>> from copy import deepcopy
>>> a=[0,0]
>>> b=deepcopy(a)
>>> print a,b
[0,0] [0,0]
>>> b[0]=1
>>> print a,b
[0,0] [1,0]

Code: Select all

>>> from copy import copy
>>> a=[0,0]
>>> b=copy(a)
>>> print a,b
[0,0] [0,0]
>>> b[0]=1
>>> print a,b
[0,0] [1,0]


And actually, I get the same results from using your other methods too. So yeah, pretty sure I'm misunderstanding the difference.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
diabolo
Posts: 72
Joined: Fri Aug 08, 2008 4:17 pm UTC
Location: france

Re: Coding: Fleeting Thoughts

Postby diabolo » Thu Apr 07, 2011 6:59 am UTC

e^iπ+1=0 wrote:And actually, I get the same results from using your other methods too. So yeah, pretty sure I'm misunderstanding the difference.

a=[0,0] isn't the right example to test on.

Code: Select all

>>> from copy import copy
>>> a=[[0,0],[0,0]]
>>> b = copy(a)
>>> print a, b
[[0, 0], [0, 0]] [[0, 0], [0, 0]]
>>> b[0][0] = 1
>>> print a, b
[[1, 0], [0, 0]] [[1, 0], [0, 0]]

Code: Select all

>>> from copy import deepcopy
>>> a=[[0,0],[0,0]]
>>> b = deepcopy(a)
>>> print a, b
[[0, 0], [0, 0]] [[0, 0], [0, 0]]
>>> b[0][0] = 1
>>> print a, b
[[0, 0], [0, 0]] [[1, 0], [0, 0]]


[insert Inception joke here]

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Thu Apr 07, 2011 7:08 am UTC

Alright, so why do the nested lists make a difference?
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Apr 07, 2011 7:11 am UTC

Code: Select all

a = [[[... snip horrible list of lists ...]]]
b = a
# at this point, a and b refer to the same object... so setting a[0] also changes b[0]
b = copy.copy(a) # or b = a[:] or b = [i for i in a]
# at this point, a and b refer to different objects, but a[0] and b[0] refer to the same object... so setting a[0] doesn't change b[0], but setting a[0][0] does change b[0][0] (unless you changed a[0] first, of course)
b = copy.deepcopy(a)
# at this point, a and b refer to different objects, as do a[0] and b[0], as do a[0][0] and b[0][0], etc. So there is no aliasing between the two lists.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Thu Apr 07, 2011 7:25 am UTC

Oh, alright, I think I understand. b=a means that they refer to the same object, so changing any part of one changes the other. b=copy.copy(a) creates a different object b, but its elements are the same as a's, so changing parts of the elements changes them in both, but changing the elements themselves does not. b=copy.deepcopy(a) creates a different object with different elements, so they are entirely distinct and no change in one will change the other.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Apr 07, 2011 7:34 am UTC

Yep, that's the idea. Here's another piece of code that might help:

Code: Select all

>>> def showwithids(l):
...     contents = ', '.join(showwithids(i) if isinstance(i, list) else repr(i) for i in l)
...     return "%08X[%s]" % (id(l), contents)
...
>>> a = [[1,2],[3,4]]
>>> b = a
>>> print showwithids(a)
7FF4868C[7FF43E2C[1, 2], 7FF3DAAC[3, 4]]
>>> print showwithids(b)
7FF4868C[7FF43E2C[1, 2], 7FF3DAAC[3, 4]]
>>> # Note that all the IDs are the same for both - they're the same list objects in memory.
... b = copy.copy(a)
>>> print showwithids(a)
7FF4868C[7FF43E2C[1, 2], 7FF3DAAC[3, 4]]
>>> print showwithids(b)
7FF4892C[7FF43E2C[1, 2], 7FF3DAAC[3, 4]]
>>> # Note that the ID of the main list is different, but the sublists are still the same - this is a shallow copy.
... b = copy.deepcopy(a)
>>> print showwithids(a)
7FF4868C[7FF43E2C[1, 2], 7FF3DAAC[3, 4]]
>>> print showwithids(b)
7FF4870C[7FF487AC[1, 2], 7FF4872C[3, 4]]
>>> # Note that all the IDs are different now - this is a deep copy.

The IDs are numbers that are guaranteed to be different for any two objects in memory at the same time (I think it's just the memory address where the object is stored). If you tried this yourself, you'd probably get different IDs, but they'd still be equal or not equal to each other as appropriate.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Thu Apr 07, 2011 8:07 am UTC

Right, okay. Thanks!

I can't wait to take an actual programming class. So far the extent of my knowledge is essentially whatever I've managed to glean from the internet, which probably isn't much. I know some basic terms, some basic concepts, some python, and I can usually follow simple programs in other languages. However, the vast majority of programs I've attempted to code have been pretty limited in scope. Most have been project euler problems or things which are similar. That is, primarily math-based things which are relatively short and where I have to mostly think about the math aspect, not the programming aspect. I think the most complicated thing I've coded is Reversi (2-player, not with a computer player).

Any suggestions for what to do to learn more? I know that's a bit vague and that you'd likely point me here, but I find that simply reading through tutorials is often boring. Rather, I like to just try to program something and then look up things as I find I need them. Seems that might be a bad way of doing things, though... I dunno, thoughts?
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Thu Apr 07, 2011 9:20 am UTC

SVN continues to do miracles in... very miraculous ways. It did appear to be working as expected, so I guess it's all systems go.

FT: What exactly is the purpose of static variables/functions in PHP? I can only conceivably think of them as "unique global variables/functions embedded in a class" and as some form of singleton. But then, there are other ways to create singleton classes, why are they still in use?
In Minecraft, I use the username Rirez.

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Thu Apr 07, 2011 12:56 pm UTC

e^iπ+1=0 wrote:Right, okay. Thanks!

I can't wait to take an actual programming class. So far the extent of my knowledge is essentially whatever I've managed to glean from the internet, which probably isn't much. I know some basic terms, some basic concepts, some python, and I can usually follow simple programs in other languages. However, the vast majority of programs I've attempted to code have been pretty limited in scope. Most have been project euler problems or things which are similar. That is, primarily math-based things which are relatively short and where I have to mostly think about the math aspect, not the programming aspect. I think the most complicated thing I've coded is Reversi (2-player, not with a computer player).

Any suggestions for what to do to learn more? I know that's a bit vague and that you'd likely point me here, but I find that simply reading through tutorials is often boring. Rather, I like to just try to program something and then look up things as I find I need them. Seems that might be a bad way of doing things, though... I dunno, thoughts?

You'd be surprised how little CS courses teach practical programming skills; they teach theory and leave it to you to put it into practice. They'll teach you what tail recursion is, what "volatile" means, etc, but the practical stuff like, say, parsing command-line arguments, will be for you to figure out on the assignment.

So really, get motivated and code. Pick a moderately ambitious project, grab a book or some online material, and when you get stuck on your project, read the relevant part of the book, and when that fails, ask questions online. If you're tired of "toy problems" like Project Euler, write something with networking or graphics or complex I/O or something tricky like a genetic algorithm or procedural generation. If tutorials are too flakey, grab a book for something a bit more structured.

There's nothing wrong with diving into a learning project. You won't retain that tutorial information anyway unless you're actually mucking around with code. CS courses will teach you awesome stuff someday, but it will always be up to you to really put it into practice.

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Thu Apr 07, 2011 9:42 pm UTC

Hmm, alright, I think I'll buy a book. Seems to me they'd tend to be better edited, better structured, and more interesting.

Thanks for the advice.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Re: Coding: Fleeting Thoughts

Postby Rippy » Fri Apr 08, 2011 7:01 pm UTC

It comes down to preference. Some say books are a waste because you can find all the information for free online nowadays anyway, which is true, but I like them (provided it's a quality book). You can always borrow one from the library if you're not sure you want to blow >$50.

(I would also read reviews first, because there are a *lot* of shitty books out there from what I hear)

_Axle_
Posts: 253
Joined: Fri Sep 24, 2010 7:33 pm UTC

Re: Coding: Fleeting Thoughts

Postby _Axle_ » Fri Apr 08, 2011 10:32 pm UTC

If you buy any programming books, I would suggest Algorithm books. Most language specific books just show examples and references that can be useful, but are all easily available online for free.
Yakk wrote:Computer Science is to Programming as Materials Physics is to Structural Engineering.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Sat Apr 09, 2011 3:19 am UTC

The other thing I'd say a good book provides is it aggregates a lot of information in one place, and presents it in a uniform way. If you're trying to learn programming, this is extremely valuable. It means you don't have to go hunting across 20 different websites, dealing with 20 different ways of explaining thing, 25 different coding habits, etc., evaluating each one of them. (The downside is that you are only presented one view of some things.)

This doesn't mean that you have to go out to the store to get it... there are some free books online. E.g. Dive Into Python. But this isn't universal; e.g. I don't know of a website I could point someone to if they wanted to go learn C++. I do have printed book recommendations for C++.

You can also read it on the bus. :-)

User avatar
MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

Re: Coding: Fleeting Thoughts

Postby MHD » Sat Apr 09, 2011 2:36 pm UTC

python's bind by reference thing does have it's fun applications:

Code: Select all

>>> a = []
>>> a.append(a)
>>> a
[[...]]
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Sat Apr 09, 2011 4:20 pm UTC

Wait... what? What does that even mean? Should a just be [[]]?
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Sat Apr 09, 2011 4:44 pm UTC

But the inner list isn't empty, it contains the same thing as the outer list. They are the same, after all. The ellipsis is a way of saying "here be circular references."

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Sat Apr 09, 2011 5:26 pm UTC

Shouldn't it be the same idea as a=a+1? That doesn't make a infinity.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Sat Apr 09, 2011 5:55 pm UTC

There are two things in play here. First is that lists in Python are mutable, and integers are not. So when you update the value of an integer, it gets replaced by a new one. When you update the value of the list, that list itself gets updated. It might be easiest to see this in action:

Code: Select all

>>> x = 0
>>> a = [x,x]
>>> a
[0,0]
>>> a[0] += 1
>>> a
[1,0]
>>> y = [0]
>>> b = [y,y]
>>> b
[[0],[0]]
>>> b[0][0] += 1
>>> b
[[1],[1]]


Second is that there's a difference between a reference to a thing and the thing itself (at least in languages like Python). Take the following example:

Code: Select all

>>> x = []
>>> y = x
>>> z = []

Here, x, y, and z are variables that all point to empty lists. Here though, x and y point to the same empty list, while z points to a different one. So if you do "x.append(z)", you end up with "[[]]". If you do "x.append(y)" instead though, you end up with "[[...]]", because x and y point to the same object.

Interestingly (and I didn't know this before), this seems to be a case when the "x += y" idiom behaves differently from "x = x + y":

Code: Select all

>>> a = []
>>> a += [a]
>>> a
[[...]]
>>> b = []
>>> b = b + [b]
>>> b
[[]]

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Sat Apr 09, 2011 6:25 pm UTC

b.i.o wrote:There are two things in play here. First is that lists in Python are mutable, and integers are not. So when you update the value of an integer, it gets replaced by a new one. When you update the value of the list, that list itself gets updated.

Right, I understand that.

Second is that there's a difference between a reference to a thing and the thing itself (at least in languages like Python)

Yes, I understand that too.

Here, x, y, and z are variables that all point to empty lists. Here though, x and y point to the same empty list, while z points to a different one.

This much I get.

So if you do "x.append(z)", you end up with "[[]]". If you do "x.append(y)" instead though, you end up with "[[...]]", because x and y point to the same object.

This is the bit I don't. I realize that my a=a+1 example used integers, but that was (I thought) beside the point I was trying to make. It takes the value of a at the time, adds 1 to it, and stores that back in a. a.append(a) should similarly take the value of a at the time and add it to the list a. It shouldn't reevaluate that statement to take the new value of a over and over again.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Sat Apr 09, 2011 6:51 pm UTC

e^iπ+1=0 wrote:
So if you do "x.append(z)", you end up with "[[]]". If you do "x.append(y)" instead though, you end up with "[[...]]", because x and y point to the same object.

This is the bit I don't. I realize that my a=a+1 example used integers, but that was (I thought) beside the point I was trying to make. It takes the value of a at the time, adds 1 to it, and stores that back in a. a.append(a) should similarly take the value of a at the time and add it to the list a. It shouldn't reevaluate that statement to take the new value of a over and over again.


Ah, there's where the misunderstanding comes from. There's no reevaluation of that.

This might be easier to explain if you consider what's going on at a lower level.

Suppose a is at address 0x1000. The Python runtime then gets to a.append(a). It evaluates the argument -- a -- and figures out that the result is at address 0x1000. It then mutates a to put 0x1000 at the end of the list. [Or perhaps some "reference, 0x1000" pair or something like that; I don't know the exact internal representation. It doesn't matter.]

Now a is the list [0x1000]. But here's the clincher: a -- and thus that list -- is still at 0x1000, because lists in Python are mutable and that's what append does.

So the machine hasn't gone back and reevaluated a.append(a) at all: it just twiddled some pointers.

The "reevaluation" comes when you actually look at a. Suppose the machine now prints it out, and suppose it didn't have logic to determine when there are circularities. It looks at a, sees it holds a reference to 0x1000. It looks at that address, sees the object there is a list, so it prints [. It then looks at the first element of the list, and sees it's 0x1000. It sees the object there is a list, so it prints [ and looks at the first element -- 0x1000. It sees the object at that address is a list, so it prints [ and looks at the first element -- 0x1000. Etc.

Fortunately, the runtime is smart enough that it figures out that it's traversing an object it's already looked at, so it better cut it out and print ... instead.

User avatar
e^iπ+1=0
Much, much better than Gooder
Posts: 2065
Joined: Sun Feb 15, 2009 9:41 am UTC
Location: Lancaster

Re: Coding: Fleeting Thoughts

Postby e^iπ+1=0 » Sat Apr 09, 2011 7:13 pm UTC

Oh, duh, got it. It's actually adding the list to itself, not just the value.
poxic wrote:You, sir, have heroic hair.
poxic wrote:I note that the hair is not slowing down. It appears to have progressed from heroic to rocking.

(Avatar by Sungura)


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 14 guests