/dpt/ - Daily Programming thread

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

Other urls found in this thread:

youtube.com/watch?v=uqsZa36Io2M
github.com/desvox/q4
twitter.com/NSFWRedditVideo

Idris is the language of the future

How the fuck are all the Django tutorials broken. Fuck this framework.

>inb4 removed ableist language

youtube.com/watch?v=uqsZa36Io2M

$ cat branchless.c
#include
#include
#include
int max1(int x, int y) {
return (x > y) ? x : y;
}
int max2(int x, int y) {
return (-(x>y) & (x^y)) ^ y;
}
int main() {
srand(time(NULL));
for (int i = 0; i < 1000000; i++) {
printf("%d", max1(rand(), rand()));
printf("%d", max2(rand(), rand()));
}
}
$ gcc branchless.c -std=c11 -O2
$ objdump -d a.out
[...]
0000000000400660 :
400660: 39 fe cmp %edi,%esi
400662: 89 f8 mov %edi,%eax
400664: 0f 4d c6 cmovge %esi,%eax
400667: c3 retq
400668: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1)
40066f: 00

0000000000400670 :
400670: 31 c0 xor %eax,%eax
400672: 39 f7 cmp %esi,%edi
400674: 0f 9f c0 setg %al
400677: 31 f7 xor %esi,%edi
400679: f7 d8 neg %eax
40067b: 21 c7 and %eax,%edi
40067d: 89 f8 mov %edi,%eax
40067f: 31 f0 xor %esi,%eax
400681: c3 retq
400682: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
400689: 00 00 00
40068c: 0f 1f 40 00 nopl 0x0(%rax)
[...]

>muh optimizing compiler will fix everything

Any text versions of this?

Not that I'm aware of user

What about this?

Scheme is the only good lisp dialect desu senpai. Cuckmon lisp doesn't have proper functional programming support because of the lisp2 meme after all, on top of being bloated as fuck.

1) They should both compile to the same thing
2) max2 (a bit hack) is faster than max1 (the "more clear" version which "professional" "programmers" will always recommend you)

max1 produces shorter code, however

How fast? You benchmarked your example with rand calls?

shorter code != faster code

tested this and max1 is slightly faster

wasn't saying it was (but on some weird cache-constrained architecture it might be)

On RISC architectures, it is. On the CISC RISC hybrid abomination of Intel though...

Eitherway people use neither: they macro max1 instead (#define max(x, y) (x > y? x: y))

>not parenthesizing correctly

How did you test it? With O2 enabled? Even with branches, depending on the input you can get good results because branch prediction.

Yes, but cmov is slow.
The point of this was the assembly was different. Benchmarking with rand calls isn't optimal, what's the keyword for "don't constant propagate this variable" for GCC?
Platform-dependent and missing the point.

>y?
>x:
(x > y) ? x : y

heh,
_Z4max1ii:
cmp ecx, edx
mov eax, edx
cmovge eax, ecx
ret

_Z4max2ii:
xor eax, eax
cmp ecx, edx
setg al
xor ecx, edx
neg eax
and eax, ecx
xor eax, edx
ret

_Z4max3ii:
cmp ecx, edx
mov eax, edx
cmovge eax, ecx
ret


max 3 is C++.
int max3(int a, int b) {
return std::max(a,b);
}

Wrong.
Sorry, I'm not homosexual.

Is it possible to write code with ethanol in your system? If so, how?

ballmer peak

somebody PLEASE make a Sup Forums extension for Thunderbird

I want my shitposts in the same place as my email and rss

Can your language do this?

val book =
("author" ->> "Benjamin Pierce") ::
("title" ->> "Types and Programming Languages") ::
("id" ->> 262162091) ::
("price" ->> 44.11) ::
HNil

scala> book("author") // Note result type ...
res0: String = Benjamin Pierce

scala> book("title") // Note result type ...
res1: String = Types and Programming Languages

scala> book("id") // Note result type ...
res2: Int = 262162091

scala> book("price") // Note result type ...
res3: Double = 44.11

scala> book.keys // Keys are materialized from singleton types encoded in value type
res4: String("author") :: String("title") :: String("id") :: String("price") :: HNil =
author :: title :: id :: price :: HNil

scala> book.values
res5: String :: String :: Int :: Double :: HNil =
Benjamin Pierce :: Types and Programming Languages :: 262162091 :: 44.11 :: HNil

scala> val newPrice = book("price")+2.0
newPrice: Double = 46.11

scala> val updated = book +("price" ->> newPrice) // Update an existing field
updated: ... complex type elided ... =
Benjamin Pierce :: Types and Programming Languages :: 262162091 :: 46.11 :: HNil

scala> updated("price")
res6: Double = 46.11

scala> val extended = updated + ("inPrint" ->> true) // Add a new field
extended: ... complex type elided ... =
Benjamin Pierce :: Types and Programming Languages :: 262162091 :: 46.11 :: true :: HNil

scala> val noId = extended - "id" // Removed a field
noId: ... complex type elided ... =
Benjamin Pierce :: Types and Programming Languages :: 46.11 :: true :: HNil

scala> noId("id") // Attempting to access a missing field is a compile time error
:25: error: could not find implicit value for parameter selector ...
noId("id")

Impossible thanks to moot and hiroshima's betrayals.

>memory leak that only occurs if debug information is completely stripped
why

i see, but why is it impossible?
isn't the json api stable?

>c/c++ in a nutshell

It's because of the captcha meme. Note that it would actually be possible to make an emacs mode for it, however.

Basically, gib me Django tutorials

You could fetch recaptcha, use pass, separately handle it, etc

That makes sense.

Someone made a Sup Forums viewer for emacs (no posting though): github.com/desvox/q4

What is the best programming language to write while on LSD? Lisp?

and benchmarked on my potato. (granted this was compiled with a c++ compiler..)

/*
Run on (6 X 2800 MHz CPU s)
08/19/17 22:35:23
Benchmark Time CPU Iterations
-------------------------------------------------
BM_max1 166 ns 167 ns 4480000
BM_max2 163 ns 164 ns 4480000
BM_max3 165 ns 165 ns 4072727
BM_max4 163 ns 164 ns 4480000
BM_max5 162 ns 161 ns 4072727
*/
#include
#include
#include

int max1(int x, int y) { return x > y ? x : y; }
int max2(int x, int y) { return -(x > y) & (x ^ y) ^ y; }
int max3(int x, int y) { return std::max(x, y); }
template constexpr T const& max4(T const& a, T const& b) { return a > b ? a : b; }

static std::random_device rd;
static std::mt19937 gen(rd());

static void BM_max1(benchmark::State& state) {
std::uniform_real_distribution dist(1, 1e10);
while (state.KeepRunning()) {
auto m = max1(dist(gen), dist(gen));
}
}
BENCHMARK(BM_max1);

static void BM_max2(benchmark::State& state) {
std::uniform_real_distribution dist(1, 1e10);
while (state.KeepRunning()) {
auto m = max2(dist(gen), dist(gen));
}
}
BENCHMARK(BM_max2);

static void BM_max3(benchmark::State& state) {
std::uniform_real_distribution dist(1, 1e10);
while (state.KeepRunning()) {
auto m = max3(dist(gen), dist(gen));
}
}
BENCHMARK(BM_max3);

static void BM_max4(benchmark::State& state) {
std::uniform_real_distribution dist(1, 1e10);
while (state.KeepRunning()) {
auto m = max4(dist(gen), dist(gen));
}
}

BENCHMARK(BM_max4);

static void BM_max5(benchmark::State& state) {
std::uniform_real_distribution dist(1, 1e10);
while (state.KeepRunning()) {
auto m = std::max(dist(gen), dist(gen));
}
}

BENCHMARK(BM_max5);
BENCHMARK_MAIN();

This trivializes alcoholism. I haven't been able to write code in months, and now that I'm an alcoholic, I still can't program. I hate programming.

Bit hacks win again.

Captcha needs to be refreshed regularly and needs to be request explicitly. There is no way to handle it separately per se. If you use a pass, you're a redditnigger and don't belong here.

clang appears to be a bit better (with -O3 instead of -O2):

00000000004005e0 :
4005e0: 39 f7 cmp %esi,%edi
4005e2: 0f 4d f7 cmovge %edi,%esi
4005e5: 89 f0 mov %esi,%eax
4005e7: c3 retq

00000000004005f0 :
4005f0: 89 f1 mov %esi,%ecx
4005f2: 31 f9 xor %edi,%ecx
4005f4: 31 c0 xor %eax,%eax
4005f6: 39 f7 cmp %esi,%edi
4005f8: 0f 4f c1 cmovg %ecx,%eax
4005fb: 31 f0 xor %esi,%eax
4005fd: c3 retq

bit hack and std::max is basically the same amount of nanoseconds per. (except std::max is also generic and works on any types that provide a greater-than operator (like std::greater). and you can use it at compile time, unlike bithack.)

but yes, for C the bithack is slightly faster. like 4 billionths of a second faster.

Not him, but are thunderbird extensions not allowed to make arbitrary http calls or something? If not, I don't see any reason why a captcha wrapper wouldn't work

/dpt/ how should I handle reading config settings inside of functions, should I keep the config structure in the global scope and have functions read that global, should every function that needs to read the config have a parameter that accepts a pointer to a config struct, should I have a function that returns a copy of the config struct that's stored in maybe a static variable, should I have functions that return bools for each option (`showDotfiles()`), or something else?

I'm not sure what the best practice is for this kind of thing, I am leaning either towards a global variable, or a static variable inside a function which returns a copy of the config.

I thought the thing we wanted to avoid was cmov*? clang has put it in both of them

yeah python can do dat

>should every function that needs to read the config have a parameter that accepts a pointer to a config struct
IMO this is a good way to do it.

void reads_config(const struct Config *c) {
...
}
void writes_config(struct Config *c) {
...
}

hahaah i'm retarded, nevermind

Yes, std::max probably compiles to bithack but x>y?x:y meme doesn't.
They can last for a while. Just embed an image in each post, that works fine.

As far as I'm aware they can't dynamically update the content of a pending message nor create arbitrary render surfaces.

i see, that's fucking gay

I'm a little dense but I've been using python to try and automate a boring, shitty, manual part of my job.

Basically I can get a report (only in csv) that gives me the receipts and disbursements from a remote site direct from the bank's janky clunky system and a report from the janky accouting system the remote sites run. Since absolute monkeys handle the remote sites the numbers between the bank and the accounting software never match.

The report from the bank give me on each row an account id, date, debit, credit, and a string for the type.

The report from the accounting software gives roughly the same but at a more granular level.

So far I've written a simple program in python at iterates through the csv from the bank and groups items by type and gives me totals. I have a separate program that does the same for the accounting softwate and writes both to the same spreadsheet and sends it off to the monkey to look at each.

It works right now if I pull a single site's report for each or split it manually by site. Since I have to pull each report manually from each system running it for every property one at a time takes a while.

I'm trying to figure out the best way to only pull a single report from each side and have it split out by site automatically.

Do I need to define classes for site to hold the values or some kind of dict/list structure?

>I am no longer developing this package. It's a fully functional browser but lacks a few features I initially planned because I decided not to waste any more time browsing chans then neccesary :^)
wow

It can't give you a compile error for the last one or statically type all the retrieved values

>literally arguing over individual clock cycles worth of performance

I like the idea of it but I don't like actually having to pass it, I feel like it's going to increase the arity of my functions in a weird way, like any function that calls another function which needs the config, will then also need to accept the config as a parameter.
Parent(int){
Child(*config)
}

needs to become
Parent(int, *config){
Child(*config)
}

Is my complaint indicative of bad design? If I switch to this pattern do you think it will force some good refactoring or is it mostly just a preference kind of thing?

I know this is kind of a simple problem but I'm really hung up on it, it feels important but nothing I consider feels like an obvious correct answer.

it may do it for ints, but it probably doesn't. those bithacks won't work for max(3.2, 4.7).

also, why is icc dumb?

Finished the virtual memory and interrupt system for my microkernel. Moving onto device drivers and will have a boot screen done soon.

from collections import defaultdict

report = defaultdict(defaultdict(list)
# now you just run through each report doing shit like
report[FROM][TYPE].append(result)


?

book = {
"author":"Benjamin Pierce",
"title":"Types and Programming Languages",
"id":262162091,
"price":44.11
}
print (type(book["author"]))
print (type(book["title"]))
print (type(book["id"]))
print (type(book["price"]))
print (book.keys())


out


dict_keys(['author', 'title', 'id', 'price'])

That might just do it, thanks I'll give it a shot and report back on my success/failure.

FYI: you can write that as
map(type, book.values())
print(*map(type, book.values()), sep='\n')

No, it's about the "optimizing compilers are always fastest" meme, it just goes to show how wrong they are when they're wrong about even the trivial examples, what about when you're not using an extremely common and well-known pattern?

If you write anything of any consequence in C, you're going to wind up with a few really blown out function declarations. Cost of doing business.

Global config structures are bad unless you're very, very sure nobody will ever want to embed more than one of your thing in a given process. Someone did that with lex once upon a time and it took 25 years to unfuck.

I appreciate the input.

nice didn't know

What about if you write one that masks the floats, then compares sign bit, exponent, and mantissa in that order?

>icc

Disgusting.

That's only for C, and everyone knows C is trash.

It goes for C++ too, except the compiler manages to compile a standard library expression (surprise!) into something remotely close to efficient. It's still shit at anything remotely uncommon and you're best of using bit hacks.

Are dependent types a good meme or a bad meme?

How do they compare to refinement types?

I reran it with all optimizes turned off. just a "release" build with no debugging symbols, frame pointers or any of that useful stuff.
Benchmark Time CPU Iterations
-------------------------------------------------
BM_max1 284 ns 285 ns 2357895 // ternary
BM_max2 287 ns 290 ns 2488889 //

>What are you working on, Sup Forums?
I'm working on a combined assembler-disassembler library for x86.

[citation needed]

Clearly you're not mixing enough uppers with your downers. You might also want to try lsd microdosing and writing lisp.

Working through a Data Structures (in C) book

Is this a good way to delete/free all the nodes in a tree?

void clearTree(Tree t)
{
if(t == NULL) return;
else
{
clearTree(t->Left);
clearTree(t->Right);

free(t);
}
}

Sure, if you want stack overflows.

Yes. Recursion is very suited for trees.

The max depth scales logarithmically. It's likely that he would need a tree that doesn't fit in memory before it stack overflows.

the else is not needed
if it returns, how would it get to the other statements?
other than that, looks solid
don't listen to he's gay. 99.9% of the time you won't have a large enough tree for this to cause any issue
and if it did. you could just increase the amount of branches at each level to make things more efficient stack wise

Would it stack overflow due to the (possibly) large number of recursive calls?
How else could it be done?

>The max depth scales logarithmically
Correction: it scales logarithmically if it's height balanced.

Set a flag matching the node after you traverse it left, operate only on fully marked or leaf nodes. Work backward from the leaves.

>How else could it be done?
make a stack structure of Tree objects
stack them up by level. when you're done, pop and free

that said, if you're doing things right though. freeing a tree won't cause a stack overflow

just keep your trees balanced

10/10

>10/10
thanks senpai

>his language is susceptible to stack overflows
LOL

>the absolute state of /dpt/

>ever using unbalanced trees

...

Thanks famalam(bda)

Yes, always balance your trees, but I was just covering my bases because it's possible.

There is literally nothing wrong with recursion.

unbalanced trees should really be a type error tbqhwy

There is when you have an undelimited depth and you refuse to be tail-recursive.

>There is when you have an undelimited depth and you refuse to be tail-recursive.
why do they do this in MODERN ENTERPRISE PROGRAMMING languages?

>those digits
>that wisdom
noice

If you have unlimited depth then you can't do it with iterations either.
There is literally nothing wrong with unlimited depth.
t. Haskell.

Languages without infinite stacks means that you have to manage the recursion manually using an explicit stack, which is always going to be more complex and less elegant than the simple recursive solution.

Infinite stack doesn't just mean tail calls; many Lisps that have proper continuations consequently allow for the stack to grow arbitrarily, in the same way that other languages can arbitrarily allocate from the heap (until you run out of memory, of course)

you know whats a shame about infinite stacks? practical hardware doesn't have them :^)