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 » Mon Jun 13, 2011 11:13 pm UTC

It sounds like you're splitting up the same task into multiple classes. Do you think you can put C's methods part of A? If that ends up putting to much responsibility in A, then just pass B to C when it needs it.
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
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 » Sat Jun 18, 2011 8:47 pm UTC

FT: I can't believe it took me all day to figure this bug out.

Code: Select all

void foo (int bar) {
  printf ("bar=%d\n", bar);
}

int main () {
    foo (10);
    
    foo
,(20);
    
    foo 
(30);
}
 


See, I had written "std::thread (foo, 10)" in another place, so the autocomplete feature in my text editor let me hit enter and it autocompleted "fo" into "foo," (with a comma).
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.

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

Re: Coding: Fleeting Thoughts

Postby EvanED » Sat Jun 18, 2011 8:53 pm UTC

Reminds me of this commit. Oops. (Though bigger consequences.)

Image

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 » Sat Jun 18, 2011 9:50 pm UTC

TheChewanater wrote:See, I had written "std::thread (foo, 10)" in another place, so the autocomplete feature in my text editor let me hit enter and it autocompleted "fo" into "foo," (with a comma).
You put spaces before the open parenthesis?

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Sat Jun 18, 2011 10:10 pm UTC

Once, as root (on a non-critical system), I ran

Code: Select all

rm -rf foo /*


That's the sort of typo you only make once.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

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 » Sat Jun 18, 2011 10:26 pm UTC

Xeio wrote:
TheChewanater wrote:See, I had written "std::thread (foo, 10)" in another place, so the autocomplete feature in my text editor let me hit enter and it autocompleted "fo" into "foo," (with a comma).
You put spaces before the open parenthesis?

In C++, yes. I think I picked that habit up from Lisp, since a lot of my earlier stuff isn't like that.

Also,
The GNU Coding Standards wrote:We find it easier to read a program when it has spaces before the open-parentheses and after the commas.
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.

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

Re: Coding: Fleeting Thoughts

Postby EvanED » Sat Jun 18, 2011 11:47 pm UTC

I don't want to comment on this particular issue (I think I wouldn't like it when a function is used in a compound expression but I'd be fine with it on its own), but I will say that I would not trust the GNU standards to be reasonable at anything. :-)

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Coding: Fleeting Thoughts

Postby Emu* » Mon Jun 20, 2011 2:46 pm UTC

EvanED wrote:Reminds me of this commit. Oops. (Though bigger consequences.)


I lol'd.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Mon Jun 27, 2011 8:45 am UTC

Ugh,.. debugging multi-threaded applications is a pain. Spent hours trying to figure out why a simple function call that should absolutely be working caused a segfault. Turns I forgot to wait in the destructor and as the thread variable fell out of scope in the other thread, the calling object was destroyed.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

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

Re: Coding: Fleeting Thoughts

Postby Steax » Tue Jun 28, 2011 10:00 am UTC

Discovered this in my own code:

Code: Select all

// DEPRECIATED: Slow as a carrot. Use get_row instead
function get_result($row$field)
{
    return 
mysql_result($this->result$row$field);    
}
 


"Slow as a carrot" has turned into a meta-meme with my dev team now.
In Minecraft, I use the username Rirez.

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

Re: Coding: Fleeting Thoughts

Postby MHD » Tue Jun 28, 2011 12:52 pm UTC

I wonder if it would be possible to make a graphics editing program that used OpenGL as a back end.
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"

Laguana
Posts: 49
Joined: Sat Jan 19, 2008 10:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Laguana » Tue Jun 28, 2011 1:56 pm UTC

Sure, depending on what you want it to do, fairly easily I think. Draw everything to a texture (which is both displayed to the screen and eventually saved out as the resulting image; probably through another library since openGL doesn't deal with filesystem interactions), and do whatever operations on that texture. Drawing lines and other primatives is fairly easy, although if you want to do vector editing / be able to move lines around (eg, flash's editing abilities rather than paint's) then you need to manage that data yourself.

Fancier operations such as blurring and such can be done via shaders on the graphics card, or you could get the pixel data and modify it yourself.

OpenGL is pretty flexible, if you put in the effort to do things. Disclaimer though: I make no promises about speed. It's surprisingly easy to make slow programs, in my limited experience. I'm sure that it's possible to do a photoshop-like via openGL and have it run well, but I wouldn't want to be the one writing it myself.

User avatar
RoadieRich
The Black Hand
Posts: 1037
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Behind you

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Tue Jun 28, 2011 4:15 pm UTC

Code: Select all

<me> what's the exact call you're making to glRotate()?
<x> glRotatef(degreeAngle, 0.0, lx, 0.0) [*]
<x> lx is calculated by sin(angle)
<me> why?
<x> mostly because it is used to calculate the camera's vector

All aboard for the Cargo Cult. How he managed to pass his Software Design and Implementation module I'll never know.

* glRotatef:

Code: Select all

void glRotatef(glFloat angle, glFloat x, glFloat y, glFloat z) //rotates the active matrix by angle degrees around vector {x,y,z}
73, de KE8BSL loc EN26.

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 » Wed Jun 29, 2011 12:42 am UTC

Hey, it's not a complete no-op... it ensures that a rotation by 10 degrees and a rotation by -10 degrees both rotate in the same direction. That's useful, right?

Code: Select all

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

User avatar
chridd
Has a vermicelli title
Posts: 846
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Wed Jun 29, 2011 12:57 am UTC

phlip wrote:Hey, it's not a complete no-op... it ensures that a rotation by 10 degrees and a rotation by -10 degrees both rotate in the same direction. That's useful, right?

Code: Select all

glRotatef(abs(degreeAngle), 0.0, 1.0, 0.0);

Does x know this:
RoadieRich wrote:* glRotatef:

Code: Select all

void glRotatef(glFloat angle, glFloat x, glFloat y, glFloat z) //rotates the active matrix by angle degrees around vector {x,y,z}
?
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · she · Forum game scores
mittfh wrote:I wish this post was very quotable...

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 » Wed Jun 29, 2011 1:51 am UTC

chridd wrote:
phlip wrote:Hey, it's not a complete no-op... it ensures that a rotation by 10 degrees and a rotation by -10 degrees both rotate in the same direction. That's useful, right?

Code: Select all

glRotatef(abs(degreeAngle), 0.0, 1.0, 0.0);

But that still lets you set degreeAngle = 350... the sin() thing makes sure there's no way to rotate in the opposite direction in a single call...

Code: Select all

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

User avatar
Ptolom
Posts: 1559
Joined: Mon Mar 24, 2008 1:55 pm UTC
Location: The entropy pool
Contact:

Re: Coding: Fleeting Thoughts

Postby Ptolom » Thu Jun 30, 2011 12:34 pm UTC

I've written up a set of graph classes and managed to cram everything needed in the nodes into general methods. Now I just have to put all the data into the nodes. Booooooooring. I've got 15 of these kind of things to hammer out. Links={{3,0.4,true,{5,5},{{0,4,8,9,13},{1,3,11,12,13}}},{3,0.1,false,{5,5},{{0,4,8,9,13},{1,3,11,12,13}}},{4,0.2,false,{2,0},{{8,10},{}}

User avatar
Dason
Posts: 1311
Joined: Wed Dec 02, 2009 7:06 am UTC
Location: ~/

Re: Coding: Fleeting Thoughts

Postby Dason » Thu Jun 30, 2011 12:42 pm UTC

Ptolom wrote:I've written up a set of graph classes and managed to cram everything needed in the nodes into general methods. Now I just have to put all the data into the nodes. Booooooooring. I've got 15 of these kind of things to hammer out. Links={{3,0.4,true,{5,5},{{0,4,8,9,13},{1,3,11,12,13}}},{3,0.1,false,{5,5},{{0,4,8,9,13},{1,3,11,12,13}}},{4,0.2,false,{2,0},{{8,10},{}}

Image
double epsilon = -.0000001;

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Thu Jun 30, 2011 8:04 pm UTC

Ok, I think it's safe to say Oracle is actively trying to kill Java. Not only does it pester me about installing patches every 5 minutes, the upgrade is bundled with a fucking browser toolbar. I'm seriously questioning why on earth I even have Java installed in the first place.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

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 Jun 30, 2011 8:11 pm UTC

You, sir, name? wrote:Ok, I think it's safe to say Oracle is actively trying to kill Java.

Yay for Oracle! I think... :?
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
Vault
Posts: 169
Joined: Mon Nov 10, 2008 5:00 pm UTC
Location: Just past the event horizon
Contact:

Re: Coding: Fleeting Thoughts

Postby Vault » Thu Jun 30, 2011 8:26 pm UTC

So a friend of mine is interning for a company working on a new web-dev language, Opa (http://opalang.org/). I've been messing with it for the past week or so and it seems pretty nifty. It's a functional language that combines a webserver and database into the language itself. As far as I can tell it seems a lot like ML, and also borrows a few things from Haskell (read: partial application).

At any rate, I was wondering what the folks around here would make of it.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Coding: Fleeting Thoughts

Postby You, sir, name? » Thu Jun 30, 2011 9:24 pm UTC

Vault wrote:So a friend of mine is interning for a company working on a new web-dev language, Opa (http://opalang.org/). I've been messing with it for the past week or so and it seems pretty nifty. It's a functional language that combines a webserver and database into the language itself. As far as I can tell it seems a lot like ML, and also borrows a few things from Haskell (read: partial application).

At any rate, I was wondering what the folks around here would make of it.


At a glance, it seems similar to CouchDB. Maybe with a jigger of ruby on rails. Except functional.

I guess if you're into buzzwords, this may be your thing.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
RoadieRich
The Black Hand
Posts: 1037
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Behind you

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Fri Jul 01, 2011 5:02 pm UTC

You, sir, name? wrote:I'm seriously questioning why on earth I even have Java installed in the first place.

Minecraftck?
73, de KE8BSL loc EN26.

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 » Fri Jul 01, 2011 6:39 pm UTC

I have it installed for the Android SDK and... that's it...

I have the plugin to my browser manually disabled and auto-update check off. I'd say I can't remember the last time I'd seen a website require java, but then I remembered that wells fargo recently updated their ESPP page, and it uses java... for a stock ticker... at the top of the page. Needless to say I just leave that silly little "needs plugin" icon alone.

User avatar
RoadieRich
The Black Hand
Posts: 1037
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Behind you

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Sat Jul 02, 2011 5:49 pm UTC

But Minecraft.
73, de KE8BSL loc EN26.

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

Re: Coding: Fleeting Thoughts

Postby Ubik » Sun Jul 03, 2011 6:33 pm UTC

Summer vacation started last week, so I finally dug up my C# + OpenGL (with OpenTK) on Mono project. I'm making some helper classes to represent OpenGL things like vertex buffers, textures or shaders. With the shaders especially I happened to bump into an annoying issue: OpenGL assumes that the object I'm operating on is "bound", "activated" or "in use" for the function calls to affect specially that object. This is especially evident with shader programs.

Now I'm thinking if I should always just automatically bind the program when I'm doing other things to the shader (potentially slow to do unnecessary calls all the time), or always just check what the bound program is and throw if correct shader is not bound (might also be slow but wouldn't alter state) or make the check only in debug mode (faster in release mode but not so safe).

Hmm, or maybe I should keep book by myself what the currently bound shader is...doesn't sound too nice.

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 » Wed Jul 06, 2011 9:07 pm UTC

FT: I wish Python would let you catch SyntaxErrors. I was hoping to abuse it to extend the syntax of the language.

Code: Select all

= 1

while n 
< 10:
    try:
        add 1 to n
    except SyntaxError as error
:
        line = str (error).split ('\n')[1]
        special_eval (line)
    
    print 
(n) 

Of course, this is impossible, but it would be cool.
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
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 » Wed Jul 06, 2011 9:32 pm UTC

I'll never understand why languages seek to rename common concepts like "try" and "catch". Here's looking at you, ruby; you bastard.

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 » Wed Jul 06, 2011 9:37 pm UTC

Similarly, I was just getting slightly frustrated that Python seemed to lack a "throw" keyword, until I remembered it's "raise" for some reason.

C is even worse.
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
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 » Wed Jul 06, 2011 9:48 pm UTC

Well, I can at least understand that C may have come before the convention (I say that under the hopful assumption that it's true, as I have no idea, did Fortran and such have sane exception handling?).

It just annoys me when a language uses the exact same syntax for a command, but decides the existing keywords are too... bland or something... I dunno.

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 » Wed Jul 06, 2011 11:07 pm UTC

I know Ada has exception handling similar to Python's, and it was made in the 70s. C came first, but it didn't get its current standard library until the 80s.
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.

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

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Jul 07, 2011 8:53 am UTC

People who read these forums may have noticed that I can be hard on C... but I will "defend" it here. IMO, exceptions (in their traditional sense*) are way outside of C's philosophy. Even a terrible implementation of exceptions is more complex than anything else a C compiler must do, and a reasonable (e.g. table-based) implementation would probably be an order of magnitude more complicated than anything else a C compiler does outside of optimization.

* By exception handling, I mean the ability to throw a value and have it propagate up to a catch block of the corresponding type. In other words, the runtime has to do do type discrimination. If you remove that requirement and just have it go to the most recent catch block (think like SEH), you just have syntactic sugar around a less powerful setjmp/longjmp.

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 Jul 07, 2011 8:29 pm UTC

FT: I haven't used Python much in a while (besides the interactive interpreter), so I'm trying to learn how to do things the "Pythonic" way. From what I understand, this is how this sort of string concatenation is done. Is this correct? It seems somewhat ugly to me. (foos and bars are dicts, by the way)

Code: Select all

def foo_bar_info ():
    foo_information = ''.join (
        ['Foos: '] +
        ['({0}, {1}) '.format (foo, foos[foo]) for foo in foos] +
        ['\n'])

    bar_information = ''.join (
        ['Bars: '] +
        ['({0}, {1}) '.format (bar, bars[bar]) for bar in bars] +
        ['\n'])

    return ''.join (
        ['Foos and bars:\n',
        foos_information,
        bars_information]) 


This should return something like:

Code: Select all

Foos and bars:
Foos: ('abc', 45) ('lol', 10) ('wtf', 20)
Bars: ('baz', 87) ('meep', 2) ('zed', 7) 
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
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Thu Jul 07, 2011 8:42 pm UTC

Eeeeh, that's not terrible, but I think you're going overboard on the joins. Python does still let you do good, old-fashioned concatenation, you know. Here's how I might do it:

Code: Select all

def foo_bar_info2():
    foo_information = 'Foos:' + ' '.join('(%s, %s)' % item for item in foos.items()) + '\n'
    bar_information = 'Bars:' + ' '.join('(%s, %s)' % item for item in bars.items()) + '\n'
    return 'Foos and bars:\n' + foo_information + bar_information

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 » Thu Jul 07, 2011 8:44 pm UTC

Using ''.join and comprehensions is the pythonic way to handle strings generatable in a loop, but for the rest, just use plain concat or string formatting.
(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 Jul 07, 2011 9:50 pm UTC

Xanthir wrote:Using ''.join and comprehensions is the pythonic way to handle strings generatable in a loop, but for the rest, just use plain concat or string formatting.

Yeah, that makes much more sense that what I was doing.
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
RoadieRich
The Black Hand
Posts: 1037
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Behind you

Re: Coding: Fleeting Thoughts

Postby RoadieRich » Fri Jul 08, 2011 1:07 pm UTC

Part of me wants to say "avoid addition of string literals", as immutable strings requires you to initialise the two substrings, initialise the result string then throw the initial strings away. In this case, you save seven str objects (I think).

Code: Select all

def foo_bar_info2():
    
foo_information ' '.join('({0!s}, {1})'.format(item) for item in foos.items())
    
bar_information ' '.join('({0!s}, {1})'.format(item) for item in bars.items())
    return 
"""\
Foos and bars:
Foos: {0}
Bars: {1}
"""
.format(foo_informationbar_information

I know about premature optimisation, but (in my opinion) you don't really lose anything in terms of readability with the second form, which is one of the major objections to it. The best route will probably be half way between cosomologicon's code and my code.

Also, the % operator on the str type is deprecated in python 3, replaced with str.format(), which is quite a bit more powerful, once you get used to the syntax.
73, de KE8BSL loc EN26.

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

Re: Coding: Fleeting Thoughts

Postby EvanED » Fri Jul 08, 2011 2:09 pm UTC

RoadieRich wrote:Part of me wants to say "avoid addition of string literals", as immutable strings requires you to initialise the two substrings, initialise the result string then throw the initial strings away. In this case, you save seven str objects (I think).

Code: Select all

def foo_bar_info2():
    foo_information = ' '.join('({0!s}, {1})'.format(item) for item in foos.items())
    bar_information = ' '.join('({0!s}, {1})'.format(item) for item in bars.items())
    return """\
Foos and bars:
Foos: {0}
Bars: {1}
"""
.format(foo_information, bar_information) 

I know about premature optimisation, but (in my opinion) you don't really lose anything in terms of readability with the second form, which is one of the major objections to it. The best route will probably be half way between cosomologicon's code and my code.

Also, the % operator on the str type is deprecated in python 3, replaced with str.format(), which is quite a bit more powerful, once you get used to the syntax.

str.format is available in 2.6/2.7 as well, just in case that's not clear.

Personally I dislike multiline string literals because of the way they miss up indenting (except in cases where you can indent the subsequent lines and it's okay); I'd actually be happier with either of these:

Code: Select all

return "Foos and bars:\nFoos: {0}\nBars: {1}\n".format(...)

or

Code: Select all

return ("Foos and bars:" +
        "\nFoos: " + (''.join(...)) +
        "\nBars: " + (''.join(...)))


(Edited a couple times for great justice and avoidance of double posting and such)
Last edited by EvanED on Fri Jul 08, 2011 2:14 pm UTC, edited 2 times in total.

User avatar
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Fri Jul 08, 2011 2:12 pm UTC

Yeah yeah I know, but they can have my overloaded modulo operator when they pry it from my cold, dead hands.

...which they probably will at some point.

User avatar
Red Rule
Posts: 23
Joined: Thu Apr 29, 2010 11:24 am UTC

Re: Coding: Fleeting Thoughts

Postby Red Rule » Sat Jul 09, 2011 4:28 pm UTC

I wonder if one could compress source-code (and slabs of text) using linguisticall-analysis.. (obviously different rules for different languages)
If it's possible one could shrink e.g. the linux-kernel or code repositories, or web-sites with a high text density, such as forums, a blog, twitter: the decoding could be done with all those fancy new light-speed JS-engines. perhaps gzip would also work...
"I gotta have a little bit of Orange juice
I gotta have.. my.. Orange juice!
I gotta have.. my.. Orange juice!
Juice juice juice juice juice juice juice juice
"

~Richard Feynman


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 7 guests