/dpt/ - Daily Programming Thread

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

Attached: 1543874106187.jpg (1500x1500, 1.26M)

Other urls found in this thread:

crates.io/crates/indicatif
onlinegdb.com/BJdn1v7QE
twitter.com/SFWRedditImages

First for Rust and Haskell

Second for C and C.

nth for Nim!

nth for nim!

Back for ArnoldC!

Attached: npc.png (96x128, 7K)

Hey! Question.
In C++, if I reinterpret_cast something to another type and dereference it, that is technically UB, right? I'm writing an allocator where char * is the type of the arena pointer and I'm using reinterpret_cast to allocate objects.

Haskell is slow

Correct. Char and unsigned char are exceptions to the rule though, specifically permitted for stuff like custom allocators or type punning.

nimPC

Looking into a simd solution.
I'm leaning towards something like ISPC. But I don't find any comparisons between auto vectorization and ISPC. I'm using clang and would appreciate a way to signal the expectation that something is going to be vectorized. So I could have an easier time with potential regressions.
A lot of people also recommend you just use intrinsics but that would probably mean targeting the lowest common denominator for me (SSE2). It's for a desktop program.

>error: expected identifier
>thing->stdin.read = -1;
>/usr/include/stdio.h:60:16: note: expanded from macro 'stdin'
>#define stdin (stdin)
is this musl libc being stupid or correct? it compiles fine with glibc and i'd rather not rename the struct member unless some official-looking standard says i have to

the absolute state of C

stdin is defined as a macro, so you shouldn't use it here.

Considering that's not the standard input stream, it's weird to name your variable that.
But in general, you need to be very careful with identifiers that match standard ones, because a lot of them are allowed to be macros.

>Wake up
>Decide to code a small CLI downloader in Rust
>With a progress bar for ANSI compliant terminals…
>It's trivial shits' right?

That feel when I made every single possible mistakes (array overflow, integer division OBVIOUSLY rounding to 0, syntax errors, mistaking bytes, string and char, inability to perform basic math, like I forgot how to calculate percent, forgot to flush() stdout, forgot that \0 stand for octal numbers, used UTF-8 string instead of OsString, forgot to consider all edge cases, etc…)
FUCKING HELL, I SUCK!

That was a good reminder nonetheless. Thank god someone invented Rust to prevent brainlet to harm themselves too much.

LoL
>unless some official-looking
Welcome to programming! We conform to implementations before standards.

>\0 stands for octal
In what context does it do that in Rust? Usually that's nullbyte in character arrays.

fizzbuzz = fn
0, 0, _ ->
"FizzBuzz"

0, _, _ ->
"Fizz"

_, 0, _ ->
"Buzz"

_, _, x ->
x
end

say_fizzbuzz = fn n ->
fizzbuzz.(rem(n, 3), rem(n, 5), n)
end


cool

i thought a real programmer would just not support hipster libc implementations

>musl
Do you happen to be a vegan too?

It's not directly related to Rust. I found documentation saying that I should send \033[1A to STDOUT to move the cursor one line up.
I Scratched my head for a while until I found that in Rust it should be \x33[1A.
Low IQ sucks, I tell you.

…err \x1B[1A I mean.

>I found that in Rust it should be \x33[1A
No, that's not right. The escape character is 0o033 or 0x1b.
You were probably entering the decimal 33, which is wrong, but 0x33 shouldn't work either; that is the character '3'.

There's a crate for it: crates.io/crates/indicatif

Attached: yarn.gif (1128x620, 664K)

I rather do it myself, it carries a lot of «lessons» for the future

JavaScript rocks!

Attached: javascript rocks.png (1000x494, 134K)

diving into API, looking at the docs, the one im trying to pull from uses OAuth2
It waffles on about client auth, access/refresh tokens etc but I don't care about my clients auth, I just want to pull data for myself
Do I still need everything or is the access token enough

You're goddamn right

TypeScript paper

Lua rocks!

Attached: 1496757793100.jpg (573x892, 113K)

think you want m8

>write a shit compiler for a shitLang
>woah im hot shit now
>decide to write an interpreter for the shitLang, since interpreters are obviously trivial
>struggle for days, start over 5 times, finally get it to work through a hack
>still not sure if it actually works in all use cases

i think the key is to do things you actually WANT and CARE about, instead of 'i just have to be able to do this, it's trivial stufff right'

every reference I'm reading says you're right, it wants me to use it on a stupid website but I just want to pull it into a local app using python
I will ask them I guess. Every lesson I find online for it uses public APIs. I hate being low IQ

Sometimes it's worth doing boring shit so you appreciate how much work it is.

Rust doesn't have this problem

Attached: 1548072720735.png (1866x1944, 317K)

did you write up your Jow Forums post in latex or something

who cares what language the compiler is written in?
>c no longer hosted
lol what?

of course not, latex is inferior to rust. it's not safe enough and doesn't superscale.

self hosted

tcc YOU FOOL

who
reminder that pet projects with inferior or no optimizations don't count as relevant

It optimizes. No idea what counts as "pet project" in you crazy head, probably anything that is not written in rust.
Keep moving your goalpost buddy.

Go read some GCC source code (excluding C++ exclusive parts like libstdc++), and tell me that's "C++".

gcc can't be compiled with a C compiler, tell me that's "C".

>Rust doesn't have this problem
It's like removing the +/-* operators from the language to avoid arithmetic errors.

GCC was never written in standard C to begin with. It's full of fuckloads of extensions which basically turn it into a bastardisation of C. At that point, the difference between that and a couple of originally C++ features they shoehorned into their C code is largely irrelevant.
My other point is that using GCC as some pinnacle of something being C++ is retarded.

Is there any difference if I program and compile stuff on an AMD or Intel platform?

There could be if you use -march=native or -mtune=native, the compiler will optimize for the CPU you have.

Where did this meme that C isn't self-hosted come from? The parts of GCC written in C++ are the ones that pertain to C++ compilation last time I checked.

>>use C
>C is dead
I think this person doesn't recognize how many of their issues stem from using C++ features.
The standard library would still be limited of course. But the burden of using C libs is much less than using C++ libs.

The practical answer is no. You'd have to be a very discerning programmer with very specific code for this to change what you do when programming.

weak pointers make me feel good

You can't compile gcc with a C compiler, nor can you clang or MSVC.

good pointers make me feel weak

Where my OCaml niggas at?

Idiots.
They don't realize that there's both more compilers than gcc/clang/MSVC and there's old versions that do compile C.
It's obviously self hosted and only someone who lost half their brain in an accident is justified in trying to say otherwise.
Not that it matters at all. The majority of programmers don't even poke the compiler privately, nevermind actually make some meaningful change. How many of you have commits to MSVC clang or gcc? Likely zero. It's how it should be. If all of us were constantly messing with compilers they would have to be awful for that to be justified.

i need help with for loops in c

for ( init; condition; increment ) {
statement(s);
}

normally you would set the initiation to a number, but my initation is a varible i have decalred earlier, a variable called g

so instead of initiating g to anything, i just want g to be g, because its input from elsewhere

for (my variable g; needs to be checked if its higher than a number; then subtract a number)
{

}

>that do compile C
*that are written in C.

ive tried this:

for (g = g; g > number; g - number)

then the compiler says that g has already been declared earlier, which is true, so im not sure how to just use g as is

for ( ; condition; increment ) {
statement(s);
}

You don't have to put anything there.
Just for(;condition;increment)
Even for(;;) {} is valid. But that's an infinite loop.

Is the retard who hijacks dpt OPs with his (non anime) blogposts still around?

"variable 'g' used in loop condition not modified in loop body"

hmm, im trying to make like a cash register as a problemset, user puts in the g variable as a float, the loop needs to check and then subtract and print until g is lower than a certain point

every section of a for loop is optional, like said

If you post more of your code it's easier to see what you're doing wrong.
Use code tags
[code.]your code here[/code.] remove '.'

[code.]print 'test'[/code.] remove '.'

sorry, it's a basic cash register
user puts in a float like 4.25

for (; g > 0.25; g - 0.25);
printf("Here's a quarter"\n)

im not even sure if i should be using a while or a for loop for it

bbcode too hard for newfags


▲ ▲

>g - 0.25
that doesn't mutate g fella. yknow g -= 0.25 and other such things

>g - 0.25
This doesn't modify g
You want g = g - 0.25
Or for short g -= 0.25
Same thing.
>for(...);
The semicolon there is not what you want. Try to always couple for loops with blocks for a start.
Always write

for(...)
{
//loop body here
}

If you put a semicolon right after the for statement it won't do anything except for what's inside the loop statement.

Literally first month of programming, I'm working on this:

I have an input string ABBCDEF...
and some substrings, each with a value
AB = 2
ABC = 6
CDE = 1
CD = 2
A = 9
B = 16
C = 20
etc.

How can I break down the original input string into the concatenated substrings so that the total value is miinimized?

>emojis in a terminal
What the absolute fuck is wrong with people these days?

so to change g
for (; g > 0.25; g-=0.25)
{
printf("Here's your quarter\n");
}

for (; g > 0.10; g-=0.10)
{
printf("Here's your dime\n");
}

and so forth

maybe it would be smarter to use a while loop im not sure
>while g > 0.25 print "here's your quarter" and subtract g by 0.25
>if g > 0.10 || g < 0.25 print "here's your dime" and subtract g by 0.10

You have an iteration statement so a for loop is appropriate.

Dont know where you found this but this can only be done by bruteforce which makes for a shitty example to learn coding by

Isn't the galaxybrain solution some form of nondeterministic finite automaton?

>dpt OPs with his (non anime) blogposts
who?

Attached: 1545777847077.jpg (374x374, 41K)

Is there a way to get the JSON body/payload in a Go request?
r.Body seems to have it in there somewhere but the decoder is spitting out some useless error and I want to see exactly what it's getting.

Create a number guessing game where the function takes n and a list that contains n.

Is there some sort of restriction on the values? If not, there's really no efficient way to compute this. You can always construct an example where a single combination is the soluation, but you still have to inspect all others.
To verify your solution you also have to test against all other combinations, so it's not even in NP.

Here's a peanut brain python solution:
onlinegdb.com/BJdn1v7QE
Haven't checked for correctness in _any_ way.

Yeah, the substring values are capped off at 100 max. I don't know how it helps me though.

Looking through it right now

okay thanks for the help

just use Gin

A cap doesn't really help. You need some sort of relation between a substring and its value. If you had something like "a string has lower value if it's longer", you wouldn't need to be blindly guessing

No such relation.

This looks like it'd find a combination but not the optimal one, let me check.

It's a bit early in the day for me.

>Looking through it right now
Just note that these aren't optimal by any stretch.
As others have stated the problem is bruteforce.
The solutions really spring from things I think the description of the problem implies.
AB = 2
ABC = 6
CDE = 1
CD = 2
A = 9
B = 16
C = 20

To me it looks like this list favors characters late in the alphabet with low values and gives short string matches a penalty. But AB = 2 and CD = 2 just shatters that pattern. Even if this list was completely made up by you I think something relevant to the original list might have snuck in.
If this is a rookie problem they either aim for you to realize the utility of library functions (method1) or they wish for you to have engaged with this problem a little (method2).
>not the optimal
It absolutely won't find optimal for anything but very short strings. The second method presumes you only eat the input from the front but even there it's not sufficient.

just sord acsending by the value and take the first combination of substrings that when combined equals the original string lmao

>mfw he hasn't been enlightened by nim yet

Attached: 5flyip5fqr311.jpg (257x320, 29K)

Of course it won't find the optimal one. Again, this isn't even an NP problem because you can't check the solution in polynomial time. Repeat:
There is no efficient and correct algorithm for this problem.
Your only option is to literally try every solution that exists.

Where the hell does this problem come from anyway?

>using nim when crystal exists
python has shit syntax

>Where the hell does this problem come from anyway?
Answer please.

>ruby syntax
i want to puke.

A friend sent it to me yesterday so I don't know. I'll ask when I see him.

crystal isn't even half as useful as nim

Do we actually know if it is bruteforce?
Can there be some incredibly clever solution?