/dpt/ - Daily Programming Thread

Real Programmers Edition #8

Previous thread: What are you working on, Jow Forums?

Attached: carmack.jpg (800x1000, 52K)

Other urls found in this thread:

github.com/IwoHerka/auk
yurichev.com/writings/AL4B-EN.pdf
youtube.com/watch?v=6w4pcPrrG70
twitter.com/AnonBabble

monads

Start using OOP.

Attached: yegor.jpg (400x400, 22K)

no

Attached: spj.jpg (224x223, 31K)

Where do you guys buy your programming socks?

I program barefoot.

Getting ready to port my tic tac toe game written in C with ncurses to Windows. What am I in for?

free as in free

cuck

It should be
return new Start(
new Using(
new OOP(
new Every(
new AnonIterator())))));

Yes, he actually tries to make Lisp out of Java

soiboy

>What are you working on, Jow Forums?

Well, since you ask, I just wrote this: github.com/IwoHerka/auk
It compiles s-expressions into Python functions. I'm converting
them into Python's AST and compiling using exec and compile.

What do you think?

Instances of user-defined types (without Python AST nodes) end-up as free-variables (ast.Name). I generate a name for them and stick into their value into target function's globals. I bet there is a better way. Any suggestions?

started my second c compiler after 5 years, this time it will be more complete, my target is bootstrapping. it's just ~600 sloc and already can compile pic related

Attached: Capture.png (1293x647, 74K)

Reposting from last thread
>Is java "still" worth it for solid, fast cross platform desktop applications written in a reasonable amount of time? Should I go with Qt for my next project? I don't really like C++ at all and there are no bindings for C, but I see some big potential there. Having some existential dread over this with less and less time for side projects.

As in parse, or are you emmiting code already? Pure x86 or LLVM?

this is a parser, description of a grammar, it's not a compiler

That might just be an unlucky preemption or something. For a reasonable test you have to do it in kernel mode and disable interrupts (or, better, just test in a single-user OS)

emitting pure x86, i don't know why would someone make a hobby project and use 3rd party tools like parser generator, assembler, linker etc.

that's my 4th compiler project and my code generation part was always the worst. mostly because i used to emit code as a parse, not very flexible.

right now the parser only generates an AST, so i walk through it and emit code. i also have an AST evaluate function and a print statement, so it's also interpreter at the moment.

thanks, i did not know that.

>and use 3rd party tools like parser generator
If it's LLk grammar you're using, then yeah, it's trivial to write a parser for it, but shit like LALR will take too much time to do it by hand so I'd rather get it written for me.

>What are you working on, Jow Forums?
Im reading yurichev.com/writings/AL4B-EN.pdf on my spare time while also learning c for an exam in an old realtime programming course i skipped due to depression a couple of years ago.

I know what a pointer is now and how to deference stuff (someone congratulate me). But the way pointers are designed are hard. Trying to wrap my head around how the hell a pointer to a pointer would work. It's seriously simple key value stuff, but somehow it's confusing with all the signs and stuff.

Why is the * also used for dereferencing? Why not some other better sign? I honestly think that & would fit better, making it sort of mean "toggle between address and value"-symbol.

class Monad m where
unit :: a -> m a
flatMap :: (a -> m b) -> m a -> m b

leftid k x = flatMap k (unit x) == k x
rightid x = flatMap unit x == x
assoc k1 k2 k3 = k1 >=> (k2 >=> k3) == (k1 >=> k2) >=> k3
where k1 >=> k2 = flatMap k2 . k1

Here you go. Now you know monads and can move on to something else.

C is not exactly LL1 but you can parse it easily with a few trick.

i parse expression with shunting yard algorithm and it takes care of almost everything, the only problem is that you can't give good errors. you have a token left in the operator stack and the only thing you can say is that syntax error at line xx.

for statements i use recursive descent, even pajeets can parse statements with rdp so it's very easy.

You can't use & for dereferencing because then you can't obtain pointers to pointers.

If you know any assembly language even on a basic level, you'll understand pointers much better.

Name for a struct consisting of a pointer to data and its length in bytes. GO

Attached: 1518833969176.png (200x209, 34K)

Yeah that's my thinking too, I'm just a beginner but it feels like assembly and C goes hand in hand.

vec

Only when necessary.

Attached: 1472923867608.png (500x437, 261K)

PointerStruct

A little confusing since in my codebase I'm using glm::vec, std::vector and have a GLSLvec.
C++ specific: Should I use a struct for something like that, or simply have tuple?
auto [data, len] = getData(); looks nice imo.

fucking ell

Attached: hmmmmmmm.png (1223x818, 787K)

Slice is also fairly descriptive.

Why the hell would professional programmers use a language that's only for people utterly terrified of programming?

>struct PointerStruct
Why?

So far the best option.

Pointer is taken

I think starting in C99 you can use sizeof(&struct)

In C89 the default is sizeof() only works for things known at compile time, but I think sizeof() can work for anything now

Unless you NEED randomly placed structs (WHY) it can be more sensible to use two arrays, one for the pointer and one for the length.i.e. pointers[i] lengths[ii]

Depends on your project but it can be a lot easier than a single struct for such small not-really-related data.

>Thirty years of C++
>utterly terrified of programming
Yeah, sounds about right.

>poland STRONK

Is the grammar ambiguous?

compiler is magic i aint gotta explain shit

stop using classes in python and just use it purely imperatively

I hope rust makes inroads into embedded systems. Someday we will actually need something with better support for concurrency, and Rust has the potential.

>using 2 arrays as a map
i love this meme

Here's what I'm doing:
I have a GLSLvec class that holds a vector of std::variant (sum-type) of all supported glsl variables. To actually do anything with it I have to use OpenGL functions like glBufferData that accept a void pointer to data and length, so I need a function that returns a dynamically allocated byte array packed with data of underlying type and its length.

OOP Design is too hard for our fizzbuzz community, please leave at once.

>Rust has the potential.
no

just call it Buffer then

>just use it purely imperatively
>"""purely imperatively"""
>while making use of a bazillion classes and methods written by other people though
Sweety..

Attached: 1376330839072.png (233x250, 73K)

>mfw people here writing in c
>mfw webdev master race

Attached: despiteallmyrage.jpg (601x508, 27K)

>what is fastCGI

How in the fuck do you make a Python class immutable?

I don't mean an instance of a class. I means the class itself.
class Test():
def __init__(self, var):
# totally normal behavior
self.__var = var

def __setattr__(self, attr, value):
raise AttributeError("That's my purse! I don't know you!")

# lazy/evil user:
t = Test()
t.lazybullshit = 12
# ERROR #

# lazy/evil user: Well FUCK you
Test.lazybullshit = 12

Of course, assuming you aren't an idiot and can write a snippet correctly...

How valid is function pointer arithmetic in C?
Say I have a function pointer of type A and a function pointer of type B. I want to cast fp1 to the type of fp2. Are any of the following expressions valid:
fp2^fp2^fp1;
fp2-fp2+fp1;
fp2*0+fp1;

Or do I have to do this the hard way with memcpy?

Nevermind. I figure it out. Metaclasses are ALWAYS the answer.

>How valid is function pointer arithmetic in C?
It isn't.
> I want to cast fp1 to the type of fp2.
This is valid, but only if you don't invoke the function pointer. It's undefined behavior if you do.
>Are any of the following expressions valid
No.
>Or do I have to do this the hard way with memcpy?
How will memcpy help you?

typeof(fp2) *f = (void*)fp1;

Casting a function pointer to a data pointer is undefined behavior. If you want a type that can contain either a function pointer or a data pointer, use something like this
union anyptr
{
void *data;
void (*fn)();
}

i know, i just did the (void*) to get rid of the error
typeof(fp2) *f = (typeof(fp2)*) fp1; would be better i guess.

Realistically how hard is it to write your own pdf reader?

pretty straightforward if you use a pre-built PDF parsing+rendering library

want to do the parsing and rendering yourself? you're fucked

I think the reason I found monads so confusing is I didn't realize bind would take the underlying value of monad and feed it into the function from a -> monad. Unless it doesn't have to do this but still that is the whole reason it is useful right?

>Working on Conway's game of life challenge from Jow Forums programming challenge.
>Figure I'll keep it simple and keep the grid 8x8
>Decide to use Java partially because I'm familar with it, and partially because I want to see how well java does bit manipulation.
>Get about half way.
>Forget that Java doesn't have unsigned primitives.
>I was originally going to store each grid row in a byte and go from there.
I want to kill myself.

Attached: 1492312059245.gif (420x276, 905K)

Well, there isn't necessarily one value. Sometimes there's no value (e.g. when you have an error with Either), and sometimes there's more than one value (e.g. when you have a non-deterministic computation with []).

alright /dpt/, i'm expecting to get memed
but, now that i'm halfway through a book on c, i'm starting to wonder where i go from here

i learned python as my first babby language, and then went to C because i was memed
now that's happened, should i pick up a book on data structures or should i go and pick up C++, even though it's shitty to be learning so many languages so early
if i try to do data structures, though, i don't know any good python books for them and i would assume ones on C data structures are a little outdated, at least

wat do

Wait what I thought the point with functional languages is that everything is a value even functions? Or am I misunderstanding something? What do you mean nondeterministic computation? Are you talking about theoretical stuff that doesn't exist in practice or just a random/pseudorandom variable?

var clk = document.getElementsByClassName("open-qr-link")[0]; clk.click()
var elems = document.getElementsByClassName("postNum desktop"); for(var i = 0; i

Just store it in a 2D array silly.

ignore the open-qr-link stuff...

also
var b = ""; var a = document.querySelectorAll(".quotesYou"); for(x in Array.from(a)) { b+= "\r\n"+ a[x].id.replace("pc", ">>"); }; console.log(b+"\r\nThanks for the (You)s");

The point of monads is being able to write a computation as if you were just passing around single values at a time when in reality there may be alternate (e.g. an error) or additional (e.g. state, other possibilities) information in play.

The list monad encodes non-determinism by letting a computation produce multiple values simultaneously and aggregating all the possible results.
data Coin = Heads | Tails

flipCoin :: [Coin]
flipCoin = [Heads, Tails]

flipTwoCoins :: [Coin]
flipTwoCoins = do
x

So (x,y) is a cartesian product? Wouldn't that use up a ton of memory?

He means like a list. A list is a value, but it's also a list of values. You can substitute every element in a structure and that can be a monad too.
Just think of any time you can replace the elements of a data structure with that data structure itself. That's bind.

Okay so you mean even turning say [3,4] into [3,[4]] is bind?

Really, I found only *doing* something was effective. It's like a real language, you need to use it to make sense of it. I had work in C and had to do some weird things sometimes.

I can't find the image, but there's a compilation of Jow Forums programming challenges. Project Euler is more Computer Science than a programming thing.

Good on you for doing the book. I can't say I have the naturalness to do programming (I'm EE). I was on the job while I was doing the book. Environment can make a weird difference.

I think you need to learn Haskell syntax before trying to understand concepts that are written in that syntax.

Found it:

Attached: 1532212833985.png (5198x2124, 2.46M)

Well I didn't ask for haskell syntax but okay. I just thought monads would pass a value to the bind function you pass even if that value contains many other values and even if it is monads all the way down. If not what is the point then? I know a monad could have a "Nothing" value and then it wouldn't pass anything of course but the idea is to pass around values without directly accessing them right?

It has to be one type, also it has to flatten out. I.e. when you use >>=, you replace every value of type A with a list of type B, and you get back a list of type B. A monad is also a functor, and a functor means you can replace every element with another element - i.e. you can turn a function (A -> B) into a function (F A -> F B), e.g. mapping a list.

a slice

Okay but you only actually return one of those lists?

What do you mean?

go check out the embedded working group

also xargo, an embedded-specific build tool is getting integrated into cargo, the mainstream build tool for Rust

I just want to know what bind does. Does it take the underlying type of M apply it to the function you pass from (a->M) then return the result of that function as M? Or does it not matter? Is the signature all that matters for something to be a bind?

there is literally a byte primitive

Yes, that's the type.
The type is the main thing that matters, sometimes you expect it to follow certain rules, i.e. binding into the method "return" shouldn't change it.

Okay that is what I meant. I guess I didn't explain myself well because I see that a Monad could have many values with no underlying type at all and bind would work differently there.

Yes, it could have no values, or one value, or many values, or a "prospective" value - like a future or an io procedure.

You could even have something like
data Unit1 a = Unit1
You can write all the monad functions simply by returning "Unit1"

Alright so it really is more about the signature than the implementation at least for the definition.

It's actually 100% about it, although it's not just the type signature, there are also laws that are not usually able to be expressed in types.

Yes, it's pretty much just the type signature. As I said, there are a few laws, but they're more like expectations or guidelines.

What's your preferred way to track nodes visited when traversing a tree?

Why does every fucking thread have to be about rust? Don't you shills have other places to shit up with your overhyped meme language? Take your circlejerk back to your subreddit, don't make every post in the thread have to be about rust

Attached: 1d3.gif (392x410, 451K)

Wolfenstein 3D for TempleOS

youtube.com/watch?v=6w4pcPrrG70

the call stack

there is literally nothing overhyped about it because it has the highest satisfaction rate among its users out of any language currently

CMake is shit. What build system does /dpt/ use for their sepples projects.

How do I know if I like programming or not?

that's because it's a cult

Should I link my git on my resume even if a lot of my projects have bad looking code practices?

Why does every fucking thread have to be about HASKAL? Don't you shills have other places to shit up with your overhyped meme language? Take your circlejerk back to your subreddit, don't make every post in the thread have to be about HASKAL

How do I into systems programming?

i personally like make.
meson looks good too.

sure, its unlikely that they will actually look through it.