/dpt/ - Daily Programming Thread

Previous thread: What are you working on Sup Forums?

Other urls found in this thread:

clbin.com/oEG4m
twitter.com/NSFWRedditVideo

how do languages with multimethods usually implement that anyway? Is it just bouncing off a bunch of function pointers for every type that participates in the dispatch like you'd do manually with the visitor pattern?

fuck opengl

Why doesn't gcc let you use __auto_type as return type for function?

In Julia, type stable code selects the correct method to use at (jit) compile time. If the type of one or more arguments can't be determined in advance, then I do believe it just follows a function pointer.

No, fuck you, pal.

while(1) {
printf("Please don't use an anime picture next time. Thank you!\n");
}

hey plebs,

chad here just popping in to say you will never amount to anything.

>16'b
>end
it's shit

In ANSI C, this is just:

#include
#include
#include

// classes

defclass (Asteroid)
// data members
endclass

defclass (Spaceship)
// data members
endclass

// generics

defgeneric (bool, collide_with, _1, _2);

// multimethods

defmethod (bool, collide_with, Asteroid, Asteroid)
// deal with asteroid hitting asteroid
endmethod

defmethod (bool, collide_with, Asteroid, Spaceship)
// deal with asteroid hitting spaceship
endmethod

defmethod (bool, collide_with, Spaceship, Asteroid)
// deal with spaceship hitting asteroid
endmethod

defmethod (bool, collide_with, Spaceship, Spaceship)
// deal with spaceship hitting spaceship
endmethod

// example of use

int main(void)
{
OBJ a = gnew(Asteroid);
OBJ s = gnew(Spaceship);

printf("=%d\n", collide_with(a,a));
printf("=%d\n", collide_with(a,s));
printf("=%d\n", collide_with(s,a));
printf("=%d\n", collide_with(s,s));

grelease(a);
grelease(b);
}

I'm learning Python and I'm afraid I'm still fucking clueless regarding the entire CS thing. I will be continuing the course I'm on right now but I'm not sure what after. Like I'm fucking clueless. I think I get the basics of programming but if I wanted to program anything that's actually useful I'm lost as it requires not only some specialized knowledge(like working with specific libraries) and generic CS knowledge which I don't have. When I go to any chat or here I simply have no idea what are the people talking about.

That looks nothing like C though

this
>int main(void)

>> Preprocessor macros

kys

Keep working and exposing yourself to other people's code. You'll get there. Branch out, there's no such thing as bad experience, just a lack of experience. You don't have to work fast, it's cheesy but the most important thing is to not give up-- I can't emphasize this enough.

T. T. T. by mathematician/inventor/poet Piet Hein

Put up in a place
where it's easy to see
the cryptic admonishment
T. T. T.

When you feel how depressingly
slowly you climb,
it's well to remember that
Things Take Time!

glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_NOTEQUAL, 0b0011, 0b0001);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// render1 (pass stencil test)
glStencilFunc(GL_NOTEQUAL, 0b0011, 0b0001);
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
// render2 (fail stencil test)
glStencilFunc(GL_EQUAL, 0b0001, 0b0011);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// render3 (pass stencil test)

why the flip does render3 pass? i'm expecting render2 to write 0b0011 thus making render3 fail the stencil test.

are there any viable options excepts webdev? i dont know but it doesn't seem too fascinating too me

>Generics in C
>Using void pointers. Ever.
Leave.

You're right, you should use token pasting macros.

If only C had better macro system.

If only C wasn't 40 years old.

It wouldn't matter if it had proper macro system.

Lisp is like 20 years older in the same sense and those have great macro systems. "C" has had many revisions since then and failed spectacularly every time because its users are resistant to change against their best interests.

To elaborate: its users are deluded into thinking they're writing the same language that was being written in the 70s. Whatever talk you hear about its design decisions being because it's a "system's language" is bullshit, but only more clearly bullshit now that we have better languages like Rust. In reality, the language is vastly different from its former self, and programs written today in "C" are a lot different than they used to be. C today is the way it is because people want it to be "C" instead of a good language.

>users are resistant to change against their best interests.
No the standard committee just sucks.

What resource did you use for learning design patterns, /dpt/?

If only C++ didn't ruin everything for all involved.

But at least all the rest of the languages got to learn from its mistakes.

Same guy from the last thread about the proxy.
Can anyone explain to me what this means? (pic related)
the proxy loads up everything required up to a request like that, and then it just finishes sending data (even though the browser is still waiting for more)

None

Trial and error, common sense, and reading other people's code, mostly.

Back when I took CS, it was still all about algorithms.

help me with python /dpt/
I have a file with something like this

a 1
a 2
a 3
b 4
b 5
b 6
...


how do I read this file and write in a dictionary? I need something like this

d = {
'a': [1, 2, 3],
'b': [3, 4, 5, 6],
...
}

H-hah..! Y-you can't even write something l-like this..? Go b-back to Think Python..!! B-but... if you want... I'll write it for you...! Because I'm j-just that generous!!!
>>> d = {}
>>> for line in open('file.txt').readlines():
... k, v = line.split()
... d.setdefault(k, []).append(v)
...
>>> d
{'a': ['1', '2', '3'], 'b': ['4', '5', '6']}
Don't get any weird ideas!

theres an easier way
d={'a': ['1', '2', '3'], 'b': ['4', '5', '6']}

Collide_With(a and (a or s))
Collide_With(s and (a or s))

its all the same result anyways, why do you need the cases to be independent of one another?

Y-you probably think you're funny! But... H-here's a fun little tweak if you really wanted one...
>>> for k, v in (x.split() for x in open('file.txt').readlines()):
... d.setdefault(k, []).append(v)

Is this supposed to be funny?

Kill yourself you fucking pedophile. Sage.

I wrote a CLI hangman program.
The game state is basically just a string representing the word, with the unknown characters replaced with ?s.
I ended up updating the game state by just rebuilding the string every turn. The core of the program's the guess function, which's in the image.
I am new at programming and I had a lot of fun figuring out how to implement my design. I hope building bigger things will be as fun as this was!

clbin.com/oEG4m
Implemented define-machine from SICP chapter 5. Both the concept and my implementation are silly but they work!

For example:
(define-machine gcd-machine ;; more or less from sicp
(registers a b t)
(controller
test-b
(branch (zero? (fetch b)) gcd-done)
(assign t (remainder (fetch a) (fetch b)))
(assign a (fetch b))
(assign b (fetch t))
(goto test-b)
gcd-done
(perform (displayln (fetch a)))))

(define machine
(machine-assign (machine-assign gcd-machine 'a a)
'b b))

(run-machine machine) ; prints 6

I have a bunch of ifs to initialize some vars

like
if (isVAR === undefined)
isVAR = false;


is there a way to write more tidy?

i know that in C, you can do
condition && statement;

>(define machine
> (machine-assign (machine-assign gcd-machine 'a a)
> 'b b))
Meant this, my bad:
(define machine
(machine-assign (machine-assign gcd-machine 'a 30)
'b 96))

>pythonistas

Please recommend me an IDE for C++ (Windows).

Back in school they made us use Eclipse, and it was ok but way too complex. All I need is marking, a compile/run button and debug (step by step execution).

>neet

CLion

What should I be doing in the red line?

Just return this.cards or this.cards.copy()

Atom/sublime/vmcode + PowerShell or vs community

g...guys
Why does Sup Forums keep sending data to their db every 8minutes? It's all logged on my proxy...
hiroshimoot really is selling our data, isn't he

Code Blocks for an actual IDE
VS Code for the IDE feel in a TE

someone help im retarded

input = [[int(elem) for elem in line] line.split() for line in open('input')]


how do i make this work?

Stop trying to do one liners in python, they're never readable.

trying to read each row into a list of lists (of ints)

Writing a tokenizer, how wasteful is it to reallocate on every single new token instead of counting it ahead with a strtok dry run?

struct split *tokenize(char *str, const char *delims)
{
struct split *tk = calloc(1, sizeof(struct split));
char *tok = strtok(str, delims);
while (tok && ++tk->num)
{
tk->token = realloc(tk->token, sizeof(char *) * tk->num);
tk->token[tk->num - 1] = memdup(tok, strlen(tok));
tok = strtok(NULL, delims);
}
return tk;
}

probably like this?
[[int(x) for x in line.split()] for line in open('file.txt').readlines()]

i love you

well you could allocate like n elements each time, wastes bit more memory but reduces calls to realloc.

Really shouldn't be re-allocating every single time. If you need dynamic, continuously-growing array you should be doing something more like what vectors do which is grow them by 1.5x or 2x each time they need to expand. Fixed-sized chunks also work if you have a better idea of the range of sizes you expect to get.

Stop using and talking about shitlangs, /dpt/. There is no merit in them. Go on Quora or some other website if you want to engage in the equivalent of coprophagia.

struct Thing {
virtual void collideWith(Thing& other) = 0;
};

struct Asteroid : Thing {
void collideWith(Thing& other) {
// dynamic_cast to a pointer type returns NULL if the cast fails
// (dynamic_cast to a reference type would throw an exception on failure)
if (Asteroid* asteroid = dynamic_cast(&other)) {
// handle Asteroid-Asteroid collision
} else if (Spaceship* spaceship = dynamic_cast(&other)) {
// handle Asteroid-Spaceship collision
} else {
// default collision handling here
}
}
};

struct Spaceship : Thing {
void collideWith(Thing& other) {
if (Asteroid* asteroid = dynamic_cast(&other)) {
// handle Spaceship-Asteroid collision
} else if (Spaceship* spaceship = dynamic_cast(&other)) {
// handle Spaceship-Spaceship collision
} else {
// default collision handling here
}
}
};


Someone posted this in the last thread but:
how would you make something like this in a data-oriented way?
I'm trying to make a little game using entirely data-oriented programming and I keep running into issues imagining something like this in some data-oriented structure that would work well with stuff like simd or to split off into threads.

the first thing I think about is having a SoA of "GameObject" that is just a position and then it comes with an enum of what type of object it is so that some "for each game object," code would just check it against a big ass case-switch for every type of object in the game?

See

Fuck off nigger.

someone pls help, this seems to be the only thing keeping me back
Pic related makes me think, should I treat some data differently? the fact it is an image does make me think. It isn't because of the image being too big btw, but idk what could be.

-> /dpt/ in a nutshell

C and lua are both garbage

post code

idk man do you program in C? maybe you discard the content after the null byte

>vhdl
>chad
Lol

all of those langs suck

please don't remind me of /prog/
I miss it. I miss him.

I know I should focus on learning one language and becoming proficient in it before trying to learn another one, but at what point could I know that I'm proficient?

when you can write fizzbuzz

Java question: Given a user inputted String, how do I find the index of the first character in that String and then increment a value with each successive character?

Like int index = str.indexOf('[char]');
while (index>= 0)
{
index = str.indexOf('[char]', index+ 1);
}


except with any valid char the user inputted, not a specific one.

when you can see a quick code snipped on /dpt/ or anywhere and solve it without much trouble in a short amount of time.

See

Fuck off nigger.

may be more suited to your programming abilities.

int j = 0;
for(int i = 0; i < str.length(); i++) {
if (str.charAt(i).equals(charToLookFor))
j++;
}

imagine being required to use java by your professor

Does most Unis do Java for introductory courses?

these days most of them seem to be java or python, both of which are extremely poor languages

The vast majority of CS courses are taught in Java, with the occasional place using Python for CS 1

Does most Schools do into The englishes introduction of course?

You're trying to probably trying to print a number as a char. Are you doing something like printf("%c", anInt)

Fuck off.

>The vast majority of CS courses are taught in Java
We're so fucking screwed.

Sadly, yes.
>python
>poor language
It's got a host of flaws (especially performance-wise), but if you've never programmed before, Python's an easy way to become introduced to concepts. It's also useful as shit. The worst part about it's syntax has to be its OOP, but as a language, it's much better than Java's FactoryAbstractViewFactory design.

What language should I be learning instead?

Scheme or ML

> inputted
you meant to say:
>given a user input (string)...

>It's also useful as shit.
How true.

There's literally no reason not to teach Agda as the first language in a CS program.

I really walked into that one, didn't I? I guess this is what too much Python does to you. Time to start learning Haskell.

you can learn imperative second easier than vice-versa.

This might be true if CS actually meant CS and not code monkey training, although I'd still start with a first-order type system and steadily work up to dependent types from there.

Agda's standard library is awkward to use (installation) and it doesn't compile to very efficient code

Excellent language though. Absolutely should be taught in place of garbage like C++.

>start with a first-order type system and steadily work up to dependent types from there
Yeah, that sounds like a good approach, as long as you ban any escape hatches like general recursion.

Will Python and Java be remembered as the COBOL's of our era?

Waterloo uses Scheme and C first year.

Is it true that if you learn music theory, you can write songs just like code?

>The worst part about it's syntax has to be its OOP
you can ignore OOP and just do functional programming in python
so versatile
I'd say it's a little bit higher on the blub scale than C