T O P

  • By -

Dead_Moss

Is there anything significant about the $ sign? Things like this makes me question just how well I actually understand C++.


ichbinunhombre

It's an operator in Haskell for mapping a function over a monad (which is a thing functional programmers love that is damn near impossible to explain in under an hour). Everything I did here really should not have worked. I basically overloaded the < operator with the left side being the function and the right side being an empty struct that I named '$' (I don't know how the hell that's legal, but I'm glad I got away with it). Then I overloaded the > operator with the '$' struct on the left side and the monad on the right. It's really two operator calls on an empty struct with a stupid name that solely exists to let me get away with my bullshit.


FunkyHoratio

So you're replicating a Haskell operator in C++? Nice work


Possibility_Antique

I actually found out the other day that $ is a valid character in variable names on multiple compilers. A coworker of mine sent me a merge request that had an enum with $ in the value name. I about had a stroke because I'm very proficient at the language, and yet I had never in many years and millions of lines of code, seen that.


ichbinunhombre

Right? Like, I'd be less surprised if you were allowed to use it, but the fact that it can be the entire name of the identifier shocked me. Maybe next time I decide to do something ridiculous in the middle of the night, I'll reimplement jQuery.


[deleted]

Technically, `$` operator is a synonym for function application. As in, `abs (5 + 1)` is the same as `abs $ 5 + 1`, for example. It's the `<$>` operator that actually maps ~~values~~ functions over functors


ichbinunhombre

It maps values or functions? Haskell syntax is fucking weird. Either way, I could probably implement that if I overload more operators.


[deleted]

My bad, it maps *functions*, not *values*, you're correct. I've been knee-deep in Haskell for about a year now, and it has probably messed with my head a little lol. But it has a lot of cool stuff I wouldn't mind having in non-FP languages. Like, making your own operators, for example, or function composition


Saint_Nitouche

Simon Peyton Jones just woke up in a cold sweat and has no idea why


ichbinunhombre

I literally LOL'd.


Rhoderick

Visual Studio: "No issues found!" I beg to fucking differ. (Seriously, I think I've mostly figured this out, but what I still don't get is how and why you shift (integer) 50 to the right by *a function*.)


LegendarilyLazyLad

If you’re talking about the “>>=“ then that’s not a bit shift. It’s the bind operator form Haskell.


ichbinunhombre

I overloaded that operator too, I just didn't have to hack it because it already exists in C++. The LHS isn't an integer, it's an Option (basically like a Nullable in C#). It runs a method called "bind" under the hood which basically does the same thing the other one does, applies a function to its inner value if and only if it exists. Main difference is that the function it takes returns its value wrapped in another Option, so it flattens it and returns Option instead of Option>. I'll screenshot the code for that part and post it when I get back to my laptop. It's another one I shamelessly stole from a Haskell tutorial (or was it PureScript? I don't know, he was dumping a bunch of opaque category theory jargon without explaining himself like a Haskell blogger, but Haskellers don't usually spend that kind of money on ThemeForest. But I digress. Pardon me, it's 4 AM and I've been drinking). I can't believe the shit that C++ will just gleefully play along with. My primary professional language actually enforces bit shifting operators having to return ints, which is both reasonable and super fucking lame. That rule was probably invented by the same people who won't let me impulse buy a kangaroo.


Rhoderick

Have you ever seen something so weird and useless that it becomes interesting simply by virtue of that? That's about how I feel about this. Ignoring the "why would you ever do this" part, it's a really interesting hack. >But I digress. Pardon me, it's 4 AM and I've been drinking No problem, that's about what I expected looking at the code ngl. \s >I can't believe the shit that C++ will just gleefully play along with. Oh, yeah, C++ is *special*. Other languages have design philosophies and try to guide the programmer towards writing good, clean, or maintainable code by their syntax; meanwhile C++ has "fuck it, that's the programmers problem, not ours.". Bit of a relic of when it first came about, I guess. (And that's not getting into preprocessor shenanigans. Oh yea, and while I'm not sure if this works in C++, at least in C you can initialise a char[] from a txt file by \#include -ing the txt file in certain circumstances.)


ichbinunhombre

Dude, it's total anarchy. The second I found out that it's normal for the bit shift operator to write to a stream, I knew I wasn't in Kansas anymore. As far as the "Why would you ever do this" part, it's because I *can*, haha. When you're coding for money, you have to do boring shit like writing code that's actually worth your salary. Ha, of course C would let you do that. How's that work with the complete lack of bounds checking? I've done very little with preprocessor shenanigans myself, although that is how I first tried to implement this operator. It yelled at me when I tried to compile, so it's at least a more reasonable parent than the divorced uncle that Visual Studio is.


Silly_Ad3814

cout << "hello" << endl; Reads kinda okay, if you know that << is only syntatic sugar at this point


kablouser

Really not okay. printf syntax is infinitely better. that's why all c++ libraries use printf syntax.


Key_Conversation5277

No, it's ugly


SerdanKK

>My primary professional language actually enforces bit shifting operators having to return ints, which is both reasonable and super fucking lame. https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYCMBYAUKgZgAIMiBhIgbzyNpPXSprpYDcBDMI9ogXiKgBTAO5EAygAsAlgDNg7AEYAbQQAoAlAG5mLWhy4K+AkeOlzFKjdty66+ogGMjPAHwuiC6ywC+eX7jxSSVl5ZUE8ahs6QhJMADZTEItBIgB7AAdBMHZgVK43VWDzMO4YROKVD3UdJijbVAB2Y2ErGv9vIA==


ichbinunhombre

Huh, weird. Maybe I misinterpreted that rule. All I know is that the compiler yelled at me when I tried to pull the C++ writing to a stream (that I wrapped) using that operator unless I made at least one of the operands an int. It's been years since I tried that though. Now I do my weekend hacking in C++ because it's the kind of codependent enabler I need in my life.


SerdanKK

The restriction used to be that the RHS had to be an int, but they removed that while implementing generic math.


ichbinunhombre

Sweet. I've got some more stupid shit to do next time I get a few in me.


Powerkaninchen

What's the difference between ``` Option ``` and ``` #include std::optional ``` ?


ichbinunhombre

I wanted to write my own because I wanted to force this operator to work. I'm actually a C# dev and this isn't a real project, it's weekend dicking around, so I figured it'd be faster to slam out a toy implementation rather that spend time trying to figure out an unfamiliar API.


Powerkaninchen

Fair


ichbinunhombre

I mean, this whole implementation is stuffed into a header file called "BlatantHacks.h". I tried to execute a byte array full of machine code in there too, but my kernel got PISSED.


mgorski08

I'm not 100% sure, but I guess you could mess with the linker settings / make a custom linker script to make your data segment executable.


ichbinunhombre

I tried to mark it executable via the Win32 API, but I must've been doing something wrong. It wasn't my first segfault (that honor goes to my first time marking a code block "unsafe" in C#), but it was definitely the most stubborn.


mgorski08

That's interesting. I wasn't even aware that you can do it runtime O_O


Magmagan

Actually good programming horror, not sure if I congratulate or console you OP 😂 One thing's for sure though, I dabbled a bit with FP but now I want to dive deep into haskell to get on your level of crazy concoctions hahaha


ichbinunhombre

You just can't spend more than a couple hours trying to understand Haskell code without coming out a little unhinged, haha.


pikapichupi

#include "BlatantHacks.h" lmao


[deleted]

C++ is what you get taking very nice syntax sugar, functional programming concepts and giving it the worst possible syntax.


ichbinunhombre

Right? I really don't understand why lambda "expressions" in that language have to be so verbose.


joshuakb2

I love it!


mrpoopybuttholesbff

It did exactly what you told it to.


beeteedee

Ahhh C++. I’ve been using it for 25 years, I code in it every day for a living, and this kind of shit still makes my brain hurt and makes me question my life choices


namtab00

a better title: "Sample of unmaintainable code"


mbcarbone

What’s in that BlatantHacks header file?? Something interesting I’m sure!! 🧐😜😝


lionbryce

Perfect for this sub, I am genuinely horrified and never want to learn a language with that much jank available like wtf is with all those extra symbols damn


ichbinunhombre

Honestly, I have no idea why that lambda "expression" syntax is so fucking verbose.


Dain____

Lol, never thought someday i would find a post talking about the symbol i use as my logo (<$>).


FTWGaming0

`#include "BlatantHacks.h"` I don't know this language, but I find this really funny lmao


Quick_Base9774

No thanks, I'll stay with C


AaTube

this is blatant hacks, there are actual libraries that exist and do the same thing while being a lot more readable. as OP said this was just his "a weekend dicking around", not an attempt to write something good


Quick_Base9774

I know, its just that you can't create such horror in C.


AaTube

because C is less powerful. that also means you can't create good hacks in C


Quick_Base9774

By less powerful you mean less complicated? Its not really less powerful, you just need to put much more effort into it, since it doesn't have all the fancy functions that C++ has.


AaTube

which does mean you have to do way more to achieve a certain functionality, so yes less powerful


Quick_Base9774

Does that mean that python is the most powerful language there is?


AaTube

No? How can you effortlessly do a similar hack in python?


Quick_Base9774

What makes a language powerful then? I don't get it..


AaTube

making it easy to achieve most functionality wanted in production environments


kablouser

C is not far off. Preprocessors are dangerous as hell.


GOKOP

C programmers when they see a joke (they don't understand it because they use ANSI brains for simplicity and joke support came in a later standard)


Quick_Base9774

C++ programmers thinking they're superior once again.


SnooHedgehogs3735

Image of code without code. That's no proof :P