Coding: Fleeting Thoughts

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

Moderators: phlip, Moderators General, Prelates

User avatar
mabufo
Posts: 105
Joined: Sun Sep 09, 2007 11:17 pm UTC

Re: Coding: Fleeting Thoughts

Postby mabufo » Sat Sep 11, 2010 1:01 am UTC

I had a professor in my class today say that Classes should be thought of as object factories, and that methods should be thought of as messages that are sent to objects.

Could someone give me an abstract definition of OOP, and it's principles that is along those lines?

User avatar
joshz
Posts: 1466
Joined: Tue Nov 11, 2008 2:51 am UTC
Location: Pittsburgh, PA

Re: Coding: Fleeting Thoughts

Postby joshz » Sat Sep 11, 2010 1:08 am UTC

The way I like to think about it (and I think the way that is more common) is this:
Classes are general-for example, Cars. An Object would be *my* car. It has 'methods' in common with all cars--accelerate, brake, turn, etc. and 'variables'-color, size, engine type, etc.

Does that help?
You, sir, name? wrote:If you have over 26 levels of nesting, you've got bigger problems ... than variable naming.
suffer-cait wrote:it might also be interesting to note here that i don't like 5 fingers. they feel too bulky.

Sagekilla
Posts: 382
Joined: Fri Aug 21, 2009 1:02 am UTC
Location: Long Island, NY

Re: Coding: Fleeting Thoughts

Postby Sagekilla » Sat Sep 11, 2010 4:56 am UTC

I hope your professor isn't the sort who makes OOP out as being "Objects with data and methods to act on the data," since the really important thing about OOP is dynamic dispatch.

Cause with that, you can instantiate a whole set of different kinds of cars, stick them in an array of "Cars," and call a method common to the Car super class that can be implemented in different ways for each car.

That's the really cool part, IMO. I suffered from this same problem as thinking of OOP as meaning "I have an object that stores data and I can act on that data with methods," til I got to see real OO programming which can be powerful (Within it's realm, of course).
http://en.wikipedia.org/wiki/DSV_Alvin#Sinking wrote:Researchers found a cheese sandwich which exhibited no visible signs of decomposition, and was in fact eaten.

Pepve
Posts: 57
Joined: Wed Jul 28, 2010 9:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Pepve » Sat Sep 11, 2010 9:40 am UTC

Sagekilla wrote:[...] the really important thing about OOP is dynamic dispatch.

No it's not. Inheritance is one of the most misused features of OO programming languages. It has it's time and place, and it can be really helpful in some situations. But very often it breaks encapsulation, which is far more critical to good code. (Interfaces don't have this problem, so if your Car was an interface, it would be fine.)

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 » Sat Sep 11, 2010 12:00 pm UTC

Using interfaces still gives you dynamic dispatch. I use that all the time in Java.
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
mabufo
Posts: 105
Joined: Sun Sep 09, 2007 11:17 pm UTC

Re: Coding: Fleeting Thoughts

Postby mabufo » Wed Sep 15, 2010 5:32 am UTC

Berengal wrote:Using interfaces still gives you dynamic dispatch. I use that all the time in Java.

The interface becomes a type itself?I've also been told/read that inheritance is a bad thing, and often misused. Could someone explain the right way it should be done, and when to do it? When is it preferable to use an interface over an abstract class?

I'd like to learn to do OOP in very elegant, precise, and correct way. I just started a class at school that is hopefully supposed to teach this with respect to Java. I think however, that earlier in my education we were shown OO with the 'bad' version of inheritance as being how stuff worked. could someone point me to some reading material on the right way to do things? Or maybe provide some insights here? I feel like a lot of the reading material available online is very poorly written. Thanks guys.

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 Sep 15, 2010 6:46 am UTC

mabufo wrote:I'd like to learn to do OOP in very elegant, precise, and correct way.
Yeah, good luck with that. Write tests for everything and refactor mercilessly. Designs never survive enemy contact.

And yes, interfaces become their own type. Check out File's listFiles methods. If the file represents a directory, listFiles returns an array of all files and directories in that directory. However, there are three versions of that method, two of them taking an argument, FileFilter and FilenameFilter. These are both standalone interfaces which nothing in the standard library (as far as I know) implements. They're meant to be used something like

Code: Select all

File dir = new File(dirpath);
File[] files = dir.listFiles(new FileFilter(){public boolean accepts(File file){
  return file.getName().endsWith(".xml") || file.isDirectory();
}});


When I need polymorphism I start out using interfaces. If there's common behavior between the different classes, I see if I can move that to a separate class. If that's too cumbersome, I'll turn the interface into an abstract class. This is just a rule of thumb though. Another rule is to make all classes and methods either abstract or final; never subclass a non-abstract subclass, and never override a non-abstract method.

Don't rely on spotting inheritance by the difference between "has-a" and "is-a". It's the worst guide to good OO design ever invented. Don't worry about how things ought to be. Instead think about their practical effects and limitations. And don't be afraid of making lots of classes, even in Java (remember, you can have more than one class in each file as long as only one is public. You can also have inner classes, and even method-local classes.)

Let's look at a classic OO example. We have the Animal class, and the Dog class subclassed from that. We want to add a RabidDog class, so we do that by making it a subclass of Dog. Now we also want to add Rabit, so we do that by subclassing Animal. A problem now appears when we try to add RabidRabbit; it's sharing functionality with RabidDog, but we have no good way of modeling this. We can let RabitRabbit inherit from Rabit, but then we'd have to copy-paste the rabit code from RabidDog. We could subclass Animal to RabitAnimal and let our two rabid subspecies inherit from that, but then they won't inherit from Dog and Rabit (which are not rabid animals usually). The solution is to use object composition instead, giving the rabid animals a Disease object which implements the common behavior. This can also be done with their other behaviors like eating and walking, and now you've suddenly got rabits eating dog-food and jumping dogs for free.

Test-driven development helps alot with these kinds of designs, because you're now using your objects in at least two different ways so you quickly spot the unfortunate dependencies. The downside to this is that it can lead to overcomplicated designs.
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.

Pepve
Posts: 57
Joined: Wed Jul 28, 2010 9:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Pepve » Wed Sep 15, 2010 11:53 am UTC


User avatar
mabufo
Posts: 105
Joined: Sun Sep 09, 2007 11:17 pm UTC

Re: Coding: Fleeting Thoughts

Postby mabufo » Fri Sep 17, 2010 2:28 am UTC

Great responses guys; thank you very much.

fr00t
Posts: 113
Joined: Wed Jul 15, 2009 11:06 am UTC

Re: Coding: Fleeting Thoughts

Postby fr00t » Fri Sep 17, 2010 5:24 pm UTC

I just discovered processing.org. You can create such beautiful and amusing things so easily.

At work I maintain legacy vb6 code written by people without computer science degrees.

It's amazing how such superficially similar situations can cause such different amounts of pleasure.

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 » Mon Sep 20, 2010 1:01 am UTC

Coding is damned fun. I just spent a day writing a small asteroids-y game, including making the graphics, and have had a really good time, both coding and playing. This is why I program; making something worthwhile from scratch, assuming full responsibility for making it work correctly and full credit when it does. If something's not to my liking, I can change it easily. Thrusters not showing up? Five minutes in a paint-equivalent and five minutes of coding and they're there. Fancy rock explosions? Let me just draw them up and code them in. Tomorrow I'll do more advanced physics, see where that gets me.

spacegame.png
The result of five hours of coding. (Looks best fullscreen)
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
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Wed Sep 22, 2010 1:22 am UTC

FT: When will user input-correction devices (like spellcheckers) start to learn contextually? The iPhone keypad already took it one step further by learning words the user cancels a lot and taking the keyboard layout into consideration when weighing alternatives. (It does have a deadly flaw, though: it won't work if you leave out any characters, i.e. because you tapped too softly.)

But the possibilities are huge! If I'm texting about a certain topic with a friend, the spellchecker could automatically add in words they used into the session. It's common for people to use the same words in a reply, especially stuff like names. Or, when adding new words to learn, take into account other words I used in the same input box/recently to figure out how words are connected.

Similarly, I wish there was some sort of middle-trimming. I.e. "someemail @ gmail.com" -> "someemail@gmail.com"
(I can't think of any other way to do that manually other than stepping through the individual characters and stripping out the spaces, although if any regex wizards are out there...)
In Minecraft, I use the username Rirez.

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: Coding: Fleeting Thoughts

Postby fazzone » Wed Sep 22, 2010 6:38 am UTC

Steax wrote:It's common for people to use the same words in a reply, especially stuff like names. Or, when adding new words to learn, take into account other words I used in the same input box/recently to figure out how words are connected.


The default soft-keyboard IME on my Motorola Droid does names from my contacts, months (when not ambiguous (may/May?)), and days.

Yeah it would be sort of cool for when I type something like "ill" for the IME to know whether I meant I'll (it does this for dont, wont, cant, etc) or ill as in sick, but I doubt it will happen anytime soon, and if it ever does happen it won't quite fit the same model. Using the ill/I'll example, you have to know when in the sentence you can expect "I will", a subject+verb combo, and where you can expect "ill", a noun that may be a subject, direct object, indirect object, or whatever else. Even if you were smart enough to write a system like that, you would have several problems:

    I'm not a linguist by any means, but my feeling is that you'd have to rewrite this essentially from scratch if you wanted to do another language
    You have to store a dictionary that not only stores the words themselves, but any part-of-speech, usage, and meaning information
    You'd be expending what I presume would be a fairly large number of CPU cycles on a system that has to respond interactively as you type
    You are doing a lot of work fairly frequently on a system where power is at a premium
and so on. In my view, there's no real point, even if you could get it right 100% of the time (which is likely impossible) to save a couple of characters typed/a few seconds' annoyance by implementing such a system.
*/

Posi
Posts: 111
Joined: Mon Jul 16, 2007 6:08 am UTC

Re: Coding: Fleeting Thoughts

Postby Posi » Wed Sep 22, 2010 7:18 am UTC

Steax wrote:FT: When will user input-correction devices (like spellcheckers) start to learn contextually? The iPhone keypad already took it one step further by learning words the user cancels a lot and taking the keyboard layout into consideration when weighing alternatives. (It does have a deadly flaw, though: it won't work if you leave out any characters, i.e. because you tapped too softly.)

But the possibilities are huge! If I'm texting about a certain topic with a friend, the spellchecker could automatically add in words they used into the session. It's common for people to use the same words in a reply, especially stuff like names. Or, when adding new words to learn, take into account other words I used in the same input box/recently to figure out how words are connected.

Similarly, I wish there was some sort of middle-trimming. I.e. "someemail @ gmail.com" -> "someemail@gmail.com"
(I can't think of any other way to do that manually other than stepping through the individual characters and stripping out the spaces, although if any regex wizards are out there...)

Maybe you should ask Google. Their search already does this extremely well.

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 Sep 22, 2010 9:40 am UTC

But google's suggestion stuff is based on it's crawler data right? So either we stuff phones/computers full of google's cache or send a request to google on every keystroke...

fazzone wrote:and so on. In my view, there's no real point, even if you could get it right 100% of the time (which is likely impossible) to save a couple of characters typed/a few seconds' annoyance by implementing such a system.


You're quite right. But even a few small improvements could be helpful. Doesn't have to be perfect, but any improvement is good.
In Minecraft, I use the username Rirez.

squareroot
Posts: 548
Joined: Tue Jan 12, 2010 1:04 am UTC
Contact:

Re: Coding: Fleeting Thoughts

Postby squareroot » Thu Sep 23, 2010 4:48 am UTC

I've finally started learning about graphics in Java; previously the only graphical thing I'd ever done was a cellular automata plotter.
Yesterday I made Snake, today I made Galaga.
Instant popularity as a freshman!! :D
<signature content="" style="tag:html;" overused meta />
Good fucking job Will Yu, you found me - __ -

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 Sep 23, 2010 8:33 am UTC

More fleeting thoughts!

Browsers should recognize non-anchor-tagged links that look like links and add "Open this address" in the context menu when selecting it. Right now Firefox has "Search [Selected Search Engine] for [highlighted text]", but that's still a click away.

Forum software should add AJAX that refreshes the topic lists once every, say, 5 minutes. Better than constantly refreshing, hoping for new stuff to talk about, not being sure if the refresh is in progress or if it's done, and finding nothing.

Downloading images/files should have an option to remember the last place we saved files from this domain to, instead of a global where-you-last-saved. Because the way it is now, when I'm in a flurry of collecting ideas and inspiration for a design, and stumble across something else (say, saving a video or app), I have to navigate to a Downloads folder, and then navigate back. It's a hassle. I wonder if I there's a good solution for that, though. Maybe a "select default download folder for this session" button or something?
In Minecraft, I use the username Rirez.

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

Re: Coding: Fleeting Thoughts

Postby phlip » Thu Sep 23, 2010 9:14 am UTC

Steax wrote:Browsers should recognize non-anchor-tagged links that look like links and add "Open this address" in the context menu when selecting it. Right now Firefox has "Search [Selected Search Engine] for [highlighted text]", but that's still a click away.

With vimperator, you can select text and press YP. On linux (and probably other X systems too) you can skip the Y, and just hit P. I use it a bunch.

Code: Select all

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

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 Sep 23, 2010 2:26 pm UTC

Steax wrote:Browsers should recognize non-anchor-tagged links that look like links and add "Open this address" in the context menu when selecting it. Right now Firefox has "Search [Selected Search Engine] for [highlighted text]", but that's still a click away.

Are you talking about plain-text urls? I used to use a Firefox add-on that automatically converted these into hyperlinks. This one claims to do that. I prefer this method to yours (it saves even one more click), but if you want to have to right-click as well, this add-on claims to do that.

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 » Thu Sep 23, 2010 2:32 pm UTC

Chrome has that built in. :P

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 Sep 23, 2010 3:16 pm UTC

What exactly does Chrome do? I'm using Chrome and it only lets me follow a plain-text url if I highlight it and then right-click. So many clicks....
test page

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 » Thu Sep 23, 2010 5:22 pm UTC

Developers are a really conservative bunch.

We're also bad at managing complexity; our attention to detail leaves our ability to filter out details broken.

Sometimes I wonder if the ultimate coder just might be the sysadmin throwing together quick perl scripts for ad-hoc jobs, or the web-monkey throwing together a barely-working PHP app, in a couple of hours.

Abstractions don't neccessarily have to leak. If they do they're just badly made (and the problem probably misunderstood).

"Everything should be as simple as possible, but no simpler.": the second sub-sentance is useless in practice; everything can be made simpler by throwing more intelligence at it (but the ROI decreases).

Sharpening your axe every now and then seems pretty obvious to a lumberjack, but most developers I know never voluntarily sharpen their skills.
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
Cosmologicon
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA
Contact:

Re: Coding: Fleeting Thoughts

Postby Cosmologicon » Thu Sep 23, 2010 5:37 pm UTC

Berengal wrote:most developers I know never voluntarily sharpen their skills.

Hey that reminds me, why don't we have another coding contest on this message board? :)

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 Sep 23, 2010 6:50 pm UTC

Cosmologicon wrote:
Berengal wrote:Sharpening your axe every now and then seems pretty obvious to a lumberjack, but most developers I know never voluntarily sharpen their skills.

Hey that reminds me, why don't we have another coding contest on this message board? :)


@Berengal Axes dull with use, while haxx0r ski1ls dull with a lack of use.

@Cosmologicon I'm liking that idea.
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
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Fri Sep 24, 2010 12:22 am UTC

I've been considering on and off starting up the competitions up again with a poker tournament... just need to actually sit down and write the network protocols (and interfaces for a couple of languages, so that people unfamiliar with networking can still enter)...

Code: Select all

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

elminster
Posts: 1560
Joined: Mon Feb 26, 2007 1:56 pm UTC
Location: London, UK, Dimensions 1 to 42.
Contact:

Re: Coding: Fleeting Thoughts

Postby elminster » Fri Sep 24, 2010 2:35 pm UTC

Code: Select all

#define RAND_MAX 0x7fff

Simplistic random functions FTL. Visual studio happens to use the lowest required value for RAND_MAX. Must remember to read specifications more often.
Image

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 » Fri Sep 24, 2010 7:35 pm UTC

TheChewanater wrote:
Berengal wrote:Sharpening your axe every now and then seems pretty obvious to a lumberjack, but most developers I know never voluntarily sharpen their skills.

@Berengal Axes dull with use, while haxx0r ski1ls dull with a lack of use.

Many of our skills cannot be practiced by repeating the same actions over and over. This is why there's a difference between ten years of experience, and one year of experience ten times.

Also, some people never even bother to find out what their IDE can do beyond basic text-editing. Maybe we would be better off of IDEs wore out the way axes do?

Skill related: It's scary how much high-school math I've forgotten. Not just the knowledge (which is easy to look up and understand), but the practice as well. I just spent a couple of hours solving a relatively simple problem (a given point moves on a given line with a given speed. Given another point with another speed, at what angle does it have to move to collide with the first point?)
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
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Fri Sep 24, 2010 11:29 pm UTC

Berengal wrote:Developers are a really conservative bunch.

We're also bad at managing complexity; our attention to detail leaves our ability to filter out details broken.

Sometimes I wonder if the ultimate coder just might be the sysadmin throwing together quick perl scripts for ad-hoc jobs, or the web-monkey throwing together a barely-working PHP app, in a couple of hours.

Abstractions don't neccessarily have to leak. If they do they're just badly made (and the problem probably misunderstood).

"Everything should be as simple as possible, but no simpler.": the second sub-sentance is useless in practice; everything can be made simpler by throwing more intelligence at it (but the ROI decreases).

Sharpening your axe every now and then seems pretty obvious to a lumberjack, but most developers I know never voluntarily sharpen their skills.


I think there's a turning point where developers start getting too close towards abstract matters like code style and unit testing and stuff. Not that it's bad, but then they start to focus on those matters because those are quite critical (in terms of peer image and security) and start to let go of 'learning new things' which we often feel is already 'behind us'. There used to be a sense of discovery when we learned a new ability, but not anymore. So we stop traveling in that direction.

I still voluntarily sharpen my e-axe though. That's what, say, A List Apart and Smashing Magazine is for.
In Minecraft, I use the username Rirez.

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 Sep 25, 2010 1:06 am UTC

I've noticed that since I started working, my learning capacity is more geared to learning the software project I'm working on than new coding techniques or libraries. Granted, it's huge, so, that could take years... :shock:

Though it could also be that I haven't really started any projects in my free time as of late, haven't really been in the mood (and can't think of anything to work on, really).

Axidos
Posts: 167
Joined: Tue Jan 20, 2009 12:02 pm UTC
Location: trapped in a profile factory please send help

Re: Coding: Fleeting Thoughts

Postby Axidos » Sat Sep 25, 2010 1:45 pm UTC

Xeio wrote:Though it could also be that I haven't really started any projects in my free time as of late, haven't really been in the mood (and can't think of anything to work on, really).

I know I say to myself what you just said to yourself, but I have plenty of ideas and I tell myself "Yeah, but that's too simple and easy" or "Yeah, but that's too complex" or something like that. If it's too simple, then do it and prove it because what else are you doing? If it's too complex, then great! Take steps.

Of course having said that I am putting it into practice myself now. :P

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

Re: Coding: Fleeting Thoughts

Postby Ubik » Sat Sep 25, 2010 5:41 pm UTC

A couple days ago we were talking at work about why Java has primitive types and classes representing them. I voiced my opinion that primitive types are clearly not enterprisey enough and should be removed completely, not even just wrapped into some classes like they now are with the built-in helper types. You could start with a class Boolean that has two derived types, True and False. Neither would have any member variables but it is the class itself of a Boolean variable that defines its value.

You could replace if-else-constructs with try and catch blocks. Integers could be represented as linked lists where the list's length denotes the value of the integer. An alternative would naturally be a list of Booleans where each of them represents one bit. In that case you could make the list only as long as it needs to be, so a value that can be represented in 7 bits only takes 7 booleans. There's also practically no other upper limit for the number of bits than memory, so this approach is clearly better than the arcane primitive type with fixed amount of bits. Well, actually these Booleans would clearly take no memory at all because they have no member variables, har har har.

squareroot
Posts: 548
Joined: Tue Jan 12, 2010 1:04 am UTC
Contact:

Re: Coding: Fleeting Thoughts

Postby squareroot » Sat Sep 25, 2010 8:26 pm UTC

Why do it like that? The sane way is like this:

Code: Select all


public class Boolean implements True, False{
   public void t() throws Exception{throw new Exception();}
   public void f() throws Exception{throw new Exception();}
   public java.lang.Boolean get(){
      try{
         t();
      }catch(Exception e) {
         try{
            f();
         } catch(Exception f) {
            return null;
         }
         return false;
      }
      return true;
   }
   
   public static void main(String[] args){
      System.out.println(True.t.get());
      System.out.println(False.f.get());
      System.out.println(new Boolean().get());
   }
}

public interface True {
   public void t() throws Exception;
   static final Boolean t=new Boolean(){
      public void t(){
         
      };
   };
}

public interface False {
   public void f() throws Exception;
   static final Boolean f=new Boolean(){
      public void f(){
         
      };
   };
}
<signature content="" style="tag:html;" overused meta />
Good fucking job Will Yu, you found me - __ -

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 » Sun Sep 26, 2010 4:02 pm UTC

Anyone know of a unix concatenate command that'll output a line containing the filename of each file in its output? So if file1.txt contained "foo" and file2.txt contained "bar", the output would be

file1.txt
foo
file2.txt
bar

User avatar
Briareos
Posts: 1940
Joined: Thu Jul 12, 2007 12:40 pm UTC
Location: Town of the Big House

Re: Coding: Fleeting Thoughts

Postby Briareos » Sun Sep 26, 2010 4:33 pm UTC

Why not

Code: Select all

#!/bin/bash
for file in $*
do
    echo $file
    cat $file
done
Sandry wrote:Bless you, Briareos.

Blriaraisghaasghoasufdpt.
Oregonaut wrote:Briareos is my new bestest friend.

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 » Sun Sep 26, 2010 4:45 pm UTC

Yeah I've got it submitted that way (it is part of an assignment), but I'm told there is a way to have it done automatically, so I'm trying to look it up >_>.

Edit: Ah actually I misread the question, it's only supposed to contain the filename if there's more than one file. That complicates things a little more, and makes a command that does this automatically all the more desirable. I'll post back when I find something else, since I don't want a solution for my assignment problem (I just want a better solution once I've already handed mine in :P)
Last edited by Rippy on Sun Sep 26, 2010 5:13 pm UTC, edited 3 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 » Sun Sep 26, 2010 4:46 pm UTC

That should work. Here's a one-liner if you (like me) don't like saving files you don't have to:

Code: Select all

echo file*.txt | xargs -n1 sh -c 'echo "$0" && cat "$0"'

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Coding: Fleeting Thoughts

Postby qbg » Mon Sep 27, 2010 2:53 am UTC

You can do arithmetic on a sequence of continued fraction terms, and make it so that the resulting sequence is lazy, giving what I call "lazy numbers".

Lazy numbers are cool. Lets write a mandelbrot set generator that uses them for infinitely precise output! Just think of everything doubles are throwing away!

Hmm... It takes about 15 msec to see if this point is in the set or not. Bad, but it could be worse...

Oh wait, it takes ~380 msec to see if this point is in the set...

Oh wow, by adding a few ones to the number, it now takes about 4 seconds....

WTH? It has been sitting for over 30 minutes and it hasn't return an answer yet!

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 Sep 27, 2010 7:46 am UTC

qbg wrote:You can do arithmetic on a sequence of continued fraction terms, and make it so that the resulting sequence is lazy, giving what I call "lazy numbers".

Lazy numbers are cool. Lets write a mandelbrot set generator that uses them for infinitely precise output! Just think of everything doubles are throwing away!

Hmm... It takes about 15 msec to see if this point is in the set or not. Bad, but it could be worse...

Oh wait, it takes ~380 msec to see if this point is in the set...

Oh wow, by adding a few ones to the number, it now takes about 4 seconds....

WTH? It has been sitting for over 30 minutes and it hasn't return an answer yet!


You didn't omit the limit on iteration depth by any chance?
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Coding: Fleeting Thoughts

Postby qbg » Mon Sep 27, 2010 3:46 pm UTC

You, sir, name? wrote:You didn't omit the limit on iteration depth by any chance?

Nope; I have the number of iterations capped at 100.

There might be a bug in my code that causes an infinite loop, or demands more terms than necessary. I wonder what switching to rational math would do...

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Coding: Fleeting Thoughts

Postby qbg » Mon Sep 27, 2010 6:04 pm UTC

So as it turns out, my function for deciding which continued fraction to take input from is broken, and I have no clue of how to fix it. I modified my code so that it alternates input (which is more inefficient than it needs to be), and the hack works well enough that I was able to generate a mandelbrot image with a low iteration count in a reasonable time.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests