/dpt/ - Daily Programming Thread

Previous: What are you working on, Sup Forums?

Other urls found in this thread:

github.com/v8/v8/wiki/Design Elements#dynamic-machine-code-generation
news.ycombinator.com/item?id=13006555
twitter.com/SFWRedditImages

first for lisp

second for this

>linking xkcd unironically

cringe

Good code is readable code, which is to say code that is easily understandable, even for novices. The situations where you need complex and tightly written code that should be written and maintained by true experts are very rare these days.

/codereview/
Problem statement - Give an integer n, write a function that returns 1 if n is even, and 0 if n is odd
int returnsOne() {
return 1;
}

int returnsZero() {
return 0;
}

int isEven(int n) {
int *(*fptr[2])();
fptr[0] = &returnsOne;
fptr[1] = &returnsZero;
return fptr[n&1]();
}

ok

int is_even(int n)
{
char buf[100];
sprintf(buf, "%d", n);
int len = strlen(buf);
if (buf[len - 1] == '0' || buf[len - 1] == '2' ||
buf[len - 1] == '4' ||
buf[len - 1] == '6' ||
buf[len - 1] == '8')
return 1;
else
return 0;
}

most interesting solution i've seen so far tbqh

print ('even' if (-1)**n==1 else 'odd')

Reposting from last thread.


class A
{
public:

static void (A::*whatever)();
};

I need to figure out how to assign and call whatever.

I know how to do it when it's non-static.

class A
{
public:

void ok(){};
void (A::*whatever)();
};

int main()
{
A a;
a.whatever=&A::ok;
(a.*a.whatever)();

return 0;
}

But I can't figure out the static version.

What is even the point of closures?
Why would you willingly mix data and code like this?

>here's a function that returns a customized function that returns what you want (probably)
WHYY

A::whatever();

That's a pretty good trick user.

It's been a while since I've programmed in C and the function pointer declaration syntax looks excessively ugly.

won't compile.

class A {
public:
static void whatever() { };
};

int main() {
A::whatever();
}

Because you can then change the clojured function by simply changing the data.

ok

Probably because you have such an odd declaration for your static variables. Just use static void whatever();

What do you mean by this?

whatever needs to be a pointer to a method.

a=a=>+!(1&a)

int isEven(int n) {
if (n % 2 == 0) return 1
return 0
}

Javascript is the best language ever

This is homework all about pointers to methods. This is the last problem but nothing I try works.

nope

its creative

Trying to learn some F#

let evenodd x = if x % 2 = 0 then printfn "Even" else printfn "Odd"

No branches, therefore no branch prediction miss. The only operator used is a single &.

EDIT:
I actually really like the syntax, but its browser only, so I'd also learn different languages like C

>The situations where you need complex and tightly written code that should be written and maintained by true experts are very rare these days.
So basically you're saying everyone should write crappy code and experts should have to struggle with the poor architecture of novices rather than have novices learn and adapt to experts?

Do you have employment somewhere? I need to protect my stocks.

You need to implement whatever as global variable outside class A.

class A
{
public:
static void (A::*whatever)();
};

static void (A::*whatever)();

int main()
{
A::whatever = &someFun;
}


I hope i didn't mess up the function pointer syntax.

printfn (if x < 2 = 0 then "Even" else "Odd")

node is the future man
it has better performance than other scripting languages

Complex code can work perfectly fine, but adequate comments and inline functions make readability 100x better

The point is that you are writing code for your audience, not the compiler. Your audience is your team members, and if they can't read the code, two things are true:
>You have written bad code.
>You are on the wrong team.

neat

%

int is_even(int n)
{
char buf[100];
int i = 0;
if (n >= 0) goto E;
n = -n;
E: ;
if (n == 0) goto LABEL0;
L1: if (n == 0) goto E1;
i[buf] = (n%10) + '0';
n/=10;
i++;
goto L1;
E1: ;
buf[i] = '\0';
int len = i;
i = 0;
int j = len-1;
L2: if (i >= j) goto E2;
int temp = buf[j];
j[buf] = i[buf];
i[buf] = temp;
i++;
j--;
goto L2;
E2: ;
if ((len-1)[buf] == '0') goto LABEL0;
if ((len-1)[buf] == '1') goto LABEL1;
if ((len-1)[buf] == '2') goto LABEL2;
if ((len-1)[buf] == '3') goto LABEL3;
if ((len-1)[buf] == '4') goto LABEL4;
if ((len-1)[buf] == '5') goto LABEL5;
if ((len-1)[buf] == '6') goto LABEL6;
if ((len-1)[buf] == '7') goto LABEL7;
if ((len-1)[buf] == '8') goto LABEL8;
if ((len-1)[buf] == '9') goto LABEL9;
LABEL0: return 1;
LABEL1: return 0;
LABEL2: return 1;
LABEL3: return 0;
LABEL4: return 1;
LABEL5: return 0;
LABEL6: return 1;
LABEL7: return 0;
LABEL8: return 1;
LABEL9: return 0;
}

Oh I see. Try static void (A::*)(whatever)() as your declaration.

I thought the compiler, and ultimately the end user, were my audience.

C# equivalent:
void EvenOdd(int x) => WriteLine(x % 2 == 0 ? "Even" : "Odd");

>The point is that you are writing code for your audience, not the compiler.
So we're writing code that's not meant to run anywhere. We're doing this as a masturbatory exercise where we share code in arbitrary choice of language (since it's not supposed to execute anyway) just to show off to our friends.

I know there's coding competitions and such but most programmers wouldn't consider themselves programmers when they do this.

node is awesome for ppl who really like JS, but i will probably stay with C. Is node interpreted or compiled?

Duh, lookup table would have been quicker

p.s. This is how I generally view people who praise functional programming languages.

Because they're generally crap even if the concept is sound.

In order to work as a team, you have to be able to understand what is going on, and to tell if something is wrong with high accuracy. You have to tailor to your lowest common denominator

Is this troll?

>JS
>is it compiled

You thought wrong.

Even if you're working completely alone, YOU are the audience, because you must be able to come back to it six months later and understand the code well enough to maintain it efficiently.

Modern languages allow you write code that performs to a practical standard, while also being readable by humans.

The idea is that adding complex design patterns and sophisticated decoupling is absolutely unnecessary for some projects, particularly when you're working with a team of non-experts.

Documented version
int is_even(int n)
{
char buf[100]; // Buffer for string representation for int
int i = 0;
if (n >= 0) goto E;
n = -n; // Handle negative ints
E: ;
if (n == 0) goto LABEL0; // Handle zero explicitly
/* BEGIN: Convert int to one string character at a time*/
L1: if (n == 0) goto E1;
i[buf] = (n%10) + '0'; // A[i] = i[A] = *(A+i)
n/=10;
i++;
goto L1;
E1: ;
/* END */
buf[i] = '\0'; // Null terminate
/* BEGIN: Reverse string so most significant digit is at 0-th index*/
int len = i;
i = 0;
int j = len-1;
L2: if (i >= j) goto E2;
int temp = buf[j];
j[buf] = i[buf];
i[buf] = temp;
i++;
j--;
goto L2;
E2: ;
/* END */
/* Check the least significant digit */
if ((len-1)[buf] == '0') goto LABEL0;
if ((len-1)[buf] == '1') goto LABEL1;
if ((len-1)[buf] == '2') goto LABEL2;
if ((len-1)[buf] == '3') goto LABEL3;
if ((len-1)[buf] == '4') goto LABEL4;
if ((len-1)[buf] == '5') goto LABEL5;
if ((len-1)[buf] == '6') goto LABEL6;
if ((len-1)[buf] == '7') goto LABEL7;
if ((len-1)[buf] == '8') goto LABEL8;
if ((len-1)[buf] == '9') goto LABEL9;
LABEL0: return 1;
LABEL1: return 0;
LABEL2: return 1;
LABEL3: return 0;
LABEL4: return 1;
LABEL5: return 0;
LABEL6: return 1;
LABEL7: return 0;
LABEL8: return 1;
LABEL9: return 0;
}

I was talking about node.js not js.

look no variables
a=a=>+!(!!a&a)

>Is node interpreted or compiled?

Sort of both?

github.com/v8/v8/wiki/Design Elements#dynamic-machine-code-generation

Holy shit

...

that's some GNU project quality code right there

>A[i] = i[A] = *(A+i)
kek does this actually work?

You realize you can compile literally any language? The question is not "can you compile it" but "does a compiler exist"?

This made me happy before I fail my test.

Yes. A = pointer to first element, so *(A+i) dereferences A + i elements. A[i] is the same as i[A]

But what's the point of maintaining code that doesn't compile or no one executes?

This. If you can't write your own compiler, you should probably quit programming.

I say you have to change the lowest common denominator or get rid of him.
>some projects
What kind?
The kind where you write redundant software that already exist and what you're really aiming to do is re-implement something for the millionth time.

If you're doing that just use or write software that translates what others have done into a format where you can easily extend it. For instance reverse-engineer just the UI for the application you aim to copy and inject your own.
>that's illegal
I don't know. But nobody will be able to tell when you've obfuscated it.

Your job can be automated if you're
>YOU are the audience
So? If you've written it once and you were really crappy at it you can write it again and just retain the inputs and outputs. In fact if you're not gonna do it better the second time around you should probably retire or accept the fact that you can't really comment on how to write good software.
Stupid stupid old man.

A[i] is translated to *(A + i)
so it stands to reason that i[A] would translate to *(i + A) which is identical.

It's just a fun C quirk.

bullshit

The question wasn't "can you compile it" it was "is it interpreted or compiled", dipshit.

>Your job can *easily* be automated if you're
Writing software that doesn't do anything new.

I mean...that makes sense. C is so shady lol.

There also comes a point where making it less readable doesn't make it run faster. Inlining functions can't be that hard, and it shows intent a lot better than a conglomerate of math and binary operations

Your team is your primary audience.

It is only incidental that the compiler must be able to run the code as well.

C gives it to you how it is, you could say other high-level languages were the "shady" ones for hiding things from you.

Man this makes me want to learn C.

Do it. C is basic enough that you can learn it in a week (assuming you know basic programming constructs)

do it, it's actually really easy

Neither compile, unfortunately.

What's the point of learning algorithms like sorting when there are standard libraries for everything?

class A
{
public:
static void (A::*whatever)();
void f(){};
};

void (A::*A::whatever)();

int main()
{
A::whatever = &A::f;
}
there you go

>I say you have to change the lowest common denominator or get rid of him.
Yes, let's pay 20% more salaries for the same end-result.

Few programming projects require experts.

For example, a data integration for a particular API, for a particular web-application, for a particular client, on a particular dataset, that needs to be transformed in a particular way and delivered as a particular file format to a particular email.

Maybe you spend extra time creating big, black binders of inheritance with Haskell libraries tacked on and a sprinkling of complex nested objects and their interfaces, as well as a little memory management hack using a magical binary number to improve performance by 6%...

At this point, you've perfectly met the projects needs. Great!

However, no one else at the company knows meme languages like Haskell, nor did you comment the magic bit-level hack, and so now you're going to get hit by a bus and no one can maintain that shit.

Maybe what you've created was elegant, fast, and exceeded expectations. It doesn't matter if no one else on YOUR CURRENT TEAM can fix it.

In another environment, with a higher-paid team, that same code could be considered good, but not with a particular audience.

Well you obviously have no idea how to make things go fast. Compilers trivially inline functions nowdays and it's very rare that they make a mistake. It's not where you put any of your focus. For some general clues to how true this is. There's no __forceinline (MSVC specifier to force the function to be inlined where called in code, bar places where you're calling a pointer to the function) equivalent in the C++ standard. It's because it's generally a stupid idea.

You're so far away from actually knowing what you're talking about you can't even see the subject.

Algorithmic optimization (generally what you do when you consider big-O notations) is trivial enough that anyone can do it. But it's not enough to deliver decent performance. Not for anything non-trivial. Which again, if you're just making trivial things I do genuinely recommend you just do it in HTML/JS. Users seem to prefer web-interfaces and if you do an OK job they can be snappy.

Knowing when to use those algorithms and what is suitable for your problem comes from learning what the algorithms do themselves.

No. Runnable, usable software is primary goal.
The team and code maintainability is incidental to keeping up the primary goal.

What if you need more than what the standard library offers?
Imagine if you had a metric toolbox and you come across imperial bolts.
What now?
You can't build your own tools because you're just a CRUD codemonkey pleb.

...

> What's the point of learning to add up when calculators exist?
> What's the point of ovens when microwaves exist?
> What's the point of parents when children exist?

There's no good reasons to learn algorithms (and datastructures) in general.

Understanding the way algorithms (and datastructures) are invented is far more useful.

Knowing the names of them and looking up a table of their generalized performance numbers is useful though.

But nobody should actually implement their own linked list.

You're misunderstanding.

Runnable, usable software is EASY to obtain. You can get this outcome with both complex and simple code.

However, given a particular team, both complex and simple code are not acceptable, as the complex code is generally unnecessary for runnable, usable software, and only serves to create more technical debt.

Spooky

33 get

>Imagine if you had a metric toolbox and you come across imperial bolts.
I go to the store and fucking buy some imperial fittings like a normal human being.

Who the fuck has an entire metal-casting setup in their back yard? Even if you did, those homebrew tools are not going to be as good of a quality, because there's a massive investment into producing high-quality tools.

0/10, shit analogy.

You have to go back.

The metallurgy foundry analogy doesn't fit here because the act of writing new library code is fundamentally identical to the act of using standard library code.

Why don't you try writing strstr?
You might actually learn something, and you'll learn to appreciate all the hidden abstraction that you take for granted.

How much do I need to know programming to be able to find a job in it? I think I'm decent enough in it, but I don't want to quit my job to go look for one.

Also, how much should I ask for a NY/USA wage?

by usable, i also mean optimised.

it's sometimes prudent to sacrifice short term primary goal in favour of the primary goal in long term

i think we largely agree with each other, but to me you are talking about the difference between complex code with a purpose and OOP retardants for the sake of dogma.

>the act of writing new library code is fundamentally identical to the act of using standard library code
It must be nice to live in a world where time has no meaning. Either that, or your time is completely worthless.

>You might actually learn something, and you'll learn to appreciate all the hidden abstraction that you take for granted.

No, thanks.

Instead, I'll spend that time actually creating something with the massive variety of tools that are available, rather than being proud of a FizzBuzz-tier script that doesn't use any standard library functions.

>unironically making a snarky comment about xkcd on Sup Forums
Like a damn fiddle.

People literally go to coding bootcamp for 6 weeks and land jobs with 120k pay, so not a lot apparently

for u

news.ycombinator.com/item?id=13006555

>mfw crud codemonkey thinks he's a real programmer near me

Impostor syndrome is common among programmers. Most programmers are shit. You can get a job (and keep it) despite being a remarkably poor programmer (even by your standards I bet). Fizzbuzzes are actually used which should give you a hint of the level of entry level positions.

So really you're probably good enough.
>quit your job to go look for a new one
That's not how it works. Just look for a job on your spare time.
>US wages
I'd say google is better than asking here.
Mostly NEETs around here.