/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?

Old thread:

Other urls found in this thread:

bosslinux.in/boss-mool
tech.okcupid.com/rust-and-rpc-okcupid-hackweek-2017/
en.cppreference.com/w/cpp/algorithm/transform,
martin-ueding.de/en/articles/qsort-vs-std-sort/index.html
github.com/radioman/WebRtc.NET/issues/43
twitter.com/SFWRedditGifs

nth for rewriting the linux kernel in basic

Which BASIC?

he's doing it in visual basic

M$ of course
then

Which Visual Basic?

see

The only version that matters
S E V E N
E
V
E
N

In a doubly linked list, how would you go about switching two nodes where you don't know there positions within the list when using pseudo code?

just swap their values

wait, so you write in .NET VB?

You mean with pointers?

switchNodes(Node* n1, Node* n2) {
Node* nodeTMP;

nodeTMP = n1;
n1 = n2;
n2 = nodeTMP;
free(nodeTMP);
}

In hell, im just shitposting

(rotatef (nth (position h l) l)
(nth (position g l) l))

Why I can't have functional programming in C just passing function pointers?

>just passing function pointers?
Because FP was designed to get away from shitty c hacks

>Instead you have to assign sqrt(2) to a temporary variable, and then add 1 to that.
I can do that, but why would I need that?

>from shitty c hacks
Such as?

But FP came before C, user.

def switch_nodes(a, b):
a, b = b, a

Wooby a cute!

C's type system isn't very good tbdesu

I'm shit at programming and should just kill myself

What's bad with type system?

also lambdas amd closures

Learn Lisp.

no proper polymorphism of any kind
(not OOP polymorphism)

just (non type safe) void pointers

>Such as?

Not him, but the whole idea of using something like a map-/forEach-iterator is that you can't access anything outside the members of the given collection.

If you use pointers you have the problem that they can (theoretically) point to anywhere. A completely different logic with differnt pitfalls.

every fucking thing is a void*, you can't even call that a type system.

Are you saying the function that is mapped over a collection cannot access anything other than the elements of said collection?

How would that even work?
Something like this?
void *map(void (*fp)(void *arg), void *data, size_t size)
{
void *out = malloc(size);
for (size_t i = 0; i < size; ++i) {
*(i + out) = fp(*(i + data));
}

return out;
}

>no proper polymorphism of any kind
Isn't that a result of strong typing?

C is weakly typed

No, you can have strongly typed polymorphism.

Exactly. Also, checked.

>void *map(void (*fp)(void *arg), void *data, size_t size)
{
void *out = malloc(size);
for (size_t i = 0; i < size; ++i) {
*(i + out) = fp(*(i + data));
}

return out;
}

absolutely gross

ur shit

>*(i + out) = fp(*(i + data));
Wrong.
*(i+data) has type void and the mapping function takes a void*. Also, I don't even know what would be returned by dereferencing a void*.

Building a neural network over here

Are you trying to build an AI waifu?

Yes, that is what I'd like to achieve someday (but I'm startIng out simple)

where would /dpt/ rather work:

failing startup
or
incompetent corporate giant

failing startup

You'll still get paid 100k for a year or two and be able to plan out your next job.

>Also, I don't even know what would be returned by dereferencing a void*.
You can't.

Haskell is a nice example

>everyone here shitting on c++
>somehow defending this void* mess

but what about soulsucking crunches, bro culture and sexual harassment user

Incompetent corporate giant ofc, in big companies no one notices you're dicking on the internet all day long instead of doing your job.
t. work in a relatively big company

Incompetent corporate giant

I wouldn't have to do shit. Either that or I look like a god because I'm the only competent one.

CL-USER> (mapcar (constantly 'garbage) '(c c++ rust python haskell))
(GARBAGE GARBAGE GARBAGE GARBAGE GARBAGE)

That's what I figured.

Wanna write that as a generic for us?

At least void* isn't template

// input type, result type
r* map (r(*fp)(i), i* data, size_t)

Obviously, void* is slower and less safe.

void* isn't slower and templates are less safe

Depends on the role. For junior or mid-level, corporate giant. Either they've got a lot of room for improvements or they're just looking for someone to help maintain the monolith and do smaller projects, and no one's riding you to work 80 hours to work on "it's [successful startup] but [adjective]" so you get to go home at the end of the day and have time to do whatever you want.

I've heard startups are cooler for the extremes of the skill ladder (juniors or veteran devs); my friend is currently working at some corporate giant in NY and it's torture because he's in meetings almost constantly instead of being allowed to actually program.

>indians rewriting Linux kernel to be OOP
bosslinux.in/boss-mool

does it have uses/advantages/future programming wise in your opinion, or it's just another case of poo.init()?

unlikely to be a good thing

void* is slower because it forces to use full-wide pointer even for trivial shit like uint8_t and because monomorphisation allows better optimization. void* is less safe because it doesn't enforce type safety.
You claiming otherwise just show how little you understand.

More like POO/Linux, or as I’ve recently taken to calling it, POO plus Linux.

the """equivalent""" code would be
template
... T* ...

(and even then that's not equivalent because it's instantiated and not actually polymorphic)
--
void* DOES enforce type safety, and templates don't
the difference is void* does not "remember" the type pointed to, but then it isn't supposed to anyway

temples only type check when instantiated, in C++ you might even have incorrect syntax and the compiler can't tell if the template isn't instantiated

LOL

tech.okcupid.com/rust-and-rpc-okcupid-hackweek-2017/
Meanwhile, OkCupid is close to using Rust in production.

It would be worthwhile only in CLOS.

basically, templates are just macros that can (in very specific contexts) have arguments inferred, and that can declare code at the top level

they aren't really polymorphic

learning some python, i would like to do some scripts to pipeline stuff into for some utility stuff in bash, but i am kinda out of ideas, someone?

import random
while True:
print(random.random())

>tfw defending weak typing

>the """equivalent""" code would be [..]
Equivalent to what? C++'s equivalent of map is en.cppreference.com/w/cpp/algorithm/transform, C++'s equivalent to malloc of void*'s is a vector with values stored in-place. If you want to use a pointer you just use "MyType*" as a T. Check martin-ueding.de/en/articles/qsort-vs-std-sort/index.html for comparison of std::sort with qsort, the latter being an example of void* mentality, std::sort BTFO it.
>void* does not "remember" the type pointed to
>void* DOES enforce type safety,
What, are you even serious now?
>in C++ you might even have incorrect syntax and the compiler can't tell if the template isn't instantiated
Yes, so? Non-instantiated template code doesn't compile at all, it's not like you lose some safety because of it.

Do those coffee warmers actually work?
I might just buy one.

this
these template fags need to fuck off
we don't need your macros

I don't get it

Getting destroyed by trying to implement Dijkstra's algorithm with a custom class.

Say I have the following structure:
struct Index
{
int col, row;
Index( int c, int r): col(c), row(r) {}

friend ostream& operator

>there are retards ITT comparing static metaprogramming to runtime polymorphism

vectors use a pointer too
if you wanted a better example you would have used something where it references the type directly, e.g.

template
struct Named {
T x;
string name;
}


and again, not equivalent to a void pointer

>>void* does not "remember" the type pointed to
>>void* DOES enforce type safety,
>What, are you even serious now?
Yes, it ensures that things that are void pointers and things that are not do not match.

>Yes, so? Non-instantiated template code doesn't compile at all, it's not like you lose some safety because of it.
i'm sure there's nothing wrong with a system which people are supposed to use for generics can fail despite compiling

it's not like there have been tons of problems with people when the template is instantiated, or even instantiated with a different type
it's not like C++ has awful errors
it's not like people might #include a library with templates and instantiate it differently

Stupid osdev question:
do each processor needs a separate idt and gdt table?

I want to have some bignums in C.
My bignum type is built on BCD, and benefits by being theoretically unlimited, since it uses unused values to mark decimal point and the end of number. Like:
127.38482804727491

would be equal to (in hexadecimal)
127A384828047274910F

I think I have to read values as strings, so I think it's going to look like
new_bignum(a, "127.245")

where a is going to be some pointer.
I will post my implementation soon, stay tuned.
How would you solve this problem in your language?

Void can point to any data type, and you have to cast it to use it. It doesn't do type checking.

His coffee got cold.

Store the 10^k and 10^-l separately.

This. void* (and similarily interface{}) is the worst form of generics as it requires indirection and throws type safety out of the window. It is the sign of weak language.

Yes, every processor has it's own control registers assoc. with idt/paging ect

Why?

Trying to figure out why this code doesn't work

github.com/radioman/WebRtc.NET/issues/43

Clojure is statistically typed.

Ah thanks

show set instantiation/adding + checking Index's to the set code.

How would you implement something like
int foo ( template int (*func)(T) ) {
return foo(3) + foo(3.5);
}
in a C++-like language?

clock_t clockT = clock();
time_t timeT = time();


What is the difference ?
What is best to measure the run time of my sorting procedure?

Give me one good argument that attest "functional programming is better than OPP in some cases"

What does it do?

wrong

>can point to any data type
and?
you can even do this in Haskell
data Box = forall a. Box a

>you have to cast it to use it
no, you can use it as a void pointer all you like
you can store it, copy it, move it around
you have to unsafely cast in order to treat it as a specific type
that's type safety
you know what isn't type safety?

template
T add(T a, T b) { return a + b; }

I worked for a webrtc project. Lots of javascript and shit.
My company later bought Alcatel Lucent's webrtc.
Is this still a relevant technology?

if it's only done at compile time, you would have to instead use a struct, and it would be part of a template parameter and it would be god awful on both sides

It takes a generic function as an argument and instantiates it in different ways at runtime

>equivalent to a void pointer
Can we agree that qsort is a typical void* solution, and std::sort is it's equivalent? Because it seems so and qsort is being destroyed by std::sort.
>Yes, it ensures that things that are void pointers and things that are not do not match.
So you're saying type erasure is somehow type safe? Do you even know what "type safe" is?
>i'm sure there's nothing wrong with a system which people are supposed to use for generics can fail despite compiling
It looks like you're confused, it's not like bogus template code can just find its way into resulting binary without any checks. If template code isn't instantiated then it's not compiled at all.

set hasSeen; //set instantiation
priority_queue paths; //Node has the following structure: Node.ind (Index),
//Node.weight (long), the latter keeps track of path length
/* set up priority queue */

while(!paths.empty() )
{
Node currNode = paths.top();//Take an element out of the "to-do" list
paths.pop();

Index currInd(currNode.ind.col, currNode.ind.row); //Index initialization
if(hasSeen.count(currInd))
continue; //If you've seen a node, then skip it (because it won't be the most efficient path to get to an index)

hasSeen.insert(currInd); //Insertion
cout

I think we have very different ideas of type safety.
Also much of C++'s weird shitty type rules come from it being aggressively weakly-typed

I didn't mean it's worse. It's just different. Static != runtime. Templates were introduced to allow some form of static genericity. Thus it is quite stupid to compare them. The corresponding C++ feature would be dynamic casting.

>Can we agree that qsort is a typical void* solution, and std::sort is it's equivalent? Because it seems so and qsort is being destroyed by std::sort.
that's probably because std sort uses operator< whilst qsort uses a function pointer

>So you're saying type erasure is somehow type safe? Do you even know what "type safe" is?
i don't know what you mean by type erasure, but void pointers in C are type safe
the only thing that isn't type safe is casting, which can be done with any pointer


>It looks like you're confused, it's not like bogus template code can just find its way into resulting binary without any checks. If template code isn't instantiated then it's not compiled at all.
if nobody was ever going to use it (or had ever used it), it wouldn't be in the source

Yeah but the problem with C and Go is that there is literally no way to do statically checked generics. If a language had both then that would be great because dynamic generic behavior is often useful (this is essentially what OOP tries to solve).

how do i return a template from a function in c++
let's say it takes two ints and returns a template

void* is exactly what it is supposed to be, and the weak typing of C is do to
1) lack of discriminated unions and pattern matching (hello C++!)
2) casting (hello C++ inheritance!)
3) generally avoiding using types because of the clunkiness (C++ is better at this but still not great)