• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

What's GAF's thoughts on functional programming? I used some of it's philosophies on some Python code yesterday and I liked the results, very nice way of thinking.
 

survivor

Banned
It's not free, and I actually haven't used it for Python, but the JetBrains IDE is actually incredibly well done, at least RubyMine is. It's super easy to just jump in and out of libraries, test drive code, etc. There's a 30 day trial that doesn't bother you or make you register your email or anything @ http://www.jetbrains.com/pycharm/

I'd still advocate knowing vim, though, it's incredibly useful when remoting into servers, and sometimes just when you feel like using vim.
PyCharm looks pretty good. I was considering using PyDev plugin for Eclipse, but this might be a better option. I will see if I can snag the student's license.
 

usea

Member
What's GAF's thoughts on functional programming? I used some of it's philosophies on some Python code yesterday and I liked the results, very nice way of thinking.
For things like lambdas, and functions like map and filter, I find them absurdly useful. My code would be a lot more verbose without them. It'd be tedious to read and write. However, I don't often use things like returning a function from a function, although I love passing them in as callbacks and stuff.
 

Zoe

Member
Yes, I think so :) It's a small do whatever you want assignment so I need to give some sort of context (ie make a stupid game) to a generic sorter that can do "any" kind of type/object.

Is it supposed to be generic or are you expected to use var?

(I'm not a fan of var, but it's practically a coding standard at the bf's company)
 

mltplkxr

Member
Hey guys, what's the best language to learn as a network/server administrator? People have said powershell and thinking of that but I've been kind of leaning towards c# since of how I can create MIS packages with it and also the object based benefits doing so. This is coming from a guy with little programming language done in the past and more cmd and some other syntax commands. Thanks guys.

Powershell is the right answer.
 

Minamu

Member
Is it supposed to be generic or are you expected to use var?

(I'm not a fan of var, but it's practically a coding standard at the bf's company)
Generic I think, We've been taught to substitute a specific data type with "Element" basically. I talked to my tutor and what I already have is enough but I'm kinda fumbling in the dark nonetheless.
 

usea

Member
Generic I think, We've been taught to substitute a specific data type with "Element" basically. I talked to my tutor and what I already have is enough but I'm kinda fumbling in the dark nonetheless.
Sounds right. A good example that you've probably seen before in C# is List (Systems.Collections.Generic.List<T>). If you haven't encountered it, basically it's just a container that you put things in (like an array), but the type of things you put in isn't limited to one type; you define the type of things it holds with the Type parameter "T".

As another example of using generics: IEquatable<T>. IEquatable is an interface that says you implement an Equals method that returns a bool. But the type of object you're comparing against is defined by T. So if your class implements IEquatable<Foo> that means you must have a method:
Code:
public bool Equals(Foo f)
You can implement it several times, with a different type each time. Or it can be the same type as your own class.

Here's an example of that, which says that a Crab can be compared with a Tiger for equality, and it returns true if they have the same name:
Code:
public class Crab : IEquatable<Tiger>
{
	public string Name;
	
	public bool Equals(Tiger t)
	{
		if(t == null)
		{
			return false;
		}
		return t.Name == this.Name;
	}
}

public class Tiger
{
	public string Name;
}
Notice Tiger doesn't implement IEquatable<Crab>. This is because I'm lazy, but even without it you can call myCrab.Equals(myTiger), just not the other way around.

Defining generics on your own class is only slightly more complicated than using them. Once you have the concept down that T (or whatever other name you use for the generic type) represents a Type, then you're 90% of the way there.

Here's an example of defining your own:
Code:
public class ConfigProvider
{
	public T GetConfig<T>(string filename) where T : class, new()
	{
		var configText = File.ReadAllText(filename);
		T config = Json.Deserialize<T>(configText);
		return config;
	}
}
Here, we have a class called ConfigProvider which can provide a config object by reading a json file from disk. You can use this same class for any type of json config, because it accepts the type parameter T as part of the method GetConfig.
Code:
public class ServerConfig
{
	public string Url;
	public int TimeoutMs;
	public string UserName;
	public string Password;
}

public class PlayerMotionConfig
{
	public float Acceleration;
	public float AirFriction;
	public float MaxSpeed;
}

void Main()
{
	ConfigProvider configProvider = new ConfigProvider();
	
	ServerConfig serverConfig = configProvider.GetConfig<ServerConfig>("serverCfg.json");
	PlayerMotionConfig motionConfig = configProvider.GetConfig<PlayerMotionConfig>("motion.json");
}
The part "where T : class, new()" is a generic constraint[1][2]. It specifies criteria that T has to meet, otherwise it won't compile. In this case, it says T has to be a class (not a struct or primitive) and it has to have an empty constructor. I only used this because most json decoders are going to have the same constraint.

This is a bit of a silly example, but hopefully you get the idea.
 

injurai

Banned
Solving a challenging programming problem in a clever way may be the most rewarding feeling I have ever experience. Can't wait for more challenge endeavors to come.
 
What's GAF's thoughts on functional programming? I used some of it's philosophies on some Python code yesterday and I liked the results, very nice way of thinking.

Functional programming is absolutely critical to growing as a programmer and even if you never write a program in a functional language for actual use the concepts you can learn will vastly improve other imperative programming you do as well. Everyone who is remotely interested in programming should at least expose themselves to a more pure functional language, Haskell or Scheme or similar.
 

iapetus

Scary Euro Man
Solving a challenging programming problem in a clever way may be the most rewarding feeling I have ever experience. Can't wait for more challenge endeavors to come.

Funny - I was just commenting to a colleague that 'clever' is the most dangerous word that a coder can ever use. :)
 

injurai

Banned
Funny - I was just commenting to a colleague that 'clever' is the most dangerous word that a coder can ever use. :)

I'm probably not doing anything too crazy, but I created a really beautiful way of randomly returning a character from a unordered multiset of characters with the probabilty that any character that is returned matches the frequency at which it appears in the multiset.

I just did a whole bunch of stuff that involved casting things to and from Float collections and and using its abstract state space as a sort of physical pinwheel to get a random distribution. It's accurate to within 1% when testing a large enough sample size that the frequency returned matches the frequency that its in the set.
 
Functional programming is absolutely critical to growing as a programmer and even if you never write a program in a functional language for actual use the concepts you can learn will vastly improve other imperative programming you do as well. Everyone who is remotely interested in programming should at least expose themselves to a more pure functional language, Haskell or Scheme or similar.

Haskell's been on my radar for the longest time. After you've inherited enough projects where nearly every function changes any number of states in some other random location in the project, just the single, simple concept of functions that don't cause any side effects can have a dramatic effect. I can't count the amount of hours I've spend digging through spaghetti code tracking down bugs. It's annoying to say the least.
 

injurai

Banned
Haskell's been on my radar for the longest time. After you've inherited enough projects where nearly every function changes any number of states in some other random location in the project, just the single, simple concept of functions that don't cause any side effects can have a dramatic effect. I can't count the amount of hours I've spend digging through spaghetti code tracking down bugs. It's annoying to say the least.

I'm not familiar with functional languages. Is haskell not object oriented? Or are you just talking about static methods?
 

neemmss

Member
The obnoxious response to this is to say that the right way to learn PHP is to not learn it at all. But the best way to learn it is really to make sure that you are following the most modern conventions. A lot of PHP is bad old junk that's in there because it's in there rather than because it's useful or effective.

I'm far from an expert on PHP, mainly because I refuse to use it for anything, but when I did have to learn it before, I found the Official Documentation to be a reasonably coherent explanation of everything. The comments that people post tend to fill in the blanks or provide opionions on best practice which are usually good. Sometimes a giant flame war erupts in the comments which is your clue that the topic in question is a particularly murky one.

I also came across this the other day, and while I haven't read it in detail, from a quick look through it seems to cover most of the bases well.

You said "switch to", which language(s) are you familiar with already? That might affect the best way for you to approach PHP, depending on existing knowledge.

Why not learn php?
I will have a look at the offical documentation. I think that the New Boston tutorials are going off of those, but am not 100% sure, is that a good way to start?

I was starting to learn python, and then get into web development with it later, but I decided with the vast support and knowledge of php out there it might be a little easier to learn php.
 

nicjac

Member
Hey guys,

I was wondering if you could give me a hand with a bit of C++. I learnt the basics a while back but haven't touched anything C++ in a long time.

Anyway, I have an object with a 3-dimensional std::vector field and I would like to serialize that object (or at least find a way to write that 3d vector to the disk). Any idea? A colleague suggested the Boost library but I am not sure it can be used with multidimensional vectors.

Any advice welcome!
 

squidyj

Member
this isn't really directly tied to any language or anything but as with all raytracers I need to intersect a metric ton of triangles, well, triangles in this instance anyways.

My triangle intersection using barycentric test preprocesses two edge vectors and their cross product on loading the data.

in the intersection itself I compute 4 dot products(3 mult 2 add) and 1 cross product(6mult 3sub) and additional 2 divisions and some .

I'm fairly convinced I can reduce my workload in the test more but I just don't see how my values would be further reducible.
 

Spoo

Member
Hey guys,

I was wondering if you could give me a hand with a bit of C++. I learnt the basics a while back but haven't touched anything C++ in a long time.

Anyway, I have an object with a 3-dimensional std::vector field and I would like to serialize that object (or at least find a way to write that 3d vector to the disk). Any idea? A colleague suggested the Boost library but I am not sure it can be used with multidimensional vectors.

Any advice welcome!

Hey nic (whar is dudebro!11)

S'far as I know, you may be stuck with overloading >> and << operators for use with stream objects (vanilla file io in C++ is all you get out of the box). Everyone does seem to suggest Boost, which I assume would work, but you get to specify how you want to write to a file (binary data, which is not recommended, or xml/json/what-have-you). Or just a flat file with your own format.

small edit: It's not actually necessary to overload the operators -- you could just as easily have a function Write() and Read() and pass in a stream reference.
 

Pachimari

Member
Do we discuss website coding and programming in this thread as well? Or do we have another general thread for that?

It's because I'm trying to implement the Disqus.com comment system on my website and this is what I got:

Q6jbLl.png


It works but when I go to Community it shows the thread as 'The Vampire Diaries - Denmark' while my page's title is something else. How can I do, so each page has each their headline for the thread?
 

injurai

Banned
Clever = "I don't know why it works but it does!"


worksforme.png.exe

I've gotten programs to work by changing interval values or counters by 1 digit. Probably a bad thing to do, I made the change in the first place on a hunch, but I don't know how the values are being changed down the line.

Seems mostly like it is compensating for digit truncation that happens to integers.
 

Harpuia

Member
Argh, GAF I do not know why this do/while loop won't work! Is there something wrong with where I put my end while? I can't figure it out, it just terminates and doesn't ask me to enter a value for again. Help for a newbie please. :(

If someone else can find a reason why this isn't working correctly can they shoot me a dm?

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double Pay=.01, Days, IncreaseF=0;
	const int INI=1;
	int Ini=INI;
	char again;

   do
   {

	cout<<"Welcome to PENNYMASTER"<<endl;
	cout<<"Type in the amount of days you'll be working and I will give you a neat little chart showing";
	cout<<" the pay for each day you work!"<<endl;
	cin>>Days;

	if(Days<INI)
	 cout<<"Hey, that's an invalid input! Now you'll have to reload the program!"<<endl;
	else
	{
	 cout<<"Day#              Pay"<<endl;
	 cout<<"---------------------"<<endl;
	 while(Ini<=Days)
	 {
	  cout<<Ini<<"\t\t"<<(Pay*pow(2, IncreaseF))<<endl;
	  Ini++;
	  IncreaseF++;
	 }
	}
	cout<<"Do you wish to enter a new value?"<<endl;
	cout<<"Y or y for YES, N or n for No"<<endl;
	cin>>again;
   } while ( again=='Y' || again=='y');
	return 0;
}
 

usea

Member
Argh, GAF I do not know why this do/while loop won't work! Is there something wrong with where I put my end while? I can't figure it out, it just terminates and doesn't ask me to enter a value for again. Help for a newbie please. :(

Have you tried printing the value of "again" after the loop ends, to see why it ended?

edit: I ran your code and it worked fine I guess? Here is the output:
Code:
./a.out
Welcome to PENNYMASTER
Type in the amount of days you'll be working and I will give you a neat little chart showing the pay for each day you work!
5
Day#              Pay
---------------------
1               0.01
2               0.02
3               0.04
4               0.08
5               0.16
Do you wish to enter a new value?
Y or y for YES, N or n for No
y
Welcome to PENNYMASTER
Type in the amount of days you'll be working and I will give you a neat little chart showing the pay for each day you work!
1
Day#              Pay
---------------------
Do you wish to enter a new value?
Y or y for YES, N or n for No
y
Welcome to PENNYMASTER
Type in the amount of days you'll be working and I will give you a neat little chart showing the pay for each day you work!
10
Day#              Pay
---------------------
6               0.32
7               0.64
8               1.28
9               2.56
10              5.12
Do you wish to enter a new value?
Y or y for YES, N or n for No
n
There seems to be a bug where some of the variables don't get reset when the loop loops.
 

Harpuia

Member
Have you tried printing the value of "again" after the loop ends, to see why it ended?

Hmm, before return 0;? Because I tried making it print again, and nothing shows up. I even assigned it a value and for some reason the whole thing completely bypasses the display of the prompt asking to enter a value for again.

Huh, that's curious. I've honestly no clue what seems to be the problem to be honest.

Hrm, well, in any case, a do/while loop isn't necessary for my assignment, it was just a little something extra I wanted to put in to make it easier to rerun if you put in an incorrect value.
 

Hari Seldon

Member
So this coursera class and my own pissing around has got me hugely loving Python. But I am kind of concerned that Python is pretty weak when it comes to non-hobby level programming. I was reading up on threading, and apparently python has zero support for parallel programming, and even normal OS level threading is weak. This is unfortunate because I am not experienced in threading and parallel programming at all, and was hoping that it was going to be easy in Python in order to ease myself into it. Apparently I need to either launch multiple instances of different python processes from a C program or possibly use stackless python. Doesn't seem very noob friendly in order to have to do all of this, which kind of goes against the rest of python. Any word if they are fixing this shit in future versions of Python?
 

usea

Member
So this coursera class and my own pissing around has got me hugely loving Python. But I am kind of concerned that Python is pretty weak when it comes to non-hobby level programming. I was reading up on threading, and apparently python has zero support for parallel programming, and even normal OS level threading is weak. This is unfortunate because I am not experienced in threading and parallel programming at all, and was hoping that it was going to be easy in Python in order to ease myself into it. Apparently I need to either launch multiple instances of different python processes from a C program or possibly use stackless python. Doesn't seem very noob friendly in order to have to do all of this, which kind of goes against the rest of python. Any word if they are fixing this shit in future versions of Python?
Python is very popular in certain sectors. Tons of websites use it for the server code, also the science community uses it for a lot of things.

Like any language it has some weaknesses. I don't know enough about it to say that concurrent programming is one of them, but it wouldn't surprise me. I don't think it's accurate to say it's weak for non-hobby programming, and I definitely don't think it'd be a waste of time professionally to get good at it.
 

Ya no

Member
Hey GAF, I'm trying to write a really basic perl script for a class and I'm having trouble (I have no programming experience). The script asks your name, then lists all the computer related majors at my school. From there it asks what your major is. The problem is that I can't figure out how to get it to verify the major you enter is in the list. No matter what is entered it prints out "This major does not exist."

Code:
#!/usr/bin/perl
print ("What is your name? ");
$name = <STDIN>;
print ("Welcome, $name\n");
@majors = ("Sec", "ComSc", "CIS", "WebDesign", "IT", "SAS");
print ("These are the computer majors at RWU:\n");
print ("$majors[0]\n");
print ("$majors[1]\n");
print ("$majors[2]\n");
print ("$majors[3]\n");
print ("$majors[4]\n");
print ("$majors[5]\n");


print ("What is your major? ");
$major = <STDIN>;
chomp $major;
if ($major == @majors)

{
        print ("Thank you for your time. \n");
}
else

{
        print ("This major does not exist. \n");
}

I've tried many different ways, but they all have given me the same result. I hope that it's just something simple that I'm missing.
 

Zoe

Member
Hey GAF, I'm trying to write a really basic perl script for a class and I'm having trouble (I have no programming experience). The script asks your name, then lists all the computer related majors at my school. From there it asks what your major is. The problem is that I can't figure out how to get it to verify the major you enter is in the list. No matter what is entered it prints out "This major does not exist."

I've tried many different ways, but they all have given me the same result. I hope that it's just something simple that I'm missing.

You're comparing a single major to an entire set of majors.
 

Ya no

Member
You're comparing a single major to an entire set of majors.

Is there a way to write it so it compares your input to the set? And if what you input is in the set have it print "Thanks for your time." That's what I'm trying to do.
 

Zoe

Member
Is there a way to write it so it compares your input to the set? And if what you input is in the set have it print "Thanks for your time." That's what I'm trying to do.

You need to either iterate through the set and store the results of a successful comparison against a member, or use a search function on the set (not sure if Perl has one).
 

qwerty2k

Member
Anyone got any suggestions for 3d programming for c#, so far i've seen XNA and OpenTK, are there better alternatives out there?

Ideally just want to mess around with procedurally generating terrain (blocks like mine craft) and then texture them based on height etc :).
 

Ya no

Member
You need to either iterate through the set and store the results of a successful comparison against a member, or use a search function on the set (not sure if Perl has one).

I went through each entry in the set using elsif statements and that worked. Thanks!
 

SolKane

Member
Argh, GAF I do not know why this do/while loop won't work! Is there something wrong with where I put my end while? I can't figure it out, it just terminates and doesn't ask me to enter a value for again. Help for a newbie please. :(

If someone else can find a reason why this isn't working correctly can they shoot me a dm?

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	double Pay=.01, Days, IncreaseF=0;
	const int INI=1;
	int Ini=INI;
	char again;

   do
   {

	cout<<"Welcome to PENNYMASTER"<<endl;
	cout<<"Type in the amount of days you'll be working and I will give you a neat little chart showing";
	cout<<" the pay for each day you work!"<<endl;
	cin>>Days;

	if(Days<INI)
	 cout<<"Hey, that's an invalid input! Now you'll have to reload the program!"<<endl;
	else
	{
	 cout<<"Day#              Pay"<<endl;
	 cout<<"---------------------"<<endl;
	 while(Ini<=Days)
	 {
	  cout<<Ini<<"\t\t"<<(Pay*pow(2, IncreaseF))<<endl;
	  Ini++;
	  IncreaseF++;
	 }
	}
	cout<<"Do you wish to enter a new value?"<<endl;
	cout<<"Y or y for YES, N or n for No"<<endl;
	cin>>again;
   } while ( again=='Y' || again=='y');
	return 0;
}

Hmm, before return 0;? Because I tried making it print again, and nothing shows up. I even assigned it a value and for some reason the whole thing completely bypasses the display of the prompt asking to enter a value for again.

Huh, that's curious. I've honestly no clue what seems to be the problem to be honest.

Hrm, well, in any case, a do/while loop isn't necessary for my assignment, it was just a little something extra I wanted to put in to make it easier to rerun if you put in an incorrect value.

I got this to run just fine. To fix the problem you're talking about you should take a look at the Ini variable within the inner while loop. You increment it for each pass of the this loop, and I'm not sure why. This isn't a serious error by any means, but it prevents the user from ever entering a number lower than one which has already been entered. For example, I enter 5 the first time, and any number less than equal to five will skip the inner while loop completely and go straight to the priming read for Yes/No. If what you want to do is prevent the user from entering an invalid number of days (eg, a negative number), you can try to rewrite your if/else statement. I think you could further simplify this code quite a bit, if I'm understanding your intention properly.
 

injurai

Banned
If I want to write multi-platform C++ code am I able to still write and work in Visual Studio? I suppose I probably couldn't compile and run it from there. This is assuming I use solely multi platform libraries such as QT. I really don't have any knowledge on how to package and deploy code :|
 

usea

Member
If I want to write multi-platform C++ code am I able to still write and work in Visual Studio? I suppose I probably couldn't compile and run it from there. This is assuming I use solely multi platform libraries such as QT. I really don't have any knowledge on how to package and deploy code :|
Yeah you can use Visual Studio for writing the code, no problem. However, you'll have to build for other platforms with different compilers, with different platform targets. To do this, your code has to be cross-platform (or at least portable).
 
Back again with my java problems...

Im having trouble getting the average of a two dimensional array, but one column at a time. So in this piece of code I am trying to get column 0's average. This is what I have

Code:
public static void Exam1Average(int[][]grades)
	{
		int sum = 0;
		for(int row=0; row<=4; row++)
			sum = grades[row][0] + sum;
         }

I KNOW I am missing something... but I cant remember what I forgot.

quick edit: My output is the first number I input into the array/5
 
I fixed it. My Problem was with another method where I load the array.
but thats what I get for getting frustrated.

I am going to have trouble trying to find the average of the rows though. How would I go about doing that? The program specifies 5 rows, 2 columns.
 

r1chard

Member
So this coursera class and my own pissing around has got me hugely loving Python. But I am kind of concerned that Python is pretty weak when it comes to non-hobby level programming. I was reading up on threading, and apparently python has zero support for parallel programming, and even normal OS level threading is weak. This is unfortunate because I am not experienced in threading and parallel programming at all, and was hoping that it was going to be easy in Python in order to ease myself into it. Apparently I need to either launch multiple instances of different python processes from a C program or possibly use stackless python. Doesn't seem very noob friendly in order to have to do all of this, which kind of goes against the rest of python. Any word if they are fixing this shit in future versions of Python?

Python has great support for concurrent programming using threading and multi-process programming built in. It also has a number of other avenues like asynchronous and greenlet based approaches. Those are third-party, but mature and popular.

But threading and multiprocessing are built-in, supported and very easy to use.

The only catch with Python, as far as I'm aware, is that if you're programming in parallel for reasons of performance (ie. you have 8 cores and you must exercise all of them in parallel) then you don't want to use threading as an approach. Python's threading doesn't extend to multiple cores (it's not alone in this either.) In that case it's almost always simple enough to just switch from a threaded approach to a multiprocessing approach. In fact Python even has a high-level library called futures to make such programming a doddle. Futures lets you use either threading or multiprocessing behind the scenes depending on whether your application is CPU (eg. prime number testing) or I/O bound (eg. URL fetching.)

ps. I've been a professional non-hobbyist developer of non-website systems for over 10 years in Python ;-)
 

Zoe

Member
I fixed it. My Problem was with another method where I load the array.
but thats what I get for getting frustrated.

I am going to have trouble trying to find the average of the rows though. How would I go about doing that? The program specifies 5 rows, 2 columns.

Just a note, it's more common to use < as your end condition rather than <=

When you're dealing with multi-dimensional arrays, you need nested loops.
 
Top Bottom