Coding: Fleeting Thoughts

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

Moderators: phlip, Moderators General, Prelates

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Tue Jun 26, 2012 10:51 pm UTC

Dr. Willpower wrote:I'm finding this dynamic typing stuff to be very liberating. Kind of like living with your mother for years and then finally moving out. Of course, someone has to do all the housework and it will still probably be me...

You could hire a maid...

User avatar
Xanthir
My HERO!!!
Posts: 5413
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Jun 27, 2012 5:14 pm UTC

My intuition came pretty easy - I first leaned really hard into Functors and developed my intuition about that as much as possible. Once I understood Functors pretty well in the context sense, the basic concept of Monads is trivial - you can see them as just Functors with 'join' added, so that rather than just mapping pure functions over the data in the context, you can map functions that return a value in the same context.

That gave me an in, making it easy to understand several of the basic monads like Maybe, List, and Writer. I'm still working on building out my intuition more generally now - I just grokked State yesterday. Today I'm trying to grok Reader fully - I know how to *use* it, but it's not yet intuitive. I highly suspect this is because the Reader api has been specialized away from the standard monadic structure for some sort of usability purpose.

This is all building to me having a real and proper intuition of the theoretical structure of monads.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Coding: Fleeting Thoughts

Postby b.i.o » Thu Jun 28, 2012 11:18 am UTC

I've just spent the last couple of days working on implementing + integrating a reader transformer into one of my projects at work. I understand it now, but it took me a couple of days of knocking my head against it to grok it fully.

I feel like part of the problem with monad/monad-related tutorials is that they're all written in Haskell. Haskell is not a language that's easy to read if you don't program in it frequently. (And almost all of the tutorials add another layer of indirection by using do notation, which makes the problem worse.) I started making much better progress after rereading the first half of a Haskell book.

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

Re: Coding: Fleeting Thoughts

Postby elminster » Fri Jun 29, 2012 9:03 pm UTC

Sometimes it just seems easier to code your own tools than it is to find them or to get broken things working. Recently wanted to decompile some flash files, all the popular decompilers crashed (It was possibly edited to do that deliberately) or said error... yet the files worked when used normally.
I read through the file specification, but really it's quite long to do as a "little thing". Maybe I should just make it open source (there is an opensource flash decompiler, but doesn't actually work all that well either).
Image

User avatar
tastelikecoke
Posts: 1208
Joined: Mon Feb 01, 2010 7:58 am UTC
Location: Antipode of Brazil
Contact:

Re: Coding: Fleeting Thoughts

Postby tastelikecoke » Sat Jun 30, 2012 2:50 pm UTC

TIL: MIPS Assembly.
...
can anyone give me a simple way to make a function in assembly?

User avatar
Yakk
Poster with most posts but no title.
Posts: 11128
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Sat Jun 30, 2012 4:27 pm UTC

tastelikecoke wrote:TIL: MIPS Assembly.
...
can anyone give me a simple way to make a function in assembly?

http://en.wikibooks.org/wiki/MIPS_Assembly/Subroutines
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Sat Jun 30, 2012 6:53 pm UTC

elminster wrote:Sometimes Every god damned time it just seems easier to code your own tools than it is to find them or to get broken things working.

FTFY. Although I think that's just me, so FTFMyself.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Jul 03, 2012 6:09 pm UTC

Apparently the += operator works fine on null strings in C# (it treats them as an empty string). This bothers me for some reason.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6580
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 03, 2012 7:47 pm UTC

It's not just "+=", "s = x + y" is the same way. It bothers me because I have a strong SQL Server background, and I believe that an empty string is not null. Also, I feel that in most cases if you actually have a null string you probably want to treat it differently, and if there is no exception, it might result in a hard to track down bug.

Also, on top of everything:

Code: Select all

string s = null;
string t = null;
Console.WriteLine(t == null); //Prints True
Console.WriteLine((t + s) == null); //Prints False
Summum ius, summa iniuria.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Tue Jul 03, 2012 11:23 pm UTC

The more I'm converting from PHP/MySQL to Node/MongoDB, the more I realize that I don't want to go back. It's saner in every way.

It's also nice how the transition basically made Node sites immune to practically all the security issues that aren't developer-caused (SQL injection, path traversal, etc).
In Minecraft, I use the username Rirez.

User avatar
headprogrammingczar
Posts: 3072
Joined: Mon Oct 22, 2007 5:28 pm UTC
Location: Beaming you up

Re: Coding: Fleeting Thoughts

Postby headprogrammingczar » Wed Jul 04, 2012 12:07 am UTC

I would say both of those are developer-caused.
<quintopia> You're not crazy. you're the goddamn headprogrammingspock!
<Weeks> You're the goddamn headprogrammingspock!
<Cheese> I love you

User avatar
Aaeriele
Posts: 2127
Joined: Tue Feb 23, 2010 3:30 am UTC
Location: San Francisco, CA

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Wed Jul 04, 2012 1:34 am UTC

headprogrammingczar wrote:I would say both of those are developer-caused.

^
Vaniver wrote:Harvard is a hedge fund that runs the most prestigious dating agency in the world, and incidentally employs famous scientists to do research.

afuzzyduck wrote:ITS MEANT TO BE FLUTTERSHY BUT I JUST SEE AAERIELE! CURSE YOU FORA!

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Wed Jul 04, 2012 1:45 am UTC

Probably bad wording on my part; by "developer-caused" I mean issues that developers made themselves, like not checking for XSRF tokens. SQL injections and path traversals tend to be because developers don't put in precautions to defend against them, while just getting rid of apache and (My)SQL removes the threats entirely.

The distinction matters for me because "developer-caused" ones are things I have people consciously build against (i.e. build mechanisms to avoid them), while SQL injections and the like are things people have to simply remember about (i.e. remember to use PDO or whatnot, instead of stringing strings).
In Minecraft, I use the username Rirez.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Wed Jul 04, 2012 1:51 am UTC

Steax wrote:They're because developers don't put in precautions to defend against them. Just getting rid of apache and (My)SQL removes the threats entirely.

Developers shouldn't have to put precautions in. If the framework simply differentiated between escaped and unescaped strings, there would be no threat of sql injection attacks. Obviously, an unescaped string should never be used within a query. Better yet, queries should never be formed on the fly, but I think PHP programmers would throw a hissy if anyone so much as suggested that sql queries could be formed without strings.

User avatar
Steax
SecondTalon's Goon Squad
Posts: 3038
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Coding: Fleeting Thoughts

Postby Steax » Wed Jul 04, 2012 2:07 am UTC

Yeah, the issue is that the old PHP string-concat way of SQL is just too ingrained in, uh, too much of the population.
In Minecraft, I use the username Rirez.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Wed Jul 04, 2012 2:14 am UTC

Thesh wrote:

Code: Select all

string s = null;
string t = null;
Console.WriteLine(t == null); //Prints True
Console.WriteLine((t + s) == null); //Prints False

This is why I don't like C#. It does way more than what you tell it to.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Wed Jul 04, 2012 2:14 am UTC

Steax wrote:Yeah, the issue is that the old PHP string-concat way of SQL is just too ingrained in, uh, too much of the population.

That and it sure is easy to do. Don't need extra files, don't need a combinatorial SQL library, and don't need to specify parameters. Just run a single line of concatenated sql code and you're done. All you'd need to get people to stop doing that is a type imbalance so that they can't concatenate an unescaped string. But this is PHP we're talking about. Like that's every going to happen.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6580
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Wed Jul 04, 2012 4:01 am UTC

sourmìlk wrote:
Thesh wrote:

Code: Select all

string s = null;
string t = null;
Console.WriteLine(t == null); //Prints True
Console.WriteLine((t + s) == null); //Prints False

This is why I don't like C#. It does way more than what you tell it to.


Honestly, there is not a single programming language that doesn't have its annoying little quirks. I do prefer C# over most of the alternatives.
Summum ius, summa iniuria.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Coding: Fleeting Thoughts

Postby Sc4Freak » Wed Jul 04, 2012 4:09 am UTC

Like a lot of things, a lot of C#'s annoying quirks are due to legacy.

Like non-generic containers: (the first version of .NET didn't have generics due to time constraints)

Code: Select all

ArrayList a = new ArrayList;
a.Add(5);
List<int> b = new List<int>;
b.Add(5); // safer, faster

And array covariance: (needed to be compatible with Java)

Code: Select all

string[] kitty = new [] {"meow", "purr"};
object[] arr = kitty; // !?

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Jul 04, 2012 4:43 am UTC

Thesh wrote:Honestly, there is not a single programming language that doesn't have its annoying little quirks.

That's sort of my attitude as well. I write a lot of Python, which is easily my favorite language to date for almost any short, scripty thing (and even some larger tasks). But I can easily list several things that I really really dislike about it.

I feel like even if I were to make my own language, make it just the way I want, etc. and ignore any issues of compatibility or whatever, there would still be things that would annoy me because there'd be choices where option A is better for this scenario but suck for that scenario while option B would be great for this scenario but suck for the other.

Clearly we just need strong AI and a DWIM instruction. :-)

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Wed Jul 04, 2012 7:39 am UTC

Thesh wrote:Honestly, there is not a single programming language that doesn't have its annoying little quirks. I do prefer C# over most of the alternatives.

Certainly. I'm not going to say that C# is an objectively bad language because of that. Some people are totally fine with that. I like control, so I'm really not. That's why I like C++, despite many of its obnoxious syntactic quirks: control. If it didn't involve so much gruntwork, I wouldn't mind writing everything in assembly.

EvanED wrote:Clearly we just need strong AI and a DWIM instruction. :-)

Actually, I'd hate this, for the above reasons.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
Aaeriele
Posts: 2127
Joined: Tue Feb 23, 2010 3:30 am UTC
Location: San Francisco, CA

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Wed Jul 04, 2012 4:44 pm UTC

EvanED wrote:Clearly we just need strong AI and a DWIM instruction. :-)


Yessss....
Vaniver wrote:Harvard is a hedge fund that runs the most prestigious dating agency in the world, and incidentally employs famous scientists to do research.

afuzzyduck wrote:ITS MEANT TO BE FLUTTERSHY BUT I JUST SEE AAERIELE! CURSE YOU FORA!

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Jul 04, 2012 8:27 pm UTC

I'm using a lot of reinterpret_casts. That means I'm doing it right, right? :-)

(I'll post what I'm doing when it's done.)

Edit: awww, two of them went away.

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Wed Jul 04, 2012 10:59 pm UTC

Reinterpret casts are fun. I only have one in my entire game engine though :(

Code: Select all

inline void *BUFFER_OFFSET(GLuint offset)
{
    return reinterpret_cast<void *>(offset);
}
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Wed Jul 04, 2012 11:38 pm UTC

Hey, guess what I just learned: the C++ standard doesn't guarantee that you can cast a function pointer to void*. That's why GCC complains if you try to do it with a static_cast and why you need reinterpret_cast:

Code: Select all

~ : cat > fp.cc
void foo() {}
void * p = static_cast<void*>(&foo);
~ : g++ -fsyntax-only fp.cc
fp.cc:2:35: error: invalid static_cast from type ‘void (*)()’ to type ‘void*’

User avatar
Yakk
Poster with most posts but no title.
Posts: 11128
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Jul 05, 2012 1:08 am UTC

Can you store the function pointer in a (well documented) void(*)() instead of a void(*)? That's a legal round-trip (for function pointers) (for member pointers, I forgot what the best "generic" option is...)
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Jul 05, 2012 1:52 am UTC

Nope. 3rd party API.

Practically speaking though, I can't imagine it wouldn't work in practice to just use void*.

Anyway, what I'm working on is a library that gives you a normal C function pointer that calls a std::function. I use libffi (said 3rd party API) to dynamically allocate a thunk that wraps around the functor (actually it wraps around another function that wraps around the functor) which then becomes your function pointer.

For example:

Code: Select all

std::function<int (int, int)> times = [](int x, int y) { return x*y; };
CCallableClosure<int (int, int)> times2 = times;
int (*c_ptr)(int, int);
c_ptr = times2.get_func_ptr();
std::cout << c_ptr(4,5) << "\n"; // Prints 20, remarkably enough


I put it up on GitHub. It's very rough and only works for binary functions at the moment. E.g. I want to make it so that you could pass that lambda into the constructor directly. (It was a lot of fun to write this for the most part actually, but I'm not looking forward to generalizing the arity -- no variadic templates for me.)

jareds
Posts: 436
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: Coding: Fleeting Thoughts

Postby jareds » Thu Jul 05, 2012 4:03 am UTC

Public Service Announcement

Every time you do something like this, God kills a kitten.

Code: Select all

class A {
public:
    int wish, it, were, an, array;
    int & get(int i) { return *(&wish+i); }
};

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Thu Jul 05, 2012 4:30 am UTC

Does C++ guarantee that wish, it, were, an, and array are stored consecutively in memory?
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11128
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Jul 05, 2012 5:07 am UTC

EvanED wrote:Practically speaking though, I can't imagine it wouldn't work in practice to just use void*.

/shrug, in x86 and x86-64, function pointers and data pointers are the same size. And most compilers support the round trip extension (I don't know of one that doesn't, outside of strict compliance modes).
Anyway, what I'm working on is a library that gives you a normal C function pointer that calls a std::function. I use libffi (said 3rd party API) to dynamically allocate a thunk that wraps around the functor (actually it wraps around another function that wraps around the functor) which then becomes your function pointer.

With lifetime managed by the object that owns the function. Cute.

I might suggest that you should refactor the "call a function on an array of arguments" away from the rest of binder? :)

Is there a cleaner way than naked SFINAE to call a different function for each std::function nary count? Hmm.

Oh, and you probably do not need to use an actual std::function -- forcing that level of redirection when you are trying to bind a lambda seems inelegant.

You'll want to extract the arguments and return value of anything passed in that has an operator(), and store any such type in your CallableClosure. Then you'll avoid an extra indirection in many cases! :)
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

mbrigdan
False Alarm! There's more rum.
Posts: 109
Joined: Wed Jun 25, 2008 2:45 am UTC

Re: Coding: Fleeting Thoughts

Postby mbrigdan » Thu Jul 05, 2012 5:11 am UTC

To the best of my knowledge, nothing like that is guaranteed, because its more an implementation thing. I wouldn't doubt that nearly every sane compiler does the same thing though.

Also, What the hell, PHP?

For those that don't want to bother looking:
Spoiler:
Variable variables. Ie: you can change the name of a variable dynamically. WWWWHHHHYYYY?
Spoiler:
TheNgaiGuy wrote:god is playing a huge trick on us and wants us to use our brains to come to the logical conclusion, even though wrong, that he doesn't exist and will send all atheists to heaven for exercising said gifts and send all theists to hell for having faith.

User avatar
Aaeriele
Posts: 2127
Joined: Tue Feb 23, 2010 3:30 am UTC
Location: San Francisco, CA

Re: Coding: Fleeting Thoughts

Postby Aaeriele » Thu Jul 05, 2012 5:16 am UTC

mbrigdan wrote:Also, What the hell, PHP?

For those that don't want to bother looking:
Spoiler:
Variable variables. Ie: you can change the name of a variable dynamically. WWWWHHHHYYYY?


The "right" use for those is metaprogramming. Unfortunately, most of the actual things people tend to use them for are much less sane.
Vaniver wrote:Harvard is a hedge fund that runs the most prestigious dating agency in the world, and incidentally employs famous scientists to do research.

afuzzyduck wrote:ITS MEANT TO BE FLUTTERSHY BUT I JUST SEE AAERIELE! CURSE YOU FORA!

Ben-oni
Posts: 278
Joined: Mon Sep 26, 2011 4:56 am UTC

Re: Coding: Fleeting Thoughts

Postby Ben-oni » Thu Jul 05, 2012 5:57 am UTC

Yakk wrote:
EvanED wrote:Practically speaking though, I can't imagine it wouldn't work in practice to just use void*.

/shrug, in x86 and x86-64, function pointers and data pointers are the same size. And most compilers support the round trip extension (I don't know of one that doesn't, outside of strict compliance modes).


Supposedly the sizes are different in ARM. At the very least, you should assert that the size of the pointers are the same.

jareds
Posts: 436
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: Coding: Fleeting Thoughts

Postby jareds » Thu Jul 05, 2012 6:08 am UTC

sourmìlk wrote:Does C++ guarantee that wish, it, were, an, and array are stored consecutively in memory?

Pretty much1--that's not the issue. The problem is that it's not legal to use pointer arithmetic to escape the bounds of an array. For the purposes of this rule, anything that's not an array is an array of size 1. Short story: if you want optimizations, compilers need to be able to rearrange things. If anything could be aliasing anything else, that thwarts optimizations; so rules are needed if you want optimizations. For example, consider the following:

Code: Select all

struct { int a[4], b; } x;
...
x.a[i] = 7;
x.b++;
If x.a[ i ] can legally reference x.b, the statements cannot be interleaved in scheduling. However, it in fact cannot, so they can. If i is not in the range of 0 to 3, behavior is undefined.

1 It guarantees they are in that order, so they'll be consecutive unless there's padding, which there's no reason to be for identical types.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Jul 05, 2012 6:53 am UTC

Update: I've Boost-preprocessorized the library, so it now works for arbitraryish arity, where arbitrarish means "up to some preprocessor-defined limit which is currently 10." The process was... better than I imagined but still ugly. I'd like to set up generation of a header version with the Boost PP iteration already applied so that at least it doesn't suck to look at when you're not iterating, but who am I kidding, I haven't even written a build script yet. And also because g++ ... test.cc -E currently takes over 10 seconds to run on my (admittedly low-powered) computer, which is pretty ridiculous.

For some lolz look at this file from Boost's preprocessor library. I'll leave it to your imagination as to how they use that to define a BOOST_PP_SUB macro that subtracts two numbers. I am not sure, but I kind of think it can't be good. :-)

Yakk wrote:
Anyway, what I'm working on is a library that gives you a normal C function pointer that calls a std::function. I use libffi (said 3rd party API) to dynamically allocate a thunk that wraps around the functor (actually it wraps around another function that wraps around the functor) which then becomes your function pointer.

With lifetime managed by the object that owns the function. Cute.

Mildly interesting fact: Actually all of the objects stored as fields in the CCallableClosure need to live that long, including the argument type vector. At least that's what's suggested by, uh, experimental evidence.

I might suggest that you should refactor the "call a function on an array of arguments" away from the rest of binder? :)

I could probably do that, though I'm not exactly clear what you want.

Oh, and you probably do not need to use an actual std::function -- forcing that level of redirection when you are trying to bind a lambda seems inelegant.

You'll want to extract the arguments and return value of anything passed in that has an operator(), and store any such type in your CallableClosure. Then you'll avoid an extra indirection in many cases! :)

I'd like to support more than just std::functions, but I don't think I care enough about the extra indirection to do more than add a new constructor that implicitly converts other callables to a std::function. The assumption that the backing functor is a std::function is fairly ingrained, and in fact I just made it a little moreso, because I sort of pass it around as a handle on the function type. (Is there a way of going from std::function<r (args...)> to r (args...), or would I have to write that or some traits classes? That would make things a bit easier for that conversion.)

It's not like there aren't already several layers interposed -- libffi and the binder each add two calls, so another two calls [what GCC's std::function will give you] from std::function isn't like it's adding a bunch of overhead where there wasn't before. And in fact if you optimize, one of the ones belonging to std::function goes away, which means that there are 5 middlemen on the call stack, only one of which is due to std::function. (Edit: Oh, not to mention, the impression I have (which could totally be wrong) is that libffi's wrappers are rather thick ones -- a lot more than just pass through stuff. It has to walk over the array of argument types while it walks down the stack and puts the address of each argument into the args array.)

Ben-oni wrote:Supposedly the sizes are different in ARM. At the very least, you should assert that the size of the pointers are the same.

Good idea. I'll add that in in a future version.

User avatar
Xanthir
My HERO!!!
Posts: 5413
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Thu Jul 05, 2012 8:05 am UTC

mbrigdan wrote:Also, What the hell, PHP?

For those that don't want to bother looking:
Spoiler:
Variable variables. Ie: you can change the name of a variable dynamically. WWWWHHHHYYYY?

Theoretically, for the same reason you can call a dynamic array index. You don't have to hardcode "arr[1]" in your code, you can just say "arr[$index]".

Really, though, that's what dictionaries are for. PHP is stupid.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

ycc1988
Posts: 28
Joined: Fri Jun 22, 2012 5:13 am UTC

Re: Coding: Fleeting Thoughts

Postby ycc1988 » Thu Jul 05, 2012 8:52 am UTC

I thought a PHP "array" is a dictionary, only that using the default key values gives array-like behavior?

troyp
Posts: 557
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW

Re: Coding: Fleeting Thoughts

Postby troyp » Thu Jul 05, 2012 11:09 am UTC

So I'm kinda late to comment on the goto discussion, but I'm going to anyway because it's a topic that really annoys me.

PM 2Ring wrote:I prefer to use goto. The only time you're likely to see goto in sane code is to implement a multi-level break, and such usage shouldn't be seen as bad coding, IMHO. Sure, it's not as elegant as a named break mechanism, OTOH, it's not actually spaghetti coding, so Dijkstra's dictum against goto doesn't really apply.


I suspect Dijkstra would beg to differ. A multilevel break with goto is no worse than a regular one, but I'm pretty sure Dijkstra frowned on regular breaks, along with continue, multiple return statements, etc. I guess it's true that all this makes the control flow more complicated and harder to analyse, but I don't think it makes code more error prone or harder to read if used sanely.

Let's face it: Dijkstra was a bit of a fanatic.

I think there are plenty of good uses for goto: multilevel breaks are the most common (to the extent that they should probably have their own command to help readability and enforce semantics), but there will always be other cases where a programmer wants a control flow construct that the language doesn't provide: state machines, coroutines, generators, etc. Even just minor variations of common features that may simplify a particular piece of code substantially. Julian's for-else translation is a really nice example. It's much more elegant than the alternatives.

Functional languages may be able to provide these capabilities through macros and/or continuations, but for more imperative languages, goto is probably the most appropriate way extending the capabilities of the language. It's really the "atomic" control flow statement: you can build anything out of it.

Which is why it's so annoying when an imperative (or partly imperative) language deliberately omits it. An imperative language without goto is idiotic (in the sense that it's either created by idiots, or created *for* idiots). It's one of my big gripes with Python (along with things like lack of tail call optimization and proper lambda expressions), although Python has somewhat compensated for the lack with it's powerful generators. It's also one of the reasons I want to learn (more than a little) C++.

I should probably add that I do take the problems associated with goto seriously. I think it's important - regardless of what primitives are available - to keep control flow as simple and easy to follow as you reasonably can. I just don't think the way to accomplish this is to restrict a language to a childishly basic subset of control flow commands in the hope that its users wont be able to get into quite as much trouble, even if it results in (sometimes insanely) increased verbosity and all the difficulties that stem from that. All programmers need to do is stick to builtin control flow constructs where possible and - when they need something different - to keep it simple, localized and well-tested (as opposed to, say, turning basically the *entire program* into single interconnected mass of ad hoc control flow, which is the problem that Dijkstra was originally reacting against)

User avatar
sourmìlk
If I can't complain, can I at least express my fear?
Posts: 6393
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong
Contact:

Re: Coding: Fleeting Thoughts

Postby sourmìlk » Thu Jul 05, 2012 12:46 pm UTC

jareds wrote:
sourmìlk wrote:Does C++ guarantee that wish, it, were, an, and array are stored consecutively in memory?

Pretty much1--that's not the issue. The problem is that it's not legal to use pointer arithmetic to escape the bounds of an array. For the purposes of this rule, anything that's not an array is an array of size 1. Short story: if you want optimizations, compilers need to be able to rearrange things. If anything could be aliasing anything else, that thwarts optimizations; so rules are needed if you want optimizations. For example, consider the following:

Code: Select all

struct { int a[4], b; } x;
...
x.a[i] = 7;
x.b++;
If x.a[ i ] can legally reference x.b, the statements cannot be interleaved in scheduling. However, it in fact cannot, so they can. If i is not in the range of 0 to 3, behavior is undefined.

1 It guarantees they are in that order, so they'll be consecutive unless there's padding, which there's no reason to be for identical types.


I get why it's stupid, I was just curious about how likely it was that the variables would be consecutive in memory.

troyp wrote:It's also one of the reasons I want to learn (more than a little) C++.

With your post, I think you should like C++. One of the philosophies used when designing C++ was "C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly."
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11128
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Jul 05, 2012 3:44 pm UTC

EvanED wrote:
I might suggest that you should refactor the "call a function on an array of arguments" away from the rest of binder? :)

I could probably do that, though I'm not exactly clear what you want.

This signature:

Code: Select all

template<typename Function>
ReturnType<Function>::type CallFunction( void* argv, Function& func );

turns binder into:

Code: Select all

*reinterpret_cast<ReturnType<Function>::type*>(ret) = std::move( CallFunction( args, *static_cast<FunctionType*>(funcptr) );

which extracts the ffi-specific code away from the problem of calling a function, making the iterate template code a bit simpler.

And I think simplifying the code in the iterated stuff is worthwhile.
Oh, and you probably do not need to use an actual std::function -- forcing that level of redirection when you are trying to bind a lambda seems inelegant.

You'll want to extract the arguments and return value of anything passed in that has an operator(), and store any such type in your CallableClosure. Then you'll avoid an extra indirection in many cases! :)

I'd like to support more than just std::functions, but I don't think I care enough about the extra indirection to do more than add a new constructor that implicitly converts other callables to a std::function. The assumption that the backing functor is a std::function is fairly ingrained, and in fact I just made it a little moreso, because I sort of pass it around as a handle on the function type. (Is there a way of going from std::function<r (args...)> to r (args...), or would I have to write that or some traits classes? That would make things a bit easier for that conversion.)

Why not pass around a pointer to ... some unknown type that has an operator()? So long as you can write templates to extract the arguments that is.

Your CCallableClosure would end up having a pointer to a "type-erased" (I think that is the right term?) holder of the actual unknown type, but that isn't tricky.

I'm mentioning it, because I'm poking around with doing exactly this. :)
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests