/dpt/ - Daily Programming Thread

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

Other urls found in this thread:

huonw.github.io/cfor/cfor/
doc.rust-lang.org/1.2.0/book/dining-philosophers.html
twitter.com/NSFWRedditImage

Your mom

C++ can't do this
foreach(index, number; numbers)
if (index == number)
//do stuff
}

>frog thread
What subreddit are you going to now that Sup Forums is clearly fucking over and just another part of reddit now, Sup Forums??

haskell!

user can't maintain a counter

>mismatched curly brace
What did he mean by this?

dumb frogposter

C++ cannot enumerate foreach!

Reposting
Challenge:
Get the index of the current element of a range based for loop in C++ without causing undefined behavior.

Really, are you going to defend this with that argument?

Impossible

FOR WHAT PURPOSE

you can't do it in java either, because if you're using a foreach you either don't need the index or you're FUCKING RETARDED

KILL YOURSELF

C# can do it, even (((Rust))) can. That's a new fucking low for C++

C# and rust are shit tho

java and C++ are both industry-leading programming languages

This hurts a sepples cuck
foreach(index, number; numbers)
if (number == some_value)
return index;

Really? Are you seriously defending this?
Did anyone brainwash you?

rustfags can't answer this simple question:

FOR WHAT PURPOSE

I'm not a rustfag. but should explain you why. If that is you have a single brain cell left

LITERALLY KILL YOURSELF YOU LITERAL FAG FUCK OFF

YOU'RE NOT WASTING ANY MORE OF MY TIME THIS IS YOUR LAST (YOU) FROM ME

LITERALLY KILL YOURSELF TRANNY FAGGOT

>hurr durr other languages are shit so mine is better
The EXACT same strategy rust shills take to shill their lang, not anymore!

for(size_t i = 0; i < array.size(); i++) {
if(array[i] == some_value) return i;
}

This is far more readable, and perfectly possible to do in C++.

>autistic meldown
C++
Can't
Enumerate
For loops!

>C++
>Can't
>Enumerate
>For loops!
/care

>This is far more readable
No it isn't, it looks like utter garbage eww
I might as well use a fucking while (true) loop

Are you crying right now?!

>This is far more readable
lol

>That shit is readable
C++ajeets, everyone

>far more readable.
Even if this were the case its more error prone.

This is an honest question, guys. Are you brainwashed?

#[derive(Debug)]
enum Intelligence {
Dumb,
Smart
}

#[derive(Debug)]
struct Frogposter;

trait IQ {
fn iq(&self) -> Intelligence;
}

impl IQ for Frogposter {
fn iq(&self) -> Intelligence {
Intelligence::Dumb
}
}

fn main() {
println!("{:?} {:?}", Frogposter.iq(), Frogposter);
}

Compiler Output:
rustc 1.18.0 (03fc9d622 2017-06-06)
warning: variant is never used: `Smart`
--> :4:5
|
4 | Smart
| ^^^^^
|
= note: #[warn(dead_code)] on by default

Output:
Dumb Frogposter

>putting this much emotional load on C++
It ain't healthy man. Really just don't do that.

The original was so unreadable it took me like 2 minutes to figure out what it's trying to do. Foreach with three arguments doesn't even make sense.

well done /dpt/ you shitposted for 30 minutes and trigger an autistic while I learned how to write Java code that never runs the gc

Why do you declare a constructor you don't use?

If an exploded tuple confuses you that much I think you should work on that.

//this is unreadable(!)
foreach(index, number; numbers)
if (number == some_value)
return index;

//this isn't (!)
for(size_t i = 0; i < array.size(); i++) {
if(array[i] == some_value) return i;
}

//PSA: C++ damages your brain

Other posters might be smart

Assumptions:
- The container is laid out serially. If a container isn't laid out serially, its "indices" aren't called indices, they're called keys.
- The range-based for loop traverses the container's elements in index order. If not, it's a shitty container, and whoever tried to stick it in the algorithm I'm about to show you has done a bad thing and should feel bad.
std::vector indices;
typename container::size_type i = 0;
for (auto elem : container) indices.push_back(make_pair(elem, i++));

this

JESUS FUCKING CHRIST WHAT ABOMINATION IS THAT

The two are readable.

>rust doesn't have a C-style for loop

Note to explain thing: Using vector of pairs instead of map because the container might contain the same element more than once.

They are not brainwashed, they are just trying to control damage. In fact any normal person can read and understand what this is doing just by assuming the variable names.

dumb frogposter

has anyone written a image classified for anime yet?

Haskell doesn't even have a loop. That being said, Rust macros are powerful enough to completely emulate C99 style loops.

Let's see your shit lang doing that oh wait it can't

I agree on the first assumption but not the second. There can be good reasons based on the iterator used.
I'll accept this as ok though.

The challenge said "without causing undefined behavior."
It didn't say "without causing unspeakable behavior."

EWW WTF is that ugly garbage?

huonw.github.io/cfor/cfor/

>needs all that fucking punction
C++ faggots are in clear denial

see: It's ugly sepples-speak for "just declare an int and increment it as you go along and that's the index, and if it's not, then you're container a shit"

Frogposter utterly btfo. This is something new (!)

It's called "C++", user!

``Tuples" are only used by those who are afraid of pointers.

>rust doesn't have a C-style for loop

Stupid fucking frogposter >:(

tuples are just a convenient alternative to adhoc structs.

C is for those who are afraid of assembly.

I have a local git repo connected to a remote one. Some time ago I made branch A on top of the master, made a commit, and then created branch B on top the A branch, and made a commit to B, and everything was pushed to the remote. Eventually, the A branch has been merged into master on the remote, and I pulled everything back into local repo.

Now I need to rebase the B branch no top of the master. Apparently, I can do `git rebase master` and get it rebased in my local repo, which creates a new commit on top of the master, which is apparently what I want. But when I try to push it to the remote I get
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.

which is not that I want. So, how do I rebase it on the remote?

dumb frogposter

In C, this is just:
int const len = sizeof(container)/sizeof(*container);
int i; for (i = 0; i < len; i++) printf("%d\n", i);

doc.rust-lang.org/1.2.0/book/dining-philosophers.html
>fn main() {
let p1 = Philosopher::new("Judith Butler");
let p2 = Philosopher::new("Gilles Deleuze");
let p3 = Philosopher::new("Karl Marx");
let p4 = Philosopher::new("Emma Goldman");
let p5 = Philosopher::new("Michel Foucault");
}

dumb frogposter

Come on user. 2 minutes. Face the fact, you have a problem.

>1.2.0
>Posting outdated documentation

Why Sup Forums hates this green cancer so much?

>inverted swastika
kys

>rust doesn't have a C-style for loop

>Why do you hate cancer?
That's a tough question
dumb frogposter

>frog posting
So this is where C++ajeets resorted to

if I understand correctly, you have a branch B that's behind master and you want to replace master with B?

That's not reccomended because it'll destroy part of the history of master. The best practice action is to:
>checkout master
>create a commit that reverts the commits you dont want in master anymore, commit them
>switch back to B
>git pull and merge master into B
>merge B into master

if you dont care about maintaining the history of master (you can only do this if you own the remote repository and DONT do this if you work with other people)
>check out master locally
>checkout the last commit you care about
>merge in B, commit to master
>git push origin master --force

Disliking some memes is fine. Being buttmad about them causes even more cancer.

You're looking for something equivalent to Ruby's :each_with_index method? Why? The C++ way of doing this is with a C-style for loop.

for (size_t index = 0; index < numbers.size(); i++) {
auto number = numbers[index];
/* Do something here with index and numbers */
}

You've got to go back

>The C++ way of doing this is with a C-style for loop.
That's cancer if you're doing anything with a non-constant index complexity, like a linked list. Just use your standard foreach and maintain a counter.

>Fighting meme users by feeding them
Do you understand that it doesn't work?

you feed them so that it hurts them all the more when you whip out the cane

>actually defending frogposting

Thanks, I've ended up doing push -f, since I didn't care about the history and other people at all.

>actually greentexting
whomst arst thee quoth

>tfw too unintelligent to be a lisp weenie

Indeed it is bad if you want to use a linked list, but to be fair, you should not be using a linked list in the vast majority of cases. Realistically, how many times do you need to both insert at middle a fuck ton of times AND have the index kept track of? Otherwise, yes, a counter with range style for is fine.

If it's an std::vector or array you can get the index by subtracting the address of the element from the address of element 0.

>rust doesn't have a C-style for loop

That's right, Haskell provides both Lisp flexibility and static typing guarantees.

You can implement any kind of type system you want with reader macros, or even another language if you want..

>static
I think the word you're looking for is "strong"
For contrast, C is an example of a STATICALLY typed language

size_t index = 0;
std::for_each(std::begin(numbers), std::end(numbers), [&index](const Number& nn) { if (index++ == nn) /* do stuff */ });


btfo.

>You can implement
But why would you waste your time on it instead of taking an existing high-quality implementation?

Actually I meant "strong static", but I didn't bother to mention "strong" because "weak" typing does not provide any real guarantees.

dumb frogposter

>existing high-quality implementation?
No such thing.

haskell!

wrong

Undefined behavior.
See

Nope

>Undefined behavior.
None of those points would apply.
You would never be two past the end of the array.

rude!