/dpt/ - Daily Programming Thread

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

Attached: DPT.png (934x1000, 389K)

Other urls found in this thread:

viewsourcecode.org/snaptoken/kilo/
pastebin.com/K0mf4hgd
kernel.org/doc/html/v4.10/process/coding-style.html#functions
github.com/orangeduck/CPP_COMPLETE
pll.cpsc.ucalgary.ca/charity1/www/home.html
twitter.com/SFWRedditImages

Lisp is the weakest programming language

For me, it's Rust.

C++ is the greatest programming language of all time

First for fuck programming trannies

Fact: OCaml programmers are the most powerful race in the world.

haskell is the most mutable lang of all time

C is a cute

Attached: 1486270671645.png (1000x1400, 1.31M)

I'm studying Braun heaps. They're like binary heaps except instead of being complete binary trees they use a much more straightforward invariant to remain balanced.

A terminal-based text editor in C. It's taking shape. I can view, navigate, edit and save text files now.
Struggled a bit at word wrapping, even backspace keypress is a fucking beast to handle.

have you seen this

viewsourcecode.org/snaptoken/kilo/

Nope, i looked up ncurses and started by trial and error. Tutorials are for pajeets.

I gotta work on a PWA the links that i found are from years ago and the newest ones link to google, to frameworks/libraries.

The google one says "dont do this in prod cuz bad, just use x y and z frameworks/libraries"

I don't want to do it and just use the worst options/practices, anybody who knows this can shed me some light?

k

Attached: 1234321325.jpg (625x483, 62K)

thank you for using an anime related image choice

I've been forced to use flutter and Dart in college for developing Android apps and I want to fucking murder every googler that worked on this shit:
>horrible pain in the ass to install, there is an actual fucking bug open that makes the default installer in their official site NOT WORK without a workaround
>dart language with its absolutely cryptic error messages (even worse than C++) that make it totally impossible to debug
>android emulator crashes randomly (and by randomly, I mean at least 3 times in half an hour)
>no documentation because devs are too busy injecting onions into their veins
>a million other things im too angry to write
fuck this shit so hard

you've learned your valuable first lesson in software development:
never use hipster bullshit
never trust hipster bullshit

Oh no, I had learned that lesson a long time ago, but unless I want to fail the course I have to use this abomination.
I honestly want to drop out, but there's only one year left so I might as well get my degree...

Lisp is the most powerful programming language.

Hi

Is traverse-user here? I have to forgive you, I started learning effectful FP in Scala and I see the beauty of traverse now. Sorry for saying traverse is the same as map!

ok ill take the bait

why isnt this """traverse""" meme just map

*pushes up glasses*
Traverse is actually fold over a monoid of endofunctors, sometimes called a monad.

UHHHHH
Something something effects something the types are wrapped the opposite way.

s/over/into

ur a monoid lol

What's better?

def func():
return simple_oneliner

func = lambda: simple_oneliner

pastebin.com/K0mf4hgd
It does the job, but it's absolutely horrible.
Any comments?

Too long to decipher without any context
What's it supposed to do?

Take a folder of html files, spit out csv files.
What complicates it is that the html files don't have any ids and everything is a table, so a text-based scan needs to be done.

kernel.org/doc/html/v4.10/process/coding-style.html#functions
use functions, faggot.
functions can be used as a form of documentation

no input? wtf

Trying to work myself into a mood to apply for work. My life is dead and empty.

>what's better
being consistent you faggot.
lambdas are variables for a reason. you pass them in as parameters to a function.
If you don't have a function that performs a common task in general but may do different things in different contexts then don't make a lambda for it.

If you're obsessed with functional programming go use a real FP language.

>\\
What are you escaping?

I like Perl for editing text, but if Python's what you know, eh

I have functions. I don't know what else I'd want one for. It's a simple loop. Should I have a function to parse out the tables and shit?
I know it's ugly, but how would functions help?
The former, I'd say. Principle of least astonishment.
Newlines.
I don't think it ever gets used, I just left it in from another, very similar but less ugly script.

would anyone be interested in making a Plan 9 system over a mesh network? Not exactly programming-related but we'd probably need to write a lot of drivers.

I already told you, buttmunch. They're self-documenting when used correctly.
I don't give a shit that your use case causes N levels of nested for loops and if statements.
Use some goddamn functions.
Generators. yield instead of return.
Actually make proper use of dictionaries
Drop whatever poojeet Javaland taught you and learn to take advantage of Python's features properly.

trips demand it

>python
>features
ah yes, like taking away any semblance of freedom
no shit it gets nested like that when you don't even have goto

I have to do a little bit of webdev for a project I'm working on and I'm a mouthbreathing retard

$("img").each(function(){
console.log($(this).attr("alt"));
});

how can I do this except only for things from a certain ul? something like $("list1.img).each

L I M F A O

watch this retard
for tag in your_gay_ass_tag_soup:
for nested_tag in tag:
return do_thing()

def parse_root(root):
for child in root:
yield from parse_nested(child)
def parse_nested(tag):
for child in tag:
yield do_thing(child)

I don't get it, what issue does this solve? What would the code look like without it? I don't even have yield from, this is python 2.7.
Also, how am I misusing dictionaries?

But Mr. Pajeet, the tutorial coder, is going to have a rich portfolio of project he ripped from blog posts, while all you have is your cute little text editor that ends up being just like every text editor to made, with nothing to distinguish your originality from pajeet's babby shit.

Anyway, what data structure do you use to represent your text? I was thinking about this the other day. Obviously you need something that is very fast with random insertions and deletions. Of course, its a solved problem so there is probably a bunch of information out there, but still an interesting problem to figure out from scratch.

you've had literally a decade to port your "code" to py3 holy shit

>using a shite language that's getting sunset in a year
You're doing this to yourself.
Learn functional programming. This would all be so obvious if you already knew the basic elements of functional programming.

Don't make and mutate objects and variables earlier than you need to.
That's a stupid habit from imperative programming styles that obscures the object you're trying to create with massive switch-case-break statements.

You can be extremely expressive in an easy to read way using functions, lazy evaluation (generators), lambdas, recursion, and other concepts that are fundamental to functional programming

>LARPing as a 1970's computer scientist
kek

github.com/orangeduck/CPP_COMPLETE
#include "COMPLETE.h"

/*! Thought it was time to hop on this band wagon */

#define FIZZBUZZ_C($, X) LT($, X, 101)
#define FIZZBUZZ_M($, X) IF_ELSE( NOT(BOOL(MOD($, X, 15))) , FizzBuzz \
, IF_ELSE( NOT(BOOL(MOD($, X, 3))) , Fizz \
, IF_ELSE( NOT(BOOL(MOD($, X, 5))) , Buzz \
, X \
)))
#define FIZZBUZZ_U($, X) INC(X)
#define FIZZBUZZ_E($, X)
#define FIZZBUZZ() JOIN(RECR_D, 0)(1, FIZZBUZZ_C, FIZZBUZZ_M, FIZZBUZZ_U, FIZZBUZZ_E, 1)

Post optimal python
from typing import ClassVar, TypeVar, Callable, List, Union, Mapping, Sequence
T = TypeVar('T')
V = TypeVar('V')

def identity(x: T) -> T: return x

def flatmap(o: Union[Mapping, Sequence], var_type: ClassVar[T], func: Callable[[T], V] = lambda x: x) -> List[V]:
values = o.values() if isinstance(o, Mapping) else o
for val in values:
if isinstance(val, (Mapping, List)):
yield from flatmap(val, var_type, func)
elif isinstance(val, var_type):
yield func(val)

foo = {'ura': 'fgt', 'no': {'u': ['1',2,'3',4,'5']}}
print(list(flatmap(foo, str, identity))) # ['fgt', '1', '3', '5']

2.7 is preferred for a lot of math/sci/ml stuff

I refuse to believe that the person who wrote the horrible code in is capable of "mat/sci/ml stuff"

>escaping
I was making a horrible reference to the "who are you quoting" meme for memearrows

>2.7
>math/sci/ml
wrong
I use Tensorflow, Numpy, Pandas, PyTorch on a daily basis.

If 2.7 is preferred for "math/sci/ml stuff" then it's because everyone you work with is a pathetic dinosaur who can't learn new things

On the contrary I work in a research environment and those pathetic faggots couldn't write readable code to save their lives.

really makes me think

Why would I ever use a macro instead of a function in C?

Apart from some work shit, trying to learn Rust from the online docs/book. Seems pretty cool so far and I like it.

broad question

me too

Preprocessor vs compilation stage. It allows you a lot of neat tricks (or hacks, depending) you can't do with the language itself at runtime, e.g. generics. Generally it's useful for little things, especially ugly boilerplate, where you don't want to have to write an entire function and hope it inlines and then write tests for it. You probably use macros from libc without even realizing it.

Beautiful :,)

Just had this question in an interview

>Give an example of a programming language that isn't Turing complete

python according to that faggot zed shaw :^)

C processor. regexes, certain types of SQL, theorem provers, etc.

*C PREprocessor

CSS 1.0

there are none

nice markup languages you got there

>markup languages
None of what I posted is a markup language. You could potentially argue that they're not covered under the term "programming languages" because they're special purpose rather than general purpose.
If you want a non-turing complete general-purpose language, you just have to look at one designed to guarantee all programs terminate. Such as weird shit like pll.cpsc.ucalgary.ca/charity1/www/home.html which is probably much less practical than my example of a theorem prover.

Thoughts on throwing exceptions instead of using ?

Exceptions are made to be handled. If you're just terminating the program at an error condition then there's no benefit over assertions.

>damage control
stop playing hr's stupid games

why wouldn't you be able to prove that a Hello World program will terminate

>using assertions outside of development

halting problem

C is such a time consuming language to program in, but damn if it isn't the most satisfying to get correct.

the halting problem is about programs in general
it says that you can't write a single algorithm to generally determine whether any given application will terminate

if you knew enough about the program and had some invariants and special properties which the language provides you, you may be able to determine other things of interest.
Like whether or not your program will deadlock, or ever experience an error caused by a dereferenced dangling pointer.

Can I use assembly macros in inline assembly in C?

>implying you can get C correct

Answer me this, which is better:
>pass in and modify - caller might have to free depending on what was passed in
void doStuff(char *str) {
//do stuff with str
}


>static - don't free or bad things happen
char* doStuff() {
static char temp[somenumber];
//do stuff with temp
return temp;
}


>heap - caller has to free or memory leak occurs
char* doStuff() {
char *temp = malloc(somenumber);
//do stuff with temp
return temp;
}


C++ solved this problem. Return std::string by value and it gets copy elided away and/or short string optimized. But C++ sucks lmao

You can always prove that a specific, given program will halt or not.

just stop posting

you can't prove whether I'll stop posting or not

You can always prove that a specific, given protein will halt.

I can tell you've never written a program longer than 20 LoC. That is not a problem since YOU know what YOU need for YOUR program since YOU wrote it.

First one is obviously best, and you should do that in both C and C++ so the caller has control over how things are allocated.

There's a few times I used them at work, we did lower-level stuff.

#define t4_round_nearest_uint(x,n) ((((x) + ((n)/2)) / (n)) * (n))
#define t4_round_up_to_multiple(x,n) \
(((((x) + (n) - 1)) / (n)) * (n))


The above was useful for faxes. Sometimes "204x196" meant "200x200" for historical reasons, or I'd want for example, 5, padded out to 8 or 16 or some other multiple (converting binary widths to 8/16 widths)

There was another case I don't have, where it was a rather large block of printing data, but there were a few tiny bits of logic changed if the program was ran as a single program vs two programs. Specifically, we looked for running file handle count for a "server"+"client" or single-program application, and if you're you're checking your own filehandles, when you open /proc/, if you are your own program, checking your own filehandles creates one more file handle - the one you're checking /proc//*, while if you're checking another program, it's only the file descriptors in the folder. The other bit was just labelling. I'd "pass" in a string to the macro, so if we're one process, it'd say "the process has x file descriptors" but if was dual process, it would run the descriptor checker twice (for server+client), and use two different words.

The original macros used lots of \ to interrupt newlines, and when we ran part of our build process, the macro would get substituted for one long line, so there was a subtle drawback, but it worked fine.

I'd argue a programming language SHOULD BE by definition Turing complete, and give a counter-example as the one math axiomatic set which can quickly determine if a process terminates or not - but it can't do multiplication.

I honestly LOVE C, but damn can it be intense to get everything logically perfect (to the extent you permit your functions/code to be used cough cough).

Attached: utilitymacros.png (1031x548, 35K)

The first would be preferred, generally speaking, but it would depend on the use case:

1. Most important, what're the memory expectations? In our code, memory must be consistently used, and almost religiously, NEVER dynamically allocate memory, because our code could be used on systems with once-only allocations (real-time or specifically hardened real-time systems).

2. C has no object orientation/inheritance, so the third case isn't likely to happen at all. It's far to easy for someone to fail to free the memory, though if programming C in a slightly object-oriented style, the third is permissible if an object is removed/deallocated. Looking at a library use case here, however, the pointer to the string should NEVER be accessible. Thus the only case is private, which is object orientation.

The second case is just bad, because it requires the string always be allocated in memory. In a very small, very predictable application, this might be permissible, but it'd still be a really not-good style. If you see static allocations in functions it's a sign of bad logic. Either a "global" or different algorithm should be used.

Here's an easy approach: if the caller is supposed to free(), return a char*. If they're not, return a const char* instead. Why? Because free() will not accept const char* cleanly without a cast. It's a good way to distinguish what should be free()d and what shouldn't - if you're not supposed to write to it, you shouldn't free() it either.

For the reverse, such as your case 1, the function should only take char* if it's supposed to modify or take ownership of the string (like with std::string& or std::string&&), otherwise it should always take const char* and assume no responsibility. If it needs to store the string it should copy it (like with const std::string&).

'const' is not just a tool of annoyance, use it where appropriate. And if you find yourself needing to remove const with casts, rethink what you're doing.

No love for Ada?

Agda.

Solve the Collatz Conjecture

tfw you learned scala instead of idris

sad_frog_picture.jpg.png.gif

Protip: programming won't fill the void inside you.

Is there a nice way to only change one character in a text file using C++ or shall I just copy it into a string, change it there and rewrite the file with it?

Mmap.

Find the character, seek to it and override it

But it will distract me from it.

math isn't ready for such problems

something that works on all the popular OSs, I'm doing this for a Windows user
>override it
but how?

Attached: 1525620634419.jpg (1920x1080, 310K)

void f(int k) {
while (k != 1) {
if (k % 2)
k--;
k /= 2;
}
}

How do you prove this ends for any k > 1?

That's a proper use of exceptions.

k /= 2 decreases k