Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

tef posted:

MY CONSISTENCY

This, but unironically.

tef posted:

python eschews anonymous definitions -- ask yourself how you would do this in a language without braces.

I can't answer that question other than by noting that it can be interpreted as an argument in favour of braces.

Adbot
ADBOT LOVES YOU

TasteMyHouse
Dec 21, 2006

tef posted:

MY CONSISTENCY

python eschews anonymous definitions

Except lambdas!

tef
May 30, 2004

-> some l-system crap ->

TasteMyHouse posted:

Except lambdas!

almost! they're not statements just expressions :eng101: (unlike def, class, etc)

tef
May 30, 2004

-> some l-system crap ->

Hammerite posted:

This, but unironically.

please go and learn a lisp variant.
that is what you deserve.

:q:

FlyingDodo
Jan 22, 2005
Not Extinct
Is there any reason in windows that a window would get a WM_QUIT message everytime it starts up? It's the very first thing that happens in the message loop. Is it possible I have not set up the window correctly?

edit: in c++ using the windows api.

TasteMyHouse
Dec 21, 2006

Hammerite posted:

This, but unironically.


A foolish consistency is the hobgoblin of little minds

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

tef posted:

MY CONSISTENCY

python eschews anonymous definitions -- ask yourself how you would do this in a language without braces.
I wrote a parser for a toy language with indentation-based scope, which allowed (required?) anonymous function defs. It wasn't worth it.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
python's lovely lambda really isn't much of a problem because if your function needs to be more than just an expression it's probably worth giving it a name anyway

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
When I was learning SML, I drove my TAs crazy by always writing

code:
fun foo [] [] = ...
  | foo (a::b) [] = ...
  | ...
as

code:
val foo = fn l1 => fn l2 => case (l1,l2) of
    ([],[]) => ...
  | (a::b,[]) => ...
  | ...
It's good to learn to think that way, but yeah, my code jumped in intelligibility by ten-fold when I finally started using fun.

tef
May 30, 2004

-> some l-system crap ->
it would be nice if python had a let/where statement, or some control over scoping

imaginary syntax time:

code:
do x=foo(a,b, callback):
    def callback(...):
         .....
however it doesn't make much difference without one.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

TasteMyHouse posted:

A foolish consistency is the hobgoblin of little minds

What this quote really says (to me, at least) is that if you have a good reason to be inconsistent then you should go ahead and be inconsistent. It seems to me that it's your job to show that there's good reason for inconsistency here. Or to put it another way, it's up to you to show that the consistency I am arguing for is indeed foolish. :)

tef
May 30, 2004

-> some l-system crap ->

Hammerite posted:

What this quote really says (to me, at least) is that if you have a good reason to be inconsistent then you should go ahead and be inconsistent. It seems to me that it's your job to show that there's good reason for inconsistency here. Or to put it another way, it's up to you to show that the consistency I am arguing for is indeed foolish. :)

See that bit above

in my post

The one where I pointed out that it doesn't fit with pythons syntax

That would be the reason against your consistency.

They would rather have whitespace indentation over anonymous definitions

edit:

and if you don't like it, fine, use another language. one that suits your taste

again, if you crave syntactic consistency, learn lisp

ToxicFrog
Apr 26, 2008


Hammerite posted:

This seems to betray a failure to truly treat functions as ordinary values just like any other type of values. If they were really so, you should have to write

Python has first-class functions but does not permit the definition of anonymous functions (except via the restricted keyword lambda).

code:
>>> def foo(x, y):
...     return x+y
... 
>>> bar = foo
>>> bar
<function foo at 0x1f57848>
>>> bar(4,5)
9
Even in languages which do have full anonymous functions, such as Lua, it's not uncommon to have syntactic sugar for function definition; these two statements are both legal and equivalent:

code:
function foo(x, y)
    return x+y
end

foo = function(x,y)
    return x+y
end
E: holy poo poo beaten so hard

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

tef posted:

again, if you crave syntactic consistency, learn lisp

Even here you have to be careful which lisp :q:

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
I'm not aware of any serious lisps that don't have sugar for declaring a function — given that it's lisp, that sugar might be a macro in the standard library, but the effect is the same: it's very rare to see lisp code that declares functions with lets and lambdas.

nielsm
Jun 1, 2009



FlyingDodo posted:

Is there any reason in windows that a window would get a WM_QUIT message everytime it starts up? It's the very first thing that happens in the message loop. Is it possible I have not set up the window correctly?

edit: in c++ using the windows api.

My best guess:
Your window procedure calls PostQuitMessage() in its WM_DESTROY handler, and your WM_CREATE handling is incorrect such that Windows assumes creation failed and immediately calls WM_DESTROY, which causes the WM_QUIT to be posted.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
Perhaps I shall learn Lisp. It sounds enlightening.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
even if you never actually use it everyone should learn a lisp

baquerd
Jul 2, 2007

by FactsAreUseless

Plorkyeran posted:

even if you never actually use it everyone should learn a lisp

The more flamboyant the better.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Hammerite posted:

Perhaps I shall learn Lisp. It sounds enlightening.

I haven't finished Barski's book (land of lisp) but it's real fun, if a little triumphalist. i would recommend it if you actually want to do this

tef
May 30, 2004

-> some l-system crap ->
To quote paul graham when talking about lisp: http://www.paulgraham.com/hijack.html

'The defense that does work is to keep code and data in separate places. Then there is no way to compromise code by playing tricks with data. Garbage-collected languages like Perl and Lisp do this, and as a result are immune from buffer overflow attacks.

you see, lisp isn't about code as data, code and data are seperate

Le0
Mar 18, 2009

Rotten investigator!
I'm looking for a program that could provide metrics on my code (line of codes, nb of function, cyclomatic and stuff) what would be the best solution ? Bonus point if it is a free one :)

Effulgence
Jun 10, 2006

n. a brilliant radiance
I need to create a daily email to send to my group at work including several news articles that are published daily. The goal is for my email to be nearly completely automated. I'm not really sure how to approach the problem (I've had a few ideas but no immediate solutions have occurred to me) because of my limited programming experience in Office with VBA.

CME publishes daily Market Report Recaps that I would like to incorporate automatically into the email. The same 6-7 recaps will be sent each day.

Method 1:
Each market report recap has a specific link on the CME webpage (ex: http://www.cmegroup.com/education/market-commentary/energy/2011/08/recap-energy_279.html). I am unsure of how you would write a macro to import the paragraph automatically, especially when the weblink would be changing daily, for each product, in a way that I'm not even sure of (i see the month and year timestamp, but im not sure of the pattern, or if there is a pattern, on the "_279".

Method 2:
This is less ideal. CME publishes RSS feeds of the market reports. You can subscribe to certain feeds and it may be possible to bring all the feeds together and publish them to a single email. The problems are that the RSS feed doesn't include the entire market report, only a few words and then a link to the full report, (although in Outlook I can download the HTML attachment) and I have to filter out a lot of reports that I'm not interested in to get to the 'market recaps' for my interest products.

Realistically my programming skills are extremely limited. If any of these options seem plausible to the more experienced I would really appreciate a little bit of help. If there is another solution that would be much easier for me to create I would also really appreciate any advice on that avenue.

baquerd
Jul 2, 2007

by FactsAreUseless
To begin with, VBA is an odd choice unless you're 100% tied to Outlook without any other mail utilities and even then I'd try to parse the info in another language, dump to a file, and then use a simple VBA mailer probably.

Effulgence posted:

Method 1:
Each market report recap has a specific link on the CME webpage (ex: http://www.cmegroup.com/education/market-commentary/energy/2011/08/recap-energy_279.html). I am unsure of how you would write a macro to import the paragraph automatically, especially when the weblink would be changing daily, for each product, in a way that I'm not even sure of (i see the month and year timestamp, but im not sure of the pattern, or if there is a pattern, on the "_279".

You would need to find a DOM (or SAX) parser library and use it. If you can determine how they generate page URLS, you can use those directly. Otherwise, perhaps try to find a location on their site that has links that can be parsed. Generally, your second method is preferred.

quote:

Method 2:
This is less ideal. CME publishes RSS feeds of the market reports. You can subscribe to certain feeds and it may be possible to bring all the feeds together and publish them to a single email. The problems are that the RSS feed doesn't include the entire market report, only a few words and then a link to the full report, (although in Outlook I can download the HTML attachment) and I have to filter out a lot of reports that I'm not interested in to get to the 'market recaps' for my interest products.

This is actually great, you just parse the XML RSS feed for the links and then use a DOM (or SAX) parser to extract your paragraphs. Filtering is super cheap in terms of run time.

This will all be fairly difficult to work out if you've never done anything like it before but hopefully you have some keywords to search for now.

baquerd fucked around with this message at 16:35 on Aug 10, 2011

calcio
May 7, 2007

No Totti No party
I'm looking for the best method to determine which section I am in on a rectangle or square to move towards the middle.

I move pixel by pixel towards the middle and I can query my x,y location and the board's width/height. What I was thinking is splitting the board into eight sections and if I'm in sections 2,3,6,7 I move up or down and if I'm in 1,4,5,8 I move left or right until I get to center.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


calcio posted:

I move pixel by pixel towards the middle and I can query my x,y location and the board's width/height.

You need some information on your position relative to the rectangle. If all you know is your absolute position and the size of the rectangle, you don't even know whether you're inside its boundary.

Opinion Haver
Apr 9, 2007

calcio posted:

I'm looking for the best method to determine which section I am in on a rectangle or square to move towards the middle.

I move pixel by pixel towards the middle and I can query my x,y location and the board's width/height. What I was thinking is splitting the board into eight sections and if I'm in sections 2,3,6,7 I move up or down and if I'm in 1,4,5,8 I move left or right until I get to center.



You can actually figure out whether you need to move horizontally or vertically by comparing |x| and |y| (where x and y are relative to the middle of the square), and then check the sign to figure out which direction to move.

calcio
May 7, 2007

No Totti No party

ultrafilter posted:

You need some information on your position relative to the rectangle. If all you know is your absolute position and the size of the rectangle, you don't even know whether you're inside its boundary.
It's 0,0 top left and movement will wrap around.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


calcio posted:

It's 0,0 top left and movement will wrap around.

If the rectangle is height h and width w, the corners are at (0, 0), (w, 0), (0, h) and (w, h). You can view your problem as finding the point (x, y) that minimizes the sum of the squared distances from the corners, which works out to be 2(h^2 + w^2 - 2wx - 2hy + 2(x^2 + y^2)). What it sounds like you want to do is to use a coordinate descent algorithm with a small step size.

shrughes
Oct 11, 2008

(call/cc call/cc)
These solutions all suck because you end up moving in a vertical or horizontal line and then diagonally.

shrughes
Oct 11, 2008

(call/cc call/cc)
Here's one attempt at something cooler looking but afaict it ends up converging to (0,0) like a parabola that's attracted to the diagonal line.

The code here assumes that you're targeting (0,0) and x and y are both non-negative, because like, whatever.

code:
// make v a 2D array of '.' or something
while (x > 0 || y > 0) {
    v[x][y] = 'X';  // draw a pretty picture

    if (x * x / (x + y) != x * (x + 1) / (x + y)) {
        x--;
    } else {
        y--;
    }
}
v[x][y] = 'X';  // v[0][0]

Edit: This one's pretty cool:
code:
while (x > 0 || y > 0) {
    v[x][y] = 'X';

    int t = (x * 773) % (x + y);
    if (x * t / (x + y) != x * (t + 1) / (x + y)) {
        x--;
    } else {
        y--;
    }
}
v[x][y] = 'X'
Edit: The neat thing about these is that the next point you step to is determined only by the point you're currently at. There's no memory of your original position. The first version, if we replaced (x+y) in the loop with whatever the original value of (x+y) was, would be a simple line-drawing algorithm. Instead, since (x+y) changes as a coordinate changes, you're more likely to change the same coordinate you changed in the previous step.

The second version multiplies x by a prime number (mod x+y), which (except for one out of every 773 steps) is relatively prime to (x + y). This means instead of being a line drawing algorithm that goes a little curvy, it's more like something that picks decrementing x with probability x/(x+y).

Edit: Pictures. (0, 0) is in the top left corner.

Two pictures of the first version, from (30, 49) and (49, 49).




And the version that uses 773, starting at (49, 49).

shrughes fucked around with this message at 08:28 on Aug 14, 2011

TasteMyHouse
Dec 21, 2006
I'm having trouble thinking multithreaded. (language happens to be C# but I'm thinking this is mostly conceptual and not language oriented. If I should repost this in the .NET thread then lemme know)

I'm talking to an embedded device running Linux via RS232. The serial port class I'm using maintains its own internal thread that fires a "Data Received" event every time it gets a burst of bytes. I have an event handler that actually retrieves the data from the serial port class, shoves it into a byte[], then grabs a thread from a thread pool and tells it to take care of actually handling the data in a Process() function. So far, so good.

What I want is to be able to check each line as it comes in from the serial port to see if it matches a regex, and if it does then do something in response. The device is giving me data in unpredictably-lengthed bursts of bytes. If I'm regexing on a string that I construct from the previous step, I could miss matches that happen to be split across two different chunks coming down the wire. I can't just concatenate my strings until I see a newline and then check for matches because in the extremely common case of seeing the shell prompt, there will BE NO newline to check for. So what I've gotta do is split the strings on newlines, then check all those strings for matches, then throw all the strings except for the last one into my "processed" Queue, and hang on to the last string I get so I can concat it with the next string I get off the line before repeating that whole process.

The problem here is that this means that I have to check for matches within my Process() function. I want to, in my main thread, just be able to say "WaitFor(some_regex);" and have the main thread block until there's a match. I don't know how to do that. Right now, my WaitFor() function is just spinning until it sees true on a volatile bool that my process sets when it sees a match. This works fine, except that it completely consumes one of my two processor cores. I WANT to have Process() lock on some some "match not yet found" object, and then release the lock when it finds the match, but Process() is running out of a thread pool each time it is called and there might be multiple calls before it finally sees a match. Then I thought I could have another thread lock on an object and then my process thread could tell it to unlock but then I realized I was a moron because then I would just have to have THAT thread spin. What I'm doing right now is just have the main thread sleep 50ms every iteration of its wait loop which seems to keep the cpu usage down but also feels really hacky.

So uh basically I don't know what the hell I'm doing here. Is there any way for me to easily achieve this kind of communication between threads, i.e. have one thread sleep until a particular event? Or is there a superior way to design this program to obviate this need?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
I think the magic term you're looking for is "condition variable". In C#, this is apparently available as methods on Monitor.

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!
I believe this site has been previously recommended for all your .NET threading info needs.

Graviton v2
Mar 2, 2007

by angerbeet
Mutex?

TasteMyHouse
Dec 21, 2006

rjmccall posted:

I think the magic term you're looking for is "condition variable". In C#, this is apparently available as methods on Monitor.

hmm.... I don't think Monitor can do I what I need it to do, but I will read what I can on "condition variables" in general to see if I can figure out how to apply the concepts myself

Graviton v2 posted:

Mutex?

if you're talking about just the concept of mutexes, that's what I wanna do but I just don't know how to apply it, considering that the thread that's generating the condition is transient -- the function that will be finding the regex match is spawning over and over again from a thread pool so it can't just hold onto an object until it finds it.

If you mean the .NET class Mutex then I'd need more explanation.



Jethro posted:

I believe this site has been previously recommended for all your .NET threading info needs.

I will read through that, thanks.

haveblue
Aug 15, 2005



Toilet Rascal

rjmccall posted:

I think the magic term you're looking for is "condition variable". In C#, this is apparently available as methods on Monitor.

In other languages/environments this can be called a semaphore.

ToxicFrog
Apr 26, 2008


haveblue posted:

In other languages/environments this can be called a semaphore.

Semaphores are different (perhaps you're thinking of monitors, which are closely related and sometimes used synonymously?) The main distinction is that posting a semaphore will always increment it, and waiting it will block only if has a value <= 0; in contrast, signaling or broadcasting a CV is a no-op if no-one is waiting on it, and waiting will always block.

That said, there's a lot of situations where semaphores and condition variables can be used pretty much interchangeably, although often one or the other will result in clearer code or greater efficiency.

ToxicFrog fucked around with this message at 16:22 on Aug 18, 2011

PDP-1
Oct 12, 2004

It's a beautiful day in the neighborhood.
Is there any reason you have to block the main thread at all? If not you could have a callback method and use WaitFor(some_regex, someCallbackDelegate) to begin waiting for a result and someCallbackDelegate to handle that result when it comes in. The event handler for the SerialPort DataReceived would do all your checks and return a valid result through the callback when one was found.

Alternately, if you are able to get away with not-yet-standard code you could check out the new Async CTP. It is made to do just this kind of thing with a cleaner code structure than callbacks provide. I believe the CTP is at a point now where you can use it in production code so long as it isn't for medical devices or other super-critical applications.

Adbot
ADBOT LOVES YOU

TasteMyHouse
Dec 21, 2006

PDP-1 posted:

Is there any reason you have to block the main thread at all?

Well, maybe I just don't know what I'm doing with multithreading, but what I want is for the main thread to pause execution until a condition is true, and it seemed the ways to do that are either spinning, blocking, or some combination. I really just want the main thread to completely halt and consume no resources until the condition is true, however it can be accomplished.

quote:

If not you could have a callback method and use WaitFor(some_regex, someCallbackDelegate) to begin waiting for a result and someCallbackDelegate to handle that result when it comes in. The event handler for the SerialPort DataReceived would do all your checks and return a valid result through the callback when one was found.

I don't quite get what you're saying here. The main thread is calling WaitFor(). How is it waiting for a result? How does it get notified?


quote:

Alternately, if you are able to get away with not-yet-standard code you could check out the new Async CTP. It is made to do just this kind of thing with a cleaner code structure than callbacks provide. I believe the CTP is at a point now where you can use it in production code so long as it isn't for medical devices or other super-critical applications.

hm. I doubt this would work because of the execution environment: an absurd internal tool written in VB.net whose source I have no access to that executes C# scripts via reflection. I've been using ILMerge to patch assemblies into the executable so that the scripting engine will be aware of them... maybe I could do that with this, I'll check it out.

TasteMyHouse fucked around with this message at 15:07 on Aug 18, 2011

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply