• 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

kadotsu

Banned
You have to replace "this is also a test" with new Alpha("this is also a test") because the constructor is expecting a Alpha object and not a String.
 
You're both good people. The amount of time I spent banging my head against the keyboard over that is pretty ridiculous. Seriously, thanks a bunch.

Out of interest, is there a way (outside the main method) to convert an Alpha class object to a String (or int) while still using it in the Beta constructor? If I can't convert these objects then I can see myself having major problems with collections.
 

kadotsu

Banned
You're both good people. The amount of time I spent banging my head against the keyboard over that is pretty ridiculous. Seriously, thanks a bunch.

Out of interest, is there a way (outside the main method) to convert an Alpha class object to a String (or int) while still using it in the Beta constructor? If I can't convert these objects then I can see myself having major problems with collections.

You think of it the wrong way. Objects have a type and a behaviour. An Alpha isn't a String. If you had an object that could just be converted to a String why would you not use a String in the first place.

You can however give the Alpha object a behavior (method) that represents the object in a String like fashion. You already did this by overriding the toString method.

You will be able to do even more stuff once you understand interfaces. With them you can give objects expected behavior.
 
You think of it the wrong way. Objects have a type and a behaviour. An Alpha isn't a String. If you had an object that could just be converted to a String why would you not use a String in the first place.

You can however give the Alpha object a behavior (method) that represents the object in a String like fashion. You already did this by overriding the toString method.

You will be able to do even more stuff once you understand interfaces. With them you can give objects expected behavior.
Thanks, that's helpful. Guess I need to learn more before I start thinking about things in the right way. Will keeping working at this and see what results.
 
My program is giving me a segmentation fault when I try to catch interrupt signals and I have no idea why or what to do.

Code:
int main(){
	char *token;
	const char *delim = " \n";
	char userInput[SETMAX];
       int exitLoop = 0;
	
	struct sigaction act;
	act.sa_handler = SIG_IGN; 
	sigaction(SIGINT, &act, NULL);

       while (exitLoop == 0) 
	{
		struct mystruct C; 
		initCom(&C);
		printf("Input: "); 
		fflush(stdout);
		fgets(userInput, sizeof(userInput), stdin); //grab user input
		
		token = strtok(userInput, delim);
		printf("Hello World\n");
		
		while (token != NULL) //analyze tokens
		{
			printf("Goodbye\n");
             .......


The first Hello World prints. It never makes it to the Goodbye statement.

edit: Nevermind. Made a booboo in checking an array.
 

Jokab

Member
I was asked to redefine the function listOf in Haskell to take an integer n and create a list in which each element is generated by g, and then write a test using QuickCheck seeing that it works. I got this code:

Code:
listOf' :: Integer -> Gen a -> Gen [a]
listOf' n g = vectorOf (fromInteger n) g

prop_listOf' n g = forAll (listOf' n g) (\e -> length e == fromInteger n)

It compiles but when I run it, I get:

Code:
*Main > quickCheck (prop_listOf' (5 (arbitrary::Gen Integer)))

<interactive>:510:1: error:
    * No instance for (Show (Gen a0))
        arising from a use of `quickCheck'
    * In the expression:
        quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))
      In an equation for `it':
          it = quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))

<interactive>:510:13: error:
    * Ambiguous type variable `a0' arising from a use of prop_listOf'
      prevents the constraint `(Show a0)' from being solved.
      Probable fix: use a type annotation to specify what `a0' should be.
      These potential instances exist:
        instance Show (Blind a) -- Defined in `Test.QuickCheck.Modifiers'
        instance Show a => Show (Fixed a)
          -- Defined in `Test.QuickCheck.Modifiers'
        instance Show a => Show (Large a)
          -- Defined in `Test.QuickCheck.Modifiers'
        ...plus 37 others
        ...plus 89 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    * In the first argument of `quickCheck', namely
        `(prop_listOf' (5 (arbitrary :: Gen Integer)))'
      In the expression:
        quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))
      In an equation for `it':
          it = quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))

<interactive>:510:27: error:
    * No instance for (Num (Gen Integer -> Integer))
        arising from the literal `5'
        (maybe you haven't applied a function to enough arguments?)
    * In the expression: 5
      In the first argument of prop_listOf', namely
        `(5 (arbitrary :: Gen Integer))'
      In the first argument of `quickCheck', namely
        `(prop_listOf' (5 (arbitrary :: Gen Integer)))'

I'm having a hard time parsing what it's telling me here. Why does it need to show anything?
 

vypek

Member
Recently started learning Python. It is really fun although at first I had the habit of having to go back to erase a semicolon. I'm also trying to use it with Tumblr API to make a small application but something is definitely going wrong with it so far. Maybe I'll ask their devs what is wrong.
 
I was asked to redefine the function listOf in Haskell to take an integer n and create a list in which each element is generated by g, and then write a test using QuickCheck seeing that it works. I got this code:

I'm having a hard time parsing what it's telling me here. Why does it need to show anything?

Not a Haskell guy at all, but I think you might need to add type annotations to the prop_listOf' function.
 
I was asked to redefine the function listOf in Haskell to take an integer n and create a list in which each element is generated by g, and then write a test using QuickCheck seeing that it works. I got this code:

Code:
listOf' :: Integer -> Gen a -> Gen [a]
listOf' n g = vectorOf (fromInteger n) g

prop_listOf' n g = forAll (listOf' n g) (\e -> length e == fromInteger n)

It compiles but when I run it, I get:

Code:
*Main > quickCheck (prop_listOf' (5 (arbitrary::Gen Integer)))

<interactive>:510:1: error:
    * No instance for (Show (Gen a0))
        arising from a use of `quickCheck'
    * In the expression:
        quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))
      In an equation for `it':
          it = quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))

<interactive>:510:13: error:
    * Ambiguous type variable `a0' arising from a use of prop_listOf'
      prevents the constraint `(Show a0)' from being solved.
      Probable fix: use a type annotation to specify what `a0' should be.
      These potential instances exist:
        instance Show (Blind a) -- Defined in `Test.QuickCheck.Modifiers'
        instance Show a => Show (Fixed a)
          -- Defined in `Test.QuickCheck.Modifiers'
        instance Show a => Show (Large a)
          -- Defined in `Test.QuickCheck.Modifiers'
        ...plus 37 others
        ...plus 89 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    * In the first argument of `quickCheck', namely
        `(prop_listOf' (5 (arbitrary :: Gen Integer)))'
      In the expression:
        quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))
      In an equation for `it':
          it = quickCheck (prop_listOf' (5 (arbitrary :: Gen Integer)))

<interactive>:510:27: error:
    * No instance for (Num (Gen Integer -> Integer))
        arising from the literal `5'
        (maybe you haven't applied a function to enough arguments?)
    * In the expression: 5
      In the first argument of prop_listOf', namely
        `(5 (arbitrary :: Gen Integer))'
      In the first argument of `quickCheck', namely
        `(prop_listOf' (5 (arbitrary :: Gen Integer)))'

I'm having a hard time parsing what it's telling me here. Why does it need to show anything?

Its been a while since i've used Haskell, but i think that quickcheck writes failed tests to the console, so it needs Show to be able to do that.
 

Jokab

Member
Not a Haskell guy at all, but I think you might need to add type annotations to the prop_listOf' function.

I did try that, and the type would then be Integer -> Gen a -> Property, but it makes no difference.

Its been a while since i've used Haskell, but i think that quickcheck writes failed tests to the console, so it needs Show to be able to do that.
I guess... But how would I fix it?
 

theecakee

Member
When do companies typically hire their interns? I've applied to a few big places like Airbnb, Esri, Under Armor, and etc plus some local companies. All I have gotten rejections or just silence.

I need to work on my resume more, all I have is like a portfolio site made, one website made, and a few small dumb scripts. Working on this rails app right now and participating in a hackathon on Dec 3rd. But damn being rejected is soul crushing.
 

Kansoku

Member
Guys, this is tangentially related to programming, but I don't know where else to ask.

Does anyone knows somewhere to share surveys? I made a survey to validade the results from a neural network for my Bachelor Thesis, but I'm having trouble getting results.
 

Jokab

Member
Guys, this is tangentially related to programming, but I don't know where else to ask.

Does anyone knows somewhere to share surveys? I made a survey to validade the results from a neural network for my Bachelor Thesis, but I'm having trouble getting results.

At my programme there are occasionally surveys sent out for school projects through the programme email. Maybe try asking the programme managers if they can help you out?
 

Kansoku

Member
At my programme there are occasionally surveys sent out for school projects through the programme email. Maybe try asking the programme managers if they can help you out?

*facepalm*. Why didn't I think of that? lol

Will talk to my advisor about that. Thanks.
 
While at work the other day, I realized that sometimes I look at my code and wonder if I'm handling exceptions correctly. I am working in C#, and I have a few questions/comments:

-I feel like I have try/catch statements all over my code and to me, that feels wrong. Is it common to have these blocks all over a code base? It seems messy.
-I have difficulty knowing when to use try/catch due to the many "levels" of the application. For example, there might be a try/catch around "o.DoSomething()" in main, but within that method, there might be another try/catch block already there.
-I assume I always want to aim to not handle general exceptions, but instead handle specific exceptions. Is this the correct mindset?

Unfortunately, I am the only one looking at the code a lot of the time so I tend to see these issues and possibly confuse myself more than I should. This project deals with users editing configuration files so there is a LOT of room for user error in the project's current form. Are there any good guides out there focused on this topic? Exception handling definitely seems to be one of my weaker spots when it comes to implementation and I'd like to improve that.
 

Makai

Member
While at work the other day, I realized that sometimes I look at my code and wonder if I'm handling exceptions correctly. I am working in C#, and I have a few questions/comments:

-I feel like I have try/catch statements all over my code and to me, that feels wrong. Is it common to have these blocks all over a code base? It seems messy.
-I have difficulty knowing when to use try/catch due to the many "levels" of the application. For example, there might be a try/catch around "o.DoSomething()" in main, but within that method, there might be another try/catch block already there.
-I assume I always want to aim to not handle general exceptions, but instead handle specific exceptions. Is this the correct mindset?

Unfortunately, I am the only one looking at the code a lot of the time so I tend to see these issues and possibly confuse myself more than I should. This project deals with users editing configuration files so there is a LOT of room for user error in the project's current form. Are there any good guides out there focused on this topic? Exception handling definitely seems to be one of my weaker spots when it comes to implementation and I'd like to improve that.
I only use try-catch when the app definitely needs to not crash when that function fails. Usually you want it to crash so you can find the error early. Rarely use exceptions other than switch statements since C# can't figure out when I've exhaustively matched.
 
I only use try-catch when the app definitely needs to not crash when that function fails. Usually you want it to crash so you can find the error early. Rarely use exceptions other than switch statements since C# can't figure out when I've exhaustively matched.

This application is difficult to work on at times since it feels like almost everything COULD fail lol. However, many of the main operations do not rely on each other, so I am attempting to handle exceptions as best as possible so that the program will complete a run through even if parts of it fail. We are currently logging those failures to help the user make adjustments to their input via the files, but sometimes the logs are far too generalized to be helpful and that is basically what I'd like to fix.
 

Somnid

Member
While at work the other day, I realized that sometimes I look at my code and wonder if I'm handling exceptions correctly. I am working in C#, and I have a few questions/comments:

-I feel like I have try/catch statements all over my code and to me, that feels wrong. Is it common to have these blocks all over a code base? It seems messy.
-I have difficulty knowing when to use try/catch due to the many "levels" of the application. For example, there might be a try/catch around "o.DoSomething()" in main, but within that method, there might be another try/catch block already there.
-I assume I always want to aim to not handle general exceptions, but instead handle specific exceptions. Is this the correct mindset?

Unfortunately, I am the only one looking at the code a lot of the time so I tend to see these issues and possibly confuse myself more than I should. This project deals with users editing configuration files so there is a LOT of room for user error in the project's current form. Are there any good guides out there focused on this topic? Exception handling definitely seems to be one of my weaker spots when it comes to implementation and I'd like to improve that.

Use it where something out of your control can fail (disk access, network access etc). Otherwise don't do that. Also avoid returning null as it forces others to handle those cases (and they often don't). A common pattern would be to make a result class that wraps the expected thing and has an enum/flag that indicates whether or not it's successful.
 

Realeza

Banned
Can any of you beautiful fellas help me with this Java problem:

3xJ5qSn.png


I can't, for the life of me, figure out how to set up the rod class. I'm wasting time with no progress. So far I came with this:

import java.util.*;

public class Rod {

private ArrayList<Integer> arrayL = new ArrayList<Integer>();
private int numberOfDisks;

public Rod(int numberOfDisks) {
super();
this.numberOfDisks = numberOfDisks;

// Depending on the # of disks entered by user, this populates list
// array

for (int i = numberOfDisks; i > 0; i--) {
arrayL.add(i);
}

}

public ArrayList<Integer> getArrayL() {
return arrayL;
}

public int getNumberOfDisks() {
return numberOfDisks;
}

public int getDisk() {
return arrayL.get(arrayL.size());
}

public int addDisk() {
return getDisk();
}

public String toString() {
return; //contents of string how??

}

}

But doesn't feel right. Any help is appreciated.
 

Massa

Member
Hey everyone. New to web stuff here.

I'm writing a web based interface for a program that takes an input file, crunches it (takes anywhere from 1 to 30 minutes) and returns an output zip file. Using python and flask I quickly threw something together and basically "/hash/download" returns the output file if it's done, or a simple error message if it's still processing. (hash is the md5 of the input file).

Question is, what should I look into implementing so that the client page doesn't have to send a request "/hash/download" every 5s?
 

Kalnos

Banned
Hey everyone. New to web stuff here.

I'm writing a web based interface for a program that takes an input file, crunches it (takes anywhere from 1 to 30 minutes) and returns an output zip file. Using python and flask I quickly threw something together and basically "/hash/download" returns the output file if it's done, or a simple error message if it's still processing. (hash is the md5 of the input file).

Question is, what should I look into implementing so that the client page doesn't have to send a request "/hash/download" every 5s?

WebSockets if you're looking to keep a connection and constantly update them. Assuming I'm understanding you right.

https://flask-socketio.readthedocs.io/en/latest/
https://github.com/kennethreitz/flask-sockets
 

Somnid

Member
Hey everyone. New to web stuff here.

I'm writing a web based interface for a program that takes an input file, crunches it (takes anywhere from 1 to 30 minutes) and returns an output zip file. Using python and flask I quickly threw something together and basically "/hash/download" returns the output file if it's done, or a simple error message if it's still processing. (hash is the md5 of the input file).

Question is, what should I look into implementing so that the client page doesn't have to send a request "/hash/download" every 5s?

-Long polling
-Websockets
-Server-sent events
-HTTP streams
-WebRTC data channel

Really depends on your browser compatibility needs as they all have their ups and downs.
 

Massa

Member
-Long polling
-Websockets
-Server-sent events
-HTTP streams
-WebRTC data channel

Really depends on your browser compatibility needs as they all have their ups and downs.

I'll check those out too, thanks.

Browser requirements are not a problem, it's for internal use.
 

Somnid

Member
I'll check those out too, thanks.

Browser requirements are not a problem, it's for internal use.

SSE was built for this sort of stuff but it's not compatible with MS browsers so nobody uses it. Long polling requires nothing special, any browser with AJAX can do it and this is how you mostly see it done. Websockets are very compatible and probably the "best" general option though they're a little more geared toward a continuous stream of data. WebRTC data channels are the same thing but peer-to-peer but have difficult setup and server-side libraries for them aren't mature. HTTP streams would be the best performing option, just stream the zip as it's being made but few browsers support it.
 

zeemumu

Member
I'm trying to fill these private double members in this object with doubles stored in an array that gets passed to the object as a parameter but it keeps coming up with different numbers (like 2.023904e-310). Is it passing the address of the array element into the variable? How do I fix that?
 

Somnid

Member
I'm trying to fill these private double members in this object with doubles stored in an array that gets passed to the object as a parameter but it keeps coming up with different numbers (like 2.023904e-310). Is it passing the address of the array element into the variable? How do I fix that?

Need example code
 
For whatever reason this isn't working the way I want it to. I need this to successfully update a mysql database. This is in express/express-handlebars.

Code:
  app.get('/edit', function(req, res, next) {
	  console.log(req.query.fname);
	  console.log(req.query.lname);
	  connection.query("UPDATE `users` SET fname=?, lname=?, pword=? WHERE userID =?", [req.query.fname, req.query.lname, req.query.pword, req.query.userID], function(err, result){
	  
	 if(err){
		  next(err);
		  return;
	  }
	  
	   res.render('home', {user: req.user})
  });
  });

console.log statements show the correct stuff is being sent in the query when the user submits an edit to a data field.

Pretty sure my sql syntax is correct. I'm not getting any parse errors.

The page just goes from the edit screen to rendering 'home' , with no update to the database actually being applied.

Any idea why?
 
For whatever reason this isn't working the way I want it to. I need this to successfully update a mysql database. This is in express/express-handlebars.

Code:
app.get('/edit', function(req, res, next) {
    console.log(req.query.fname);
    console.log(req.query.lname);
    connection.query("UPDATE `users` SET fname=?, lname=?, pword=? WHERE userID =?", [req.query.fname, req.query.lname, req.query.pword, req.query.userID], function (err, result) {
        if (err) {
             next(err);
	     return;
        }
  
	res.render('home', {user: req.user})
    });
});

console.log statements show the correct stuff is being sent in the query when the user submits an edit to a data field.

Pretty sure my sql syntax is correct. I'm not getting any parse errors.

The page just goes from the edit screen to rendering 'home' , with no update to the database actually being applied.

Any idea why?

The `users` shouldn't probably be in backticks.

Furthermore, you should consider using `PUT` instead of `GET` requests for edits
 
The `users` shouldn't probably be in backticks.

Furthermore, you should consider using `PUT` instead of `GET` requests for edits

Thanks, I'll try that.

And yeah, I know it should be in PUT for edits. But I'll deal with that later.

edit: removing backticks changed nothing.

edit: figured it out.
 

Tristam

Member
Thanks, I'll try that.

And yeah, I know it should be in PUT for edits. But I'll deal with that later.

edit: removing backticks changed nothing.

edit: figured it out.

I notice you figured it out, but I imagine that the result object contains the raw SQL query that was executed, which is more useful for debugging than printing the variables to be bound. Also, Node has a good built-in REPL+debugger, where debugger; statements act as breakpoints if you run your Node script using
Code:
node debug <script>
. Once a breakpoint is hit, type in repl to enter REPL mode. Then you can analyze your variables to your heart's content. Documentation--https://nodejs.org/api/debugger.html
 

Koren

Member
Possibly stupid question, because I'm newbie for interactive js scripts.

Have someone managed to use a javascript with a read-eval-print-loop inside a Python script?

For example using subprocess.Popen?

Should I investigate or is it a lost cause?


(that's mostly a runaround solution because I can't install python modules on OS-X because of C compiler not working, and I don't want to spend hours doing system administration on OS-X)
 

Somnid

Member
Possibly stupid question, because I'm newbie for interactive js scripts.

Have someone managed to use a javascript with a read-eval-print-loop inside a Python script?

For example using subprocess.Popen?

Should I investigate or is it a lost cause?


(that's mostly a runaround solution because I can't install python modules on OS-X because of C compiler not working, and I don't want to spend hours doing system administration on OS-X)

I'm confused why this is necessary. Why not skip python and just use node?
 

Koren

Member
I'm confused why this is necessary. Why not skip python and just use node?
Because I'm not the developper, and the students that will write the code know only Python and don't have time to learn js (not that I'm fluent in js myself, to be honest).

Also, there's several numerical libraries in Python that will probably be useful, finding js equivalents may be hard.

Mostly, the time budget is low, and it's mostly prototyping to test ideas, not something that'll be used after.

It's just there is two ways to interact with hardware:
- a javascript module with a REPL mode
- a python module I haven't managed to install on OS-X and that will probably be troublesome on windows too (because it tries to compile code at installation... works flawlessly on Linux, though)
 

erpg

GAF parliamentarian
I'm doing some usability research and I'm looking to get people with dev experience to sign up for a few tests I'm planning on running in the coming weeks.

It'll be online, roughly 20-30 minutes and we'll need to record your screen + mic.
If any of you aren't familiar with usability testing, you'll be using an application and speaking your mind as you interact with it, expressing when things don't work the way you expect or when you hit any roadblocks. If any of you are up to it, please PM me for info or quote to view the form. You'll be compensated for your time (Amazon gift card).



Oh and if you sign up, DON'T try using the app beforehand.
 
- a python module I haven't managed to install on OS-X and that will probably be troublesome on windows too (because it tries to compile code at installation... works flawlessly on Linux, though)

Can I recommend creating a Docker container for development?

For example taking the Python container as a base (https://hub.docker.com/_/python/) and installing the Python module that communicates with the hardware through that.

https://docs.docker.com/docker-for-mac/
 
Top Bottom