/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?

Old thread:

Other urls found in this thread:

en.cppreference.com/w/cpp/language/enum#Scoped_enumerations
gist.github.com/rgrig/b4cdaed3ed9a70dbdb6f158f14b57263
cc.bingj.com/cache.aspx?d=1462716999241&mkt=de-DE&setlang=en-US&w=xEsMxqoa0KJSwMsTh3p_8w2qryykfDvB
twitter.com/NSFWRedditVideo

First for OCaml

debugging kubernetes calls

should have just stuck to servant-generated types, would have avoided these bugs but the haskell kubernetes lib is too heavy to pull in...

First for OCaml sucks.

I want to lick Lain's feets.

First for JavaScript would never be programming

Redpill me on macro usage in C++.

Which option would you choose?

Definitely not first, that's C thing.

enum seems to fit this case best.

the one you forgot: (unless you need to do bitwise operations on it -- then its just a bit extra code to add that).
enum class TileType {
BLANK, NORMAL, WATER
}

>enum class
wat
is this a thing?

Lain is not for sexualising.

use enum class, senpai

Yes. It's a stronger type than "normal" enums, which are basically just integers with predefined values.

Both the first and second are available in C.

I'd take the enum, personally. Fewer things you need to worry about yourself when extending with more types, though I guess if you're using C++ then looks like an equivalent solution.

since C++11.
en.cppreference.com/w/cpp/language/enum#Scoped_enumerations

every day I learn something new about sepples
fuck

You're not alone, but thats the wonders of a language that is basically 4 languages bolted together.

>4 languages
Who are they?

C
Some object-oriented language
Some metaprogramming language
A bit of functional programming too

I know some python, and I'm currently learning some C. Give me some not too big project to practice on, Sup Forums.

this, but to give it names:
CPP (read: preprocessor) +C
C++98-style OOP
TMP (template metaprogramming -- basically a whole dialect of C++)
C++11/14/17 FP

Do people even use TMP any more?

Template metaprogramming is one of the few things that actually make C++ bearable. If you limit yourself to writing "C with classes and RAII", you kind of miss out on what makes C++ a useful "low level" language.

I mean, STL containers alone is for me a reason to choose C++ over C for a new project. And it's not just about generics, I can declare semantically compile-time optimisations which simply isn't possible without templates.

C
Java
Prolog
Haskell

Wow, how can other languages compete with ++C. I'm a /seppler/ now.

>C++
>supporting Haskell
kek

inb4 #include "HsFFI.h"

Yes.. its how the 4th language is built to be fast. It lets you avoid the overhead of the '2nd language' by not having to deal with the OOP costs of double indirection, or bloating the vtable or forcing people to subclass or have to think about abstract / pure interfaces (or the headaches that MI can lead to.).

If you write libraries for other people, you generally either go full crazy with TMP (and drink the boost koolaid), or you just wrap the STL. Both but sit ontop of, or directly use TMP.

>I can declare semantically compile-time optimisations which simply isn't possible without templates.
For example? Templates are certainly useful but I'm speaking specifically about metaprogramming. Using a template for plain old compiletime polymorphism isn't what I'd consider metaprogramming.

Who thought operator overloading is a good idea?

>not simply allowing custom operators in the first place

anyone who doesn't want a separate addition operator for every single thing supporting addition (in any sense of the word)

Stroustrup.

Try doing BigNum arithmetic in Java and tell me it was a bad idea.

Sepples combine their powers (and is right, it's more about C's preprocessor rather than just C the procedural language).

I mean, C is basically a portable assembly. You generally want to avoid writing that unless you absolutely have to.

Java is a neat 90's object-oriented language, but it seriously lacks a lot of modern features which they have to bootstrap on every new release. There's also the whole issue with an indeterministic garbage collector, making it unsuitable for most embedded environments.

Prolog is just a declarative language, it's not directly comparable to the compile-time programming you do with C++, but I'll give you that one. I know very little Prolog.

And for FP, Haskell is mostly limiting itself by being a strict FP language. In my opinion, it's more suitable for learning FP rather than actually use it in a productive environment, because you end up with abstract stuff like zygohistomorphic prepromorphisms.

I'd separate concatenation and addition.

hurrdurr add(a, b)

>was Taught in universities
>hardly popular

>For example?
Well, specialisation for example. Any generic that can be specialised into optimised versions for each type, for example std::sort.

>people complain about D's parens dropping
lmao
echo x

Base converter which writes decimal numbers instead of own digits.
Let's say, hex FEED.17 would look like 15,14,14,13;1,7 or 15'''14''14'13°1'17''

Say you want to compute optimal slabs chunk sizes for a custom allocator based on the target platform, you can compute this at compile time and not have the runtime overhead of figuring out how many bytes you need here or there.

Say that same slab allocator determines which chunks to use based on how often a class of objects are allocated with it. Like, for instance it'll specialize Foo for small objects and pack them tightly. And for large objects it'll do something more cache friendly (like storing their addresses in a forward list -- or similar).

Why do it at runtime when the compiler can generate better code from your templates? Especially when the compiler can help you find tons of errors, instead of maybe catching an exception or an assert.

Another use case, look at how is implemented. Compile-time errors for invalid casts (truncating durations, etc), and automatically conversions based on a computed algebra, so you can easily refactor later if you want to switch to a more fine-grained unit. This type of interface relies heavily on TMP.

But if operator overloading is bad, is method overloading bad too?

>But if operator overloading is bad, is method overloading bad too?
It fucks the ABI by default, so I'll say it's bad. I'd rather have something like the C11 _Generic macro, except obviously not a macro.

you still need an operator for every numeric type &c.

no different from operator overloading

I suggest that you use int::add, float::add, or auto::add

That's not opOverloading

It's garbage, UFCS by itself is cool though.

>It fucks the ABI by default
It just creates a larger ABI, the reason why it's fucked in sepples is because no one ever bothered standardising the ABI.

Implement your custom::add
Not that hard

oh, I just realised what you actually meant

yeah it's fucked and a bad idea

It being taught is a decision of a small group of people. This does not indicate popularity. Popularity is indicated by how many actually use it for work. Barely any.

>upload image of cat in popular blog platform
>click post
>nothing happens
>image is posted

>upload image of nude women
>notification about NSFW, sensitive material, etc
>delete image before posting

how do they do it? AI shit scan every uploaded image posted checking for nude content?

Yes. I didn't find any publicly available neural networks for nudity detection, but there are some proprietary.

You don't "use" SICP for work. You use the concepts in it for work. I agreed that Scheme was not popular (as in was not used a lot for projects), however the concepts that it had reached a lot of people as SICP was taught to them, and that's how closures got popularized.

Machine learning.

>language that popularized closures
>SICP

user, please.

Clearly it was Javascript.

That was my original claim, yes.

Rate my shitcode, /dpt/
def to_new_base(a, base):
b = ""
mult = 1
pos = 0
anew = 0

while (mult < a):
pos += 1
mult *= base

pos -= 1
mult /= base

while (a != 0):
anew = a // mult
b += str(int(anew))
if (pos != 0):
for i in range(abs(pos)):
b += "'"
else:
b += "°"
a -= anew * mult
mult /= base
pos -= 1

return b


>inb4: >python
What's better to shitcode?

Python is great for writing shit code, awful code, and many other bad forms of code

Thanks, I am on the right way then.

Diving back in to C# after having not touched anything related to .NET Framework since 2.0.
C++/C# interop is super comfy.
Unit and integration testing also super comfy.
I'm also really digging System.Diagnostics.Contracts

Welcome back, rajesh

you should look into pex and moles. its a shame other languages haven't picked up things like that.

>C++/C# interop is super comfy.
explain

>not touched anything related to .NET Framework since 2.0
Well, you're in for a ride.

Both the language and framework have come a long way; .NET 2.0 is basically unusable in comparison.

Welcome black, Duqan.

I think what he means is M$ C++/CLI thing, which helps using C# from C++ and vice versa. You write things in C#, write a helper function that converts C# values to C++ equevalent in /CLI and then call them from the normal C++
At least that's how i remember it

>tfw find a screenshot from 2014

it was from adding image display to Yi buffers..

C++ / C# interop is done via p/invoke. And when something is too complex for p/invoke (which is basically placing an attribute of which library the native function is om, plus a C# declaration (using types like IntPtr, etc).) You can use C++/CLI and write a simple wrapper over the native api and compile it to a .net assembly, which'll look exactly like it was written in C# (or F# or VB.net, etc).

example of p/invoke:
extern "C" {
__declspec(dllexport)
void Foobar(int a) {
printf ("%d\n",a);
} }

// C#
[DllImport("Foobar.dll")]
public static extern void Foobar(int a);

// now you can do Foobar(42);

r8 the use of my degree

...

or just copy paste c++ into an unsafe block

gist.github.com/rgrig/b4cdaed3ed9a70dbdb6f158f14b57263

Jesus christ that amount of gymnastics is worse than writing C89

what the fuck?

"enterprise"

>that amount of gymnastics
A single line? Are you ok?

>Assume that there is about 1 toilet for every 3 persons, that existing toilets use an average of 15 liters per flush...

>specify a method call
>specify the path to the dll that the method is in
>gymnastics
owo?

>C++/C# interop is super comfy.
>explain
>here, look: C/C# interop

???

turing-complete type systems are fun

cc.bingj.com/cache.aspx?d=1462716999241&mkt=de-DE&setlang=en-US&w=xEsMxqoa0KJSwMsTh3p_8w2qryykfDvB

lmeow

...

Try non fizzbuzz C/C++ libraries nest time

so?

so, you'll copy/paste all of, say, libwebp into an unsafe block?
or the windows api?
or directx?

you fucking fucker, why did you not just copypaste the code

nonetheless:
>making a separate 3 and 5 condition

C++ is such a mess, it makes me sad.

alright
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr LoadImage(IntPtr hinst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);

picture related, its the documentation for this simple function.
its still two lines with p/invoke.

if you want to use structs, you just make the structs with the right alignment (tons of attributes for doing all kinds of lifetime management, alignment, layouts, etc). There's also tools (for visual studio) that literally automatically generate all this for you. You just tell it which function from which lib you want and it'll create everything for you.

Say you want to pass in a pointer to a C struct:
[StructLayout(LayoutKind.Sequential)]
public struct ALTTABINFO
{
public const int SIZE = 40;
public int cbSize;
public int cItems;
public int cColumns;
public int cRows;
public int iColFocus;
public int iRowFocus;
public int cxItem;
public int cyItem;
public Point ptStart;
}

// variable names don't matter, literal copied from the API
[DllImport("user32.dll")]
static extern bool GetAltTabInfo(IntPtr hwnd, int iItem, out ALTTABINFO pati, StringBuilder pszItemText, uint cchItemText);


Its an okay way of doing it. As for C++, you have to write an extern "C" wrapper, or provide a .NET wrapper in C++/CLI. Most (useful) libraries, that want to be used from other languages already provide a C interface because only D can do FFI with C++.

I think Mono has x/invoke or something, but I don't do C# any more so..

For similarly grouped objects, use an enum, preferably an enum class so things don't get put into global scope.

For regular old constants, use constexpr, as it is typed, and can be namespaced if need be.

int
*cracks knuckles*
main
*sips energy drink*
(void)
*puts on the hoodie*
{
*eurobeat intensifies*
>Enter
Let's CODE

you're really defining everything in main?

>(void)
well, at least you didn't write an empty param list

I just discovered a minor step in the algorithm I'm developing for my thesis which I thought would be trivial is actually NP-hard.

ha ha ha

I-

I c-can get approximate solutions quickly

You know what else is hard?
*unzips list of tuples*

Sorry, I shouldn't have laugh, but it was funny.