Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Vector valued functions

Name: Anonymous 2014-06-12 23:50

Fuck lists and their overhead. I just want an extension to C that lets me return vectors. Not classes, not structs, and nothing involving preprocessor shit either. Something like:
<int, int> divr(int a, int b){
return < a / b, a % b >;
}
int main(int asdfasdf, char ** qwerqwe){
int touhous = 5, men = 12;
int eachSucked, unsucked;
<eachSucked, unsucked> = divr(men, touhous);
printf("Each of the %d touhous sucked %d cocks, and there were %d left unsucked", touhous, eachSucked, unsucked);
return 0;
}

which would compile to something simple, like:divr:
pop eax
pop ebx
push edx
xor edx, edx
idiv ebx
mov ebx, edx
pop edx
push ebx
push eax
ret
main:
touhous dd 5
men dd 12
eachSucked dd ?
unsucked dd ?
push touhous
push men
jmp divr
pop [eachSucked]
pop [unsucked]
;; blah blah blah, printf goes here

Isn't that much more elegant that a nasty list every time where things have to be inserted or initialized? Oh, and if you don't need a return value, <a,b,NULL, c> will compile the unneeded return value out, or at least discard the result.

Name: Anonymous 2014-06-13 0:01

ts, and nothing involving preprocessor shit either. Something like:
<int, int> divr(int a, int b){
return < a / b, a % b >;
}
int main(int asdfasdf, char ** qwerqwe){
int virgins = over 9000, women = 3;
int eachVirgin, unvirgin;
<eachVirgin, unvirgin> = divr(virgins, women);
printf("Each of the %d women had boyfriends %d and the virgins %d were left eachVirgin", women, eachVirgin, unvirgin);
return 0;
}
which would compile to something simple, like:divr:
pop eax
pop ebx
push edx
xor edx, edx
idiv ebx
mov ebx, edx
pop edx
push ebx
push eax
ret
main:
women dd 3
virgins dd 9001
eachVirgin dd ?
unvirgin dd ?
push women
push virgins
jmp divr
pop [eachVirgin]
pop [unvirgin]
;; blah blah blah, printf goes here
Isn't that much more elegant that a nasty list every time where things have to be inserted or initialized? Oh, and if you don't need a return value, <a,b,NULL, c> will compile the unneeded return value out, or at least discard the result.

Name: Anonymous 2014-06-13 0:02

woops

Name: Anonymous 2014-06-13 4:14

Why not just return an array?

Name: Anonymous 2014-06-13 4:20

#include <stdio.h>

int divr(int a, int b, int *rem) {
if (rem) *rem = a % b;
return a / b;
}

int main(int argc, const char *argv[]) {
int touhous = 5, men = 12, unsucked = 0;
int eachSucked = divr(men, touhous, &unsucked);
printf("Each of the %d touhous sucked %d cocks, leaving %d unsucked\n",
touhous, eachSucked, unsucked);
return 0;
}


So what's the problem?

Name: Anonymous 2014-06-13 4:33

>>1

I've implemented vector valued return in Symta, unoptimized, but as far as I can see it would still require copying between stack-frames, unless the return vector's size is know statially. In that case we can preallocate memory at call site's stack frame. Anyway, I'm using vectors for lists, because they are so much more efficient than cons-lists.

Name: Anonymous 2014-06-13 4:38

u mena tuple?

Name: Anonymous 2014-06-13 8:49

>>5
Destructive side effects are the problem. Your divr does something besides returning a value!

Name: Anonymous 2014-06-13 8:57

>>8
I dissagree, this works like prolog's way of doing this

Name: Anonymous 2014-06-13 9:01

>>9
So what?

Name: Anonymous 2014-06-13 9:16

>>10
DECLERATIVE EMULATION!

Name: Anonymous 2014-06-13 10:36

>>8
That would be bad only if computers were magical math machines operating on infinite memory. In reality they have finite memory and destructive updates are sometimes necessary.

Name: Anonymous 2014-06-13 10:48

>>5
Disgusting.

Name: What is Your Favorite Food? 2014-06-13 11:13

>>13
Are you disgusted by the impure, destructive update in divr?

Name: Anonymous 2014-06-13 11:23

>>8
Okay, in academia, you would be right. That is not theoretically the right way to express multiple return values, because side effects. In the industry, we actually run our code, and that means it has side effects.

Name: What is Your Favorite Food? 2014-06-13 11:30

>>15
Purity is an obvious advantage in industry. It makes the code more modular and easier to reason about. On the other hand, destructive updates are "spooky action at a distance" and thus a source of bugs.

Name: Anonymous 2014-06-13 11:33

>>16
Sure buddy, let's see you write a program that prints "hello world" while not having any side effects. You go ahead and do that.

Name: Anonymous 2014-06-13 11:44

>>17
If black-and-white thinking is what thrives "in industry", then it's a shitty industry. No one is talking about total elimination of side effects, only about their clean, explicit isolation. If a function is performing side effects, fine - just declare it. If a function is performing only a transformation of some input value to a result value, then it should do just that, without being entangled with the whole program state. Because if having a 100 functions each of which could modify the program state is worse than having 10 of them and 90 functions which do their job without touching state.

Name: Anonymous 2014-06-13 11:45

>>17

does anything have side effects outside of our universe? (considering we live in multiverse)

Name: Anonymous 2014-06-13 12:03

>>19
You take your jewish science back to the reddits. Thank you.

Name: Anonymous 2014-06-13 12:06

>>18
GCC has function annotations for that.

Name: Anonymous 2014-06-13 12:56

>>20
Take your goyish ignorance back to russia. Thank you.

Name: Anonymous 2014-06-13 14:17

>>20
Considering there are no universes but this one.

Name: Anonymous 2014-06-13 15:57

>>4
Typing. What if I wanted to return <int, float, char *>? The best I could do is
enum type{INT, FLOAT, CHARP};
struct __ret{void * data; enum type thisType};
struct __ret * funct(...){
int a = 1402624235; float b = 2.76; char c[] = "Lolies[i]![/i]";
struct __ret * ret = (struct __ret*) malloc(3 * sizeof(struct __ret));
ret[0].data = &a; ret[1].data = &a; ret[2].data = &a;
ret[0].data = INT; ret[0].data = FLOAT; ret[0].data = CHARP;
return 0;
}

Does that look good to you? Might as well just use an untyped list.

>>5
No, no, NO. None of that `put output in one of the parameters' shit. The only valid case for shit like that is maybe something like scanf(), but I suspect better solutions to that as well.

>>6
Well, I made this post with static (or at least deterministic at compile-time) sizes for the return vectors.

Name: >>1,24 2014-06-13 15:58

>>24
where ret[0].data = INT; ret[0].data = FLOAT; ret[0].data = CHARP; is actually ret[0].thisType = INT; ret[1].thisType= FLOAT; ret[2].thisType = CHARP; , of course.

Name: Anonymous 2014-06-13 16:16

>>24
What you want is a tuple. Try just about any other language.

Name: Anonymous 2014-06-13 16:26

>>26
No, what he wants is an unboxed tuple.

Name: Anonymous 2014-06-13 16:38

>>26
But I don't want any other language; I want C with just one tiny addition that may or may not require massive changes to optimization, machine code generation, and calling conventions.

Name: Anonymous 2014-06-13 17:05

>>28
Just use Rust already.

Name: Anonymous 2014-06-13 17:09

>>29
Rust a shit

Name: Anonymous 2014-06-13 17:11

>>30
How do I rust a shit?

Name: Anonymous 2014-06-13 17:12

Here's a good introduction to Rust:
http://rustbyexample.com/

Name: Anonymous 2014-06-13 17:33

tuple<int, int> divr( int a, int b ) {
return make_tuple( a/b, a%b );
}


It's sepples.

Name: Anonymous 2014-06-13 21:48

>>1
Why are you not using C++?

Name: Anonymous 2014-06-13 23:51

>>33
That doesn't change the second part, where >>1 has <eachSucked, unsucked> = divr(men, touhous);.

Name: Anonymous 2014-06-14 16:53

>>24
Why aren't you using a tagged union?

Name: Anonymous 2014-06-14 17:15

Write it in MATLAB and use Coder to convert to C.

Name: lamdba4ultimate 2014 2014-06-14 17:16

>>36
because when you put pin the tag through the union it ruptures the cells causing LF to be produced which will sting the eyes and this cathode raygun monitor from 1982 is already causing me enough eyestrain thankyouverymuch

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List