/dpt/ - Daily Programming Thread

Old thread: What are you working on Sup Forums?

Other urls found in this thread:

lmgtfy.com/?q=getline after cin
youtube.com/watch?v=QaqBRX8aCQA
en.wikipedia.org/wiki/Markov_decision_process#Category_theoretic_interpretation
twitter.com/NSFWRedditGif

thanks
i gotcha, so when you want multiple types, but not every type

watching anime and eating fries.

not motivated at all.

learning haskell and doing keto.

have transcended motivation into pure discipline.

considering building a browser based multiplayer game. JS + websockets seems really easy to work with

Type classes also often come with (implicit) laws about their operations. For instance, Num a is a ring (a, +, *). While Haskell can't actually check (nor can it check any sort of proof) that instances adhere to any laws, it's assumed that any instance will follow the laws and if you don't it's undefined behaviour.

What will code complete teach me if I've read SICP and the pragmatic programmer?

If you imagine the function

id :: forall a. a -> a
(forall a. is implicit)

this works on every type, but it doesn't know anything about the type - because the type could be anything

if you want to know something about the type - e.g. that you can compare its terms for equality - then you need to constrain it to "only things that can be compared for equality"

this is what classes do

is keto a javascript framework

9th for python

Somebody probably has started a """""lightweight""""" JS framework called "keto"

Programming was a mistake.

Trying to crack this hash for a Uni CTF challenge. I've already attempted to break it using Python and a word list to generate a bunch of hashes to compare to, but that didn't find anything

Been programming a few days started doing tic tac toe based on this challenge this website. Piece by piece assembly but I just wrote the worst code probably ever. It does find the win state of the matric including both diaganol.

It's just garbage code and it's like 60 lines then I go in the comment section of the challene and peoplesaid they made an effective win state checker for tic tac toe in 14 lines. Don't look at code and cry.

What hash did they use?

It's worth noting that type classes are basically an automated version of this:
count :: (a -> a -> Bool) -> a -> [a] -> Int
count (~~) a = f where
f [] = 0
f (x : xs)
| a ~~ x = 1 + f xs
| otherwise = f xs

Which with the class Eq would be:
count :: Eq a => a -> [a] -> Int
count a = f where
f [] = 0
f (x : xs)
| a == x = 1 + f xs
| otherwise = f xs

The tradeoff is that you only get one instance of a particular type class per type.

agree

tic-tac-toe was one of the first programs i wrote...
i still remember when i first showed it to my teacher. he just sat there with a blank face... then burst out laughing. he said it was the worst code he'd ever seen.

i had copy pasted 3 nested ifs for every possible 3-in-a-row in the game

rip

but who's laughing now. i'm still at it

sha256, unless they decided to use some obscure 256-bit hash for this particular challenge

I found a archive of my drive from 4th grade when I started programming. I had a wrapper for the windows `color` command and had a else if for every hex number up to 0xFF to concatenate a string. Holy shit I was dumb

If I use this code
#include
#include
using namespace std;
int main()

{
int x=1, y=2, z=3;
int sum = x+y+z;


cout

Well at least you showed work ethic and a willingness to do this thing called programming.

If you're familiar with Java, Haskell's type classes are vaguely similar to interfaces.

Except, a type class can provide default implementations for functions.

Also, you can define your own type classes then declare existing types to be instances of them (whereas Java interfaces require that you specify which interfaces a class supports when you define the class).

And you can constrain a parametric type to multiple type classes; which is why you have to use the
Num a => a -> a -> a
syntax; you can't just write
Num -> Num -> Num
Because you might want e.g.
(Num a, Ord a) => a -> a -> a
to indicate that a must belong to both classes (because you're going to use operations from both).

you can also do
Num a => Ord a => a -> a -> a

lmgtfy.com/?q=getline after cin

tl;dr: cin leaves a newline and getline doesn't give a shit if it captures only a newline.

check out hashcat

I had a hashcat challenge in a CTF and the trick was that the pattern of the password was limited to a specific regex

I am thinking of writing an ansible module

Look at getline capturing that newline like a boss. Getline doesn't give a shit.

wait it can't be as bad what i have?

I'll show you some snippets kek

for i in range(len(l)):
dwin.append(l[i][i])

x = 2
n = 0
while x >= 0:
dwin2.append(l[x][n])
x -= 1
n += 1


literally it just builds lists of the game board and then compares them lists to p1 = [1, 1, 1] and if they're the same it's a winstate.

Add lazy evaluation to that and you basically have a Haskell program.

wait is that like this -

if p1 == dwin:
print('player 1 wins')
elif p2 == dwin:
print('player 2 wins')

if p1 == dwin2:
print('player 1 wins')
elif p2 == dwin2:
print('player 2 wins')
print(dwin2)


the gist of the file is literally just comparing lists to find a win state.

the last code snippet was just creating the diagnoal win states list.

Don't forget the smug sense of superiority.

that statement's a bit misleading seeing as the sense of superiority is justified

PHP

I'm pretty shit with PHP and mysqli, so bear with me...

say I have sort of a gateway to a table in the database, and I'm writing a function that will insert a row into that table.

what is better - take an array parameter, or each field as a parameter?

and if I'm using an array like so

function insertRow(array $array) {
...
}


how do I ensure that the correct array is passed? (with the correct keys)?

also, sort of related, would I do validations here? or would I rely on the caller to do validations before passing the array to the gateway?

thanks.

PHP is pretty shit.

...

use pdo for mysql interaction

validate here if you are exposing this function to any potential input, don't validate if this function is only used by things you know are valid

>the gist of the file is literally just comparing lists to find a win state.
Sounds exactly like Haskell

well, I'll be taking input, and then packing it all into an array, which I will send to my gateway (to the insertRow function), which then updates the database.

my confusing was if I should validate as soon as I read the input, or if I should validate within the function, the function assuming that anything passed to it is 100% clean

eli says it's the best beginner language
youtube.com/watch?v=QaqBRX8aCQA

Literally who

>try it out
>crashes a display driver
this better be worth it user

en.wikipedia.org/wiki/Markov_decision_process#Category_theoretic_interpretation

Why do category theorists feel the need to throw their useless crap into otherwise decent mathematics?

he has 600 million views on youtube

>600 million youtube views
So he's a nobody

at least it's not Marxist_theoretic_interpretation

eli is like a tech / IT guy, he's not a software developer. of course he's going to say PHP.

>PHP
What the fuck?

please stop bullying eli
he likes php because he has aspergers

I'm looking for some foss projects, games specifically, to contribute to in order to get my feet wet working on bigger projects. I'd like to do it in languages I like though and most of the active and interesting projects I found were C++ which I don't like much. I want to work in Haskell or C or a language that a person who likes Haskell and C would also like. Could you recommend me something?

why would you write a game in haskell
why would you write a game in C
please stop
be a normal human being
use OOP
listen to your government

I to work on a project that involves the internet or sockets in some way. Gimmie some ideas.

Real talk, why do so many people think OOP is integral to game dev?

>use OOP
This is the worst advice you could give anybody.

Try developing non-trivial games. Eventually you'll understand.

Or rather, eventually you'll just get good enough at OOP that you won't need the "it's not me that sucks, it's OOP" defense mechanism.

Not an argument mate.

If it's so great then you must be able to come up with some reasoning why.

This is basically correct. As long as your conception of OOP does not require inheritance.

Maybe if you're making tetris or tictactoe im may not be necessary
If you're doing an fps, you need to keep track each instance of the player. If they're using a weapon abstract class, only a gun subclass should have a reload function, not a grenade subclass.

Maybe you're thinking how it benefits he game, you should think that it benefits the devs to have the same idea in building something and not fucking it up

Fuck off attention whoring nigger

I bet you fags think that "aggregate types == OOP"

>If you're doing an fps, you need to keep track each instance of the player.
>data is object-oriented

>If they're using a weapon abstract class, only a gun subclass should have a reload function, not a grenade subclass.
>object-oriented code is object-oriented

>you should think that it benefits the devs to have the same idea in building something and not fucking it up
>all abstraction and code reuse is object-oriented

Wow. Sure convinced me.

>If they're using a weapon abstract class, only a gun subclass should have a reload function, not a grenade subclass.
Shittiest way to structure it.

Are we going to go down the no true scotsman route? Again?

I'm just shitposting, my guy.

>ur dumb lol
Oh look its Sup Forums pretending to know shit for the sake of arguing again

Are we going to go down the "everything good is OOP" route again?

Listen to Carmack.
He thinks Haskell could be great for games.
Naturally you wouldn't understand why, because you are a low-info human beeing.

I don't know why you think that is a "no true Scotsman" argument.
Aggregate types (i.e. structs, records, whatever) are not exclusive to OOP and existed long before it.

There are a lot of people, especially novices, who don't understand the actual core of OOP and confuse "structs and functions that work on those structs" (a very common way to do procedural programming) to be 'OOP'.

I thought Carmack was into Racket.

He is into many things.
Racket is this thing he is using for script-like things.
He is very fond of Haskell, although I don't think he is doing that as a job currently.

But carmack doesn't give a shit about games.

Not an argument.

Why would you think so?
He is actually still programming game-related things in his free time.

I moved to UK, and I know some PHP
Should I try reach something here with PHP or something other?

Hey, I just like C, Haskell and games and I don't like C++, Java and other similar languages. And in my free time, I'll do things I like, thank you very much.

All I ever wanted was a link to some fun, open source game I could help out with and learn something in the process but I guess I just started an argument.

Any Pajeet knows PHP. Probably better than you do.

Argument to what, pajeet?

Well then kys.

Ok real talk. One of the hardest problems in programming is dealing with mutable state. Don't listen to anyone that tells you otherwise. There are two main approaches for dealing with it. One way is to minimize it -- that is functional programming. The other way is to encapsulate it so you can only interact with it in a certain way using the methods of an object. That is OOP.

Clearly defined input/output problems like compilers and web servers are where functional programming languages shine, because almost all of the mutable state can be eliminated. On the other hand video games are the antithesis of this: you have a great deal of objects on the screen that are 99% the same every frame, and you can't afford to recompute at every time step.

I'll admit the limits of my knowledge here and say I don't know how Haskellers deal with such performance sensitive problems (I would be surprised if they were completely at a loss). I am sure it involves monodic trapezoid endofunctor magmas. What I will say is that it's quite obvious how to deal with complex mutable state in OOP, you just encapsulate shit until it becomes manageable. Ugly? Yes. Good enough for the game industry? Certainly.

Probably.

I'm not a game programmer.

OOP literally encourages shared mutable state.

This.

Hell. Try developing non trivial code at all. OOP is king at abstractions, and abstractions are important for developing good code and not an unreadable mess.

C++ is literally God tier and you hipsters wouldn't understand why

No. We are not.

>I don't know why you think that is a "no true Scotsman" argument.
I don't necessarily, but it does sound like potentially the beginning of a no true Scotsman argument.
No one seems to be able to agree on what oop is anyway. Novices and veterans alike. It all just distils to either
>my mental model of the program is in terms of objects, therefore what i do is object oriented
or
>nothing i like or makes sense can possibly be object orientation because everything oop is axiomatically bad
If you can avoid this, then i'd like to hear what oop means to you.

Pajeets don't even know how to not shit in the street
They're lazy poos in the loos
So anyone is better at PHP than Pajeet

FP isn't about minimizing mutable state per se, but more about minimizing how much code is able to mutate state. In contrast, OOP is more about restricting how state can be mutated.

>lazy poos in the loos
You do not seem to use your brain at all. "Poo in the loo" is an injunction to what they are NOT doing right now. WE are the "poo in the loos" - WE use toilets.

And while we are at it, I was always under the impression that there are enough white Pajeets as well - a.k.a. people who have little idea what they are doing on the computer and just copy+paste code from SO.

OOP is about the segregation of state into small modules of single responsibility.
An object oriented program can be viewed as a network of objects passing messages to each other, indirectly modifying each other's state.

Notes:
Extremely large modularisation of a program (e.g. "Entire graphics subsystem") isn't OOP.
ADTs are not OOP.
The syntactic "subject.verb(noun)" is not OOP.
Inheritance is not core to OOP.

>you have a great deal of objects on the screen that are 99% the same every frame, and you can't afford to recompute at every time step.
That's why you can't afford to use OOP because it leads to terrible performance.
The big killer for performance is how OOP really fights you when it comes to batch processing entities - you do not ever want to write code like:
foreach e in entities:
e.move(); // which does say e.position += e.velocity
e.draw(); // check by itself if it needs to redraw, be sorted, or w/e

i.e let entities 'update themselves' through some abstract method.
What you want to do is structure things more like this:
entities_to_draw = move_entities(entities.positions, entities.velocities); // where positions and velocities are arrays
draw_entities(entities_to_draw);


>What I will say is that it's quite obvious how to deal with complex mutable state in OOP, you just encapsulate shit until it becomes manageable.
The OOP way of dealing with state is really bad because of how inflexible and bad for performance it is, you get these loads of tiny state changes from method to method (x.do_foo(), x.do_bar(), etc) which just destroys easy composability and easy batching.

I have lately been thinking about engine development. The thing is: an engine has to do graphics stuff as well. Doing the graphics stuff during the game logic stuff is ... wrong? I don't know, that's where I was kinda stuck.

But naively I'd say that the program logic should first update all the different elements that need to be updated for the proper game state, before control is giving to the renderer to actually draw things on the screen.

Can anyone confirm or deny this? I have never taken any classes or anything in game development, I am just having thoughts on my own that I'd have liked confirmed and denied.

>is giving
I meant "given". Sorry, I am dead tired right now, and English is also not my native language.

Not a game dev, but I assume those work on different threads. One thread computes all graphics related stuff and another does control stuff. Of course they are asynchronous of each other. Anyone free to correct me if Im wrong

They do, visit any street where pajeets live

>state
>responsibility

Also
>Here is a definition of X. Note: these things that can neatly be thought of as fulfilling the definition of X are not X, because fuck you.

Nope, can't take you seriously either.

Make a chess engine in C. Its fun/hard intro to AI .

The thing is I was thinking that as well. But the problem here would be locking - thread 1 updates the scene right now, while thread 2 computes other stuff. The game state might become inconsistent very quickly, depending on how many object states are calculated in the moment.

I'm not sure I follow. Why would the second block code be any faster than the first?

Sounds about right. I don't really see how else you would do it.

>>state
>>responsibility
I don't get what you mean by these meme arrows.
>>Here is a definition of X. Note: these things that can neatly be thought of as fulfilling the definition of X are not X, because fuck you.
You clearly know nothing, you fucking idiot. Actually learn some theory before you say stupid things.
You OOPfags really try to claim absolutely everything.

Yes, that's right. Otherwise you often get artifacts where objects behave or appear differently depending on how early or late they are processed, not to mention that it's going to be really inefficient because you'll be jumping around and thrashing the cache so much.

Not necessarily, no. And if that does happen, it should be part of a work queue system and not a specific "logic thread" and "render thread" that communicate using message passing or whatever.

Not necessarily OOP, then.

who thought this was a good idea?

Cache.

You're wrong, you can't do those things asynchronously.
The renderer depends on the logic update.