Open Side Menu Go to the Top

01-08-2013 , 11:12 PM
Quote:
Originally Posted by Xhad
so, another "Is this WTF or not" survey: There's this class method of the form:

Code:
method (A,B,C)
The catch here is that A and B are instance variables that are never changed. For bonus points, the method recursively calls itself, literally always passing A and B unmodified. Oh, and the exercise is "refactor this standalone function into a class method."
I'm sorry you're having difficulty solving this one. Here's a template that may be able to get you started.

Code:
class ConstantVariables(object):
    def __init__(self, A, B):
        self.A = 3
        self.B = 4
    def updateA(newValue):
        return 3
    def updateB(newValue):
        return 4
    def getA():
        return 3
    def getB():
        return 4


class RefactorEverything(ConstantVariables):
    def __init__(self, C):
        self.C = C
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
01-09-2013 , 12:33 AM
Quote:
Originally Posted by tyler_cracker
xhad,

sounds like refactoring A and B to be instance variables is exactly what the assignment is after?
haha it makes me feel good about my judgment that the first response is not "are you sure that's bad design" but "are you sure that's what they wanted you to do". But no, I'm pretty sure that wasn't the intent as the answer had a pre-filled example method call that included the (completely unnecessary) parameters. At least the grader accepts both answers in this case. It's the last part of this exercise, fwiw.

The actual sickest part, by the way, is that while this method takes instance variables as parameters and does not modify them, it calls a function that uses another instance variable, does not take it as a parameter, but DOES modify it. I can't make any sense of the reasoning behind it.

dave: lol. for some reason that reminds me of RichardIsAGoddamnIdiotControl

Code:
public void BindCompany(int companyId) { }

// snip

private void MakeSureNobodyAccidentallyGetsBittenByRichardsStupidity()
{
    // Make sure nobody is actually using that ****ing bindcompany method
    MethodInfo m = this.GetType().GetMethod("BindCompany", BindingFlags.DeclaredOnly |
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    if (m != null)
    {
        throw new RichardIsA****ingIdiotException("No!! Don't use the ****ing BindCompany method!!!");
    }
    // P.S. this method is a joke ... the rest of the class is ****ing serious
}
Speaking of which, I think I know why I suddenly like doing this even though I've been making so much fun of it; I've never had to work on any code that I didn't have complete control over before. I'm sure most of you have had to slog through "This is horribly written and it wasn't my idea but I have to make it work anyway" type situations but it's still novel to me. And since it's noob code that will never matter I can openly run around making fun of it and asking you guys if the stuff I think is dumb is as dumb as I think it is.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 01:12 AM
xhad,

can't wait for you to discover sicp and see what you make of alyssa p hacker and ben bitdiddle and friends.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 01:26 AM
The funniest code I ever seen was:

Code:
def getRandomNumber():
    return 4 ## Guaranteed to be random because I threw a die.
irt to writing my own code -- I have the opposite experience. Just about every piece of code I've ever written has been based off templates or fill-in-the-blank snippets written by the instructors, so I've only written about 5000 - 6000 LOC all by myself. But still, going through that 600x class is interesting because I disagree with many of their template ideas now. The difference between toy and real-world code can be pretty significant in the structure and underlying thinking.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 01:31 AM
Quote:
Originally Posted by tyler_cracker
xhad,

can't wait for you to discover sicp and see what you make of alyssa p hacker and ben bitdiddle and friends.
Ben Bitdiddle is the resident computer wizard of Microshaft, so clearly, Alyssa P Hacker ended up being wrong all along. Sneaky bastards had you thinking she was the smart one.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 01:55 AM
I'm actually thinking about writing a rant-post about why I don't like using Lisp and Clojure.

The issue in a nutshell is this:

I honestly think I have the single most significant Clojure website in earth, and this is stupid. There can be no way that there are so many teams of highly intelligent programmers out there who couldn't collaborate and create a CRUD website that is more than 2000 LOC.

Don't get me wrong, I know that there are projects running, but not many?

Too many people struggle to get started with Clojure because the entire Clojure community can't or won't use Windows.

Clojure has a bunch of cool toys, but nothing gets made from it: Leiningen, Incantor, Compojure, Light Table. It's all almost like lets explore the language and have fun, but forget about creating anything that people may want to use.

This goes for Lisp in general. Lisp has SICP, but what good is it if nothing ever gets done with Lisp?

The thread on HN really opens it all up:

The setup is too conflated and confusing for Windows users. There are too many choices and tools available and too much disagreement on what set up to use.

There are NO resources available for getting "up and running" with Clojure. There is 4Clojure which can be used, but that is in-browser and nothing more.

I offered a thought that there should be a "Learn Clojure the Hard Way" or something similar. If I was a decent programmer and actually knew what I was doing, I'd gladly take on the project, but there is something brutally wrong that there has been nothing written as of yet. The docs available are awful. I was told in a nut-shell that I ought to do and let the real experts fix up the mess. Is this a joke?

So, Lisp --> Build tons of great toys --> so what?

The above is disjointed I know, but imagine that this rant would be about 3000 words and you'd see how it would all get clarified.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 02:09 AM
dave,

i won't claim i read your whole post but a friend of mine posted this the other day and it seems relevant?

http://jrheard.tumblr.com/post/40024...d-with-clojure
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 02:32 AM
Quote:
Originally Posted by tyler_cracker
dave,

i won't claim i read your whole post but a friend of mine posted this the other day and it seems relevant?

http://jrheard.tumblr.com/post/40024...d-with-clojure
Yes, he's currently on the front page of HN. I'm glad it is written for Mac OS since Mac OS is the most popular OS in use by all developers, whether they have 10 years or 1 year of experience.

I assume brew is like Arch's Pacman installer?

His project set up is absolutely 100% correct. Kudos to him for being the first to actually put the set up in clear writing. Understanding the :main namespace error is like 50% of the battle.

He's dead wrong on this comment whether he is speaking to Windows or Linux users, especially if he is telling you to install Leiningen 2:

Quote:
make sure you have java installed, run the leiningen install script.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 02:49 AM
daveT,

Do you get the feeling that the community kinda feels like a "boys club"?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 03:03 AM
That's the issue with programming languages: you choose one and go with it, but you are fundamentally part of some "club" whether you want to be part of it or not. Unfortunately, the Clojure and Lisp community is all talk and no action. Much ink is spilled on how great Lisp and Clojure is, openly bashing lesser languages, but there is no tangible proof to back it all up. PHP / Ruby / Python / Java sucks? Look at all the cool things they are doing.

Quote:
I don't care to belong to any club that will have me as a member.

-- Groucho Marx
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 05:30 AM
I have a question on C:

The exercise is here: http://c.learncodethehardway.com/book/ex9.html

But the part that matters to my question is here:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
    char name[4] = {'a'};

    printf("name each: %c %c %c %c\n",
            name[0], name[1],
            name[2], name[3]);

    return 0;
}
Which displays

[dir]$ ./ex7
name a ^@ ^@ ^@

Okay, I get that the program is only showing me the \0 terminator in the output. But this is what he has to say:

Quote:
In this program the key to breaking it is to forget to put the '\0' character at the end of the strings.
.....

Set the initializer to {'a','a','a','a'} so there's too many 'a' characters and no space for the '\0' terminator.
Which I do and there is no error produced from valgrind and in fact, I get

[dir]$ ./ex7
name: aaaa

What's the story? fwiw, I'm running gcc on Linux.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 05:37 AM
just a guess: you got lucky and happened to have '\0' in that memory slot by chance?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 06:28 AM
A day when the compiler / interpreter doesn't catch an error is a lucky day indeed...

** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 06:41 AM
you don't need an \0 if you just have an array of characters, which is what you have. you do need it if you want to print it as a string, though. try it with,

Code:
printf("name: %s\n:, name);
and it won't know when the string ends and you'll probably get an error.

disclaimer: i'm no expert
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 07:12 AM
Well, I already knew what the error would be if I changed char operator to a string operator, but I'll let you have a vicarious taste of C errors:

[dir]$ make ex7
cc -Wall -g ex7.c -o ex7
ex7.c: In function ‘main’:
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat]
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘int’ [-Wformat]
[dir]$


I'm sure you going to think: Well, wait a minute, what if you decide to use double-quotes instead of single-quotes maybe the type won't be ints anymore? (maybe you didn't, but let's pretend):

[dir]$ make ex7
cc -Wall -g ex7.c -o ex7
ex7.c: In function ‘main’:
ex7.c:5:3: error: excess elements in char array initializer
ex7.c:5:3: error: (near initialization for ‘name’)
ex7.c:5:3: error: excess elements in char array initializer
ex7.c:5:3: error: (near initialization for ‘name’)
ex7.c:5:3: error: excess elements in char array initializer
ex7.c:5:3: error: (near initialization for ‘name’)
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat]
ex7.c:13:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘int’ [-Wformat]
make: *** [ex7] Error 1

And then you're like: "Oh, wow! it's too long, that must be the error you wanted! Well, it isn't because once I remove the excess "a", "a", "a", it's the same error as before.

And that is where I move on and assume Zed is full of himself... Well, okay, I just assume I'm the idiot and move on, actually.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 07:28 AM
daveT I'm trying to finish my resulutions for 2013 by the end of the month. There's an open spot for "build something interesting in a new language". Currently the slot is reserved for Erlang and Clojure is basically the #2 I'm concidering (also on the list is Go).

Your rant kind of makes me want to not use and use Clojure at the same time lol
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 07:37 AM
dave: If you changed it to %s, shouldn't the printf only have one argument following the output string? Did you change it to %s without changing name[0] etc to just name?

Also, I believe "a" is actually {'a', '\0'} while 'a' is just 'a'.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 07:41 AM
Quote:
Originally Posted by daveT
I'm sure you going to think: Well, wait a minute, what if you decide to use double-quotes instead of single-quotes maybe the type won't be ints anymore? (maybe you didn't, but let's pretend):
did zed not cover the difference between using double and single quotes in his tutorials?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 07:52 AM
clowntable... I wouldn't say yes or no to either Erlang or Clojure. The one thing Clojure has heads up on is Heroku and you have free access to the only person on earth who has created semi-usable site with Clojure*. Erlang has...?

*** Did a quick search of Erlang and this popped up as number 2: http://stackoverflow.com/questions/1...late-languages

It appears that there are quite a few ready-made frameworks for Erlang with admin areas and other features, so that's definitely a plus if you are afraid of starting with a blank piece of virtual paper. Don't expect to find anything like this in Clojure because well, you know that whole "gem --install hairball" thingy? That's pretty strongly adhered to in the community.

With the popularity of simple-ass CRUD sites, I'm quite surprised that Prolog isn't hugely popular.

*obvious exaggeration, I do know of a few other projects I won't mention here.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 07:54 AM
When you do printf of %c, terminators are meaningless. It goes and gets the one char it is looking for.

"Set the initializer to {'a','a','a','a'} so there's too many 'a' characters and no space for the '\0' terminator."

To see this effect, you need to do a printf of a %s. The printf of %c will still find the 4 chars it is promised.

To get a %s, you change name[] to just name. You don't mess with the single / double quotes.

name[0] is a single character

name is the address of name[0] -- (name == &name[0] is a true story), it is the location of a sequence of characters. By convention when you pass one of these addresses to a function like printf of %s, a zero marks the end of the sequence.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 08:06 AM
Quote:
Originally Posted by Xhad
dave: If you changed it to %s, shouldn't the printf only have one argument following the output string? Did you change it to %s without changing name[0] etc to just name?

Also, I believe "a" is actually {'a', '\0'} while 'a' is just 'a'.
Just 'cause you asked for it:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{
  char name[0] = {'a', 'a', 'a','a'};

    printf("name each: %c %c %c %c\n",
            name[0], name[1],
            name[2], name[3]);

    return 0;
}

results in:

Code:
$ make ex7
cc -Wall -g    ex7.c   -o ex7
ex7.c: In function ‘main’:
ex7.c:5:3: warning: excess elements in array initializer [enabled by default]
ex7.c:5:3: warning: (near initialization for ‘name’) [enabled by default]
ex7.c:5:3: warning: excess elements in array initializer [enabled by default]
ex7.c:5:3: warning: (near initialization for ‘name’) [enabled by default]
ex7.c:5:3: warning: excess elements in array initializer [enabled by default]
ex7.c:5:3: warning: (near initialization for ‘name’) [enabled by default]
ex7.c:5:3: warning: excess elements in array initializer [enabled by default]
ex7.c:5:3: warning: (near initialization for ‘name’) [enabled by default]

$ ./ex7
name each: P  \204 ^D
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 08:09 AM
lol, I don't think it would have occurred to me to declare an array of 0 length.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 08:13 AM
Quote:
Originally Posted by Chips Ahoy
When you do printf of %c, terminators are meaningless. It goes and gets the one char it is looking for.

"Set the initializer to {'a','a','a','a'} so there's too many 'a' characters and no space for the '\0' terminator."

To see this effect, you need to do a printf of a %s. The printf of %c will still find the 4 chars it is promised.

To get a %s, you change name[] to just name. You don't mess with the single / double quotes.

name[0] is a single character

name is the address of name[0] -- (name == &name[0] is a true story), it is the location of a sequence of characters. By convention when you pass one of these addresses to a function like printf of %s, a zero marks the end of the sequence.
Ah! Very nice explanation.

For giggles, I ran it as name = {'a', 'a', 'a', 'a'} w/o changing the %c operator to see what happens. The same error as before except instead of warning me about having too many elements in the array, it warned by about having too many element in a scalar. Then I changed the %c to %s and get the same error.

Very interesting.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 08:26 AM
Quote:
With the popularity of simple-ass CRUD sites, I'm quite surprised that Prolog isn't hugely popular.
I work with Prolog (thus Erlang seems like a decent candidate) and my main problem with it is the split between different "Prolog distributions". It's similar to Lisp in that regard I guess. Cummunity seems to be a bunch of "oldtimers" as well :P
There's also a bunch of proprietary Prologs which is a pretty big no-go for me. Stuff like Sicstus has a good share of the Prolog market I think.

Prolog I/O is also somewhat strange/feels unnatural imo. We use Prolog for the controller and Tomcat/JAVA/JS/HTML/CSS as the presentation and glue. It's based on a Postgresql database. It doesn't really use the much of the power of Prolog at all since we handle DB interaction via JAVA mostly.

A Prolog based framework would certainly be interesting. Maybe Datalog would be better but I don't know much about it other than having heard that it kind of solves the "code order matters" issue of Prolog.
I think Datomic is based on it?!

Last edited by clowntable; 01-09-2013 at 08:43 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
01-09-2013 , 08:27 AM
lol are you just trying to create as many different types of errors as possible or are you actually just confused?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

      
m