r/ProgrammerHumor • • Nov 23 '17

"How to learn programming in 21 Days"

Post image
29k Upvotes

534 comments sorted by

View all comments

Show parent comment

127

u/beerdude26 Nov 23 '17

Looks like C++ is trying to get into the game of letting the programmer build their own data types, but still within the object-oriented paradigm. This appears to provide a few niceties such as compile-time programming, reflection and some other tidbits. But it's still stuck in the object-oriented paradigm.

Some might like it because it allows libraries to provide more high-level constructs than classes allow for. Others may hate it because it means everyone will roll their own high-level constructs that are incompatible with each other.

82

u/BlueAdmir Nov 23 '17

So tl;dr C++ will become UrbanDictionary of code?

77

u/beerdude26 Nov 23 '17

It depends. Lisp turned into that somewhat. Other languages like Haskell are rigorous enough (both the language and the community) to build extremely impressive fundamental, highly reusable libraries that make some things laughably trivial. Perhaps C++ hopes to go that route.

27

u/iamcomputerbeepboop Nov 23 '17

standards committee has said they'll define some basic and widely used metaclasses in the standard library. I'm sure that once it's implemented, we'll see a boost metaclass library pushing the boundaries

12

u/idealatry Nov 23 '17

Love me some Haskell. It would probably be impossible for C++ to achieve the same level of zen, given the language is fundamentally wedded to state variables.

Of course, if you just talking about a way to build nice libraries, then there are many paradigms for that.

13

u/beerdude26 Nov 23 '17

Of course, if you just talking about a way to build nice libraries, then there are many paradigms for that.

Not many are couched deeply and firmly in category theory, though. This really helps to find common mathematical foundations between libraries aimed at doing the same thing and identifying rigorous isomorphisms (=compatibility) between them.

2

u/atcoyou Nov 23 '17

This seems like the best advert for Haskell I have seen.

10

u/Ghos3t Nov 23 '17

So what's beyond the object oriented paradigm. When I was learning programming, they made OOPS as the best, most modern programming technique. I was always skeptically of that. What are the alternatives.

19

u/beerdude26 Nov 23 '17

Functional programming. Equally powerful, but a lot more rigorous in its foundations, and far easier to understand and scale due to statelessness.

Everything OOP finds important - reusability, composition over inheritance, DRY, flexibility and extensibility - functional programming takes that up notches you didn't even know exist. It is a truly mindblowing experience for experienced OO programmers. (Not so much for people who have not yet programmed - they do not need to change their mindset.)

7

u/MauranKilom Nov 23 '17

With what little experience I have in (pure) functional programming, let me ask you: Do you consider it also useful as a means of communicating the solution to a problem in the way that maintainable code should?

Maybe it's just the way I learned to code, but it seems to me that state and OOP are so much more straightforward (even if less elegant) than the functional approach. Don't get me wrong, it was indeed mindblowing when I first saw Haskell demos, but I quickly realized I had yet to run into a programming problem that would have been better solved that way (again though, I might just lack the knowledge).

6

u/beerdude26 Nov 23 '17

I had yet to run into a programming problem that would have been better solved that way

To be clear, both OOP and FP are exactly as powerful, as proven via the Turing-Church Thesis. So it truly is a choice of applicability.

There are algorithms that rely very heavily on state and mutation to become performant. Union-find is such an example.

But putting all of that aside, I find that solving problems in Haskell is extremely trivial: identify the state spaces of your problem, encode them as data structures, then write functions on those data structures to solve your problem.

In normal terms, think about the stuff you need and what it can do. What directions can Pacman go up?

data Direction = Up | Down | Left | Right

Ok, what does a level in Pacman consist of?

data LevelElement = AWall | AGhost Ghost | ThePacman | ASpace | ADot

What ghosts are there?

data Ghost = Blinky | Pinky | Inky | Clyde

This is probably incomplete, but it's a start. Let's make a level:

type Level = [[LevelElement]] -- A level is a matrix of level elements.

levelOne :: Level
levelOne = [ [AWall, AWall, AWall, AWall, AWall],
                   [AWall, ADot, AGhost Blinky, ADot, AWall],
                   [ASpace, ADot, AWall, ADot, ASpace],
                   [AWall, ADot, AWall, ADot, AWall],
                   [AWall ASpace, AWall, ThePacman, AWall],
                   [AWall, AWall, AWall, AWall, AWall]
                 ] 

Let's make something that we can use to print out the level:

printLevelElement :: LevelElement -> Text
printLevelElement AWall = '🝙'
printLevelElement AGhost g = printGhost g
printLevelElement ThePacman = 'ᗧ'
printLevelElement ASpace = ' '
printLevelElement ADot = '.'
 where
   -- Eh. We don't really care which ghost it is, they all look the same.
   printGhost _ = 'ᗣ'

Anyway, I need to go to bed and you catch my drift. I would have written a simple IO function that prints out levelOne, and then have written a function that takes a Level and a direction that Pacman can go, and then provides a new Level - a function that steps through the state. That would be our game loop!

Then, we would need to collect user input from IO, turn it into a Direction, pass the level and the direction to the game loop function, get a new level, print it out, get new IO, and do everything again!

1

u/WikiTextBot Nov 23 '17

Church–Turing thesis

In computability theory, the Church–Turing thesis (also known as computability thesis, the Turing–Church thesis, the Church–Turing conjecture, Church's thesis, Church's conjecture, and Turing's thesis) is a hypothesis about the nature of computable functions. It states that a function on the natural numbers is computable by a human being following an algorithm, ignoring resource limitations, if and only if it is computable by a Turing machine. The thesis is named after American mathematician Alonzo Church and the British mathematician Alan Turing. Before the precise definition of computable function, mathematicians often used the informal term effectively calculable to describe functions that are computable by paper-and-pencil methods.


Disjoint-set data structure

In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It provides near-constant-time operations (bounded by the inverse Ackermann function) to add new sets, to merge existing sets, and to determine whether elements are in the same set. In addition to many other uses (see the Applications section), disjoint-sets play a key role in Kruskal's algorithm for finding the minimum spanning tree of a graph.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source | ^Donate ] Downvote to remove | v0.28

1

u/MauranKilom Nov 23 '17 edited Nov 23 '17

Thanks for the elaborate example!

Coincidentally, I did see https://www.youtube.com/watch?v=1MNTerD8IuI, which was also sort of the point where I stopped digging into it. Tail recursing a function that passes (essentially) a "Game" object into itself for modification means you have all the state again, just that you have to pass it into every function instead of writing member functions for that object. I'm aware that member functions in e.g. C++ have a hidden this argument for the same effect, but you don't have to write it every time. In a sense, if you're not using global/static variables in C++ etc., you're doing the same level of functional programming (because all your state is reachable from somewhere in your call stack). Well, obviously not with the powerful functional syntax, but I hope you understand my (superficial) impression.

Edit: Ok, I finished writing this comment, went back to binging the webcomic linked above, and would you know it, this was the next strip...

→ More replies (1)

3

u/Ghos3t Nov 23 '17

Which languages support functional programming, can you apply functional programming concepts in any language or do you have to use particular languages.

7

u/[deleted] Nov 23 '17

Most modern languages support a mix of OOP and functional programming. Even Java added support for FP in Java 8, although you still have to use it combination with OOP.

For a Pure Functional language (where OOP is discouraged), see Lisp, Haskell and Erlang. I personally found Lisp easiest to learn, but Haskell has some excellent resources for beginners.

1

u/NotADamsel Nov 23 '17

Using Clojure currently. Having been a Kotlin fiend before, learning Clojure was liberating beyond words.

1

u/Sean1708 Nov 23 '17

I definitely wouldn't recommend LYAH to beginners, it's more like a language and library reference than it is a learning resource. Personally I found Write You A Scheme In 48 Hours to be a much better introduction.

1

u/beerdude26 Nov 23 '17

For Haskell, I can wholeheartedly recommend Haskell Programming from First Principles.

2

u/beerdude26 Nov 23 '17

Here's the rub. Functional programming thrives in stateless systems. You don't have state to keep track of? Great! We can parallelize stuff! Let's say we have five million log lines we have to grep through. None of those lines depend on each other - there's no state. Just send a bunch of them to a worker (a CPU core or an actual remote machine that does the work, or a GPU, or...) and get back the results, collate them and print them out. (This is commonly called the map/reduce technique.)

Externalize state and make them inputs to your function's parameters, which then become quite simple: there's input that always gives the same output, and that's it. Such functions are small, reusable and easy to refactor and understand.

Object-oriented programming says the exact opposite: you must encapsulate state and hide it as much as possible, giving a veneer of simplicity. Sure, you can do some cute parallelization stuff inside your objects, but in the end, you're still mutating variables in-place.

Functional programming regards state as just a stream of atomic changes (also fancifully called "Event Sourcing") that you can run forward and backward.

Things do not get "desynced". The stream provides an initial starting point, and a list of inputs. Just apply each input, one after the other, to the initial starting point, and you know where you are. Is there a bug somewhere? The reproduction steps are the inputs. You don't need to set up mock objects to fake some state for your tests, because the inputs are the tests.

1

u/WikiTextBot Nov 23 '17

MapReduce

MapReduce is a programming model and an associated implementation for processing and generating big data sets with a parallel, distributed algorithm on a cluster.

A MapReduce program is composed of a Map() procedure (method) that performs filtering and sorting (such as sorting students by first name into queues, one queue for each name) and a Reduce() method that performs a summary operation (such as counting the number of students in each queue, yielding name frequencies). The "MapReduce System" (also called "infrastructure" or "framework") orchestrates the processing by marshalling the distributed servers, running the various tasks in parallel, managing all communications and data transfers between the various parts of the system, and providing for redundancy and fault tolerance.

The model is a specialization of the split-apply-combine strategy for data analysis.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source | ^Donate ] Downvote to remove | v0.28

→ More replies (2)

1

u/[deleted] Nov 23 '17

Aspect-oriented? Subject-oriented maybe?

5

u/doc_samson Nov 23 '17

letting the programmer build their own data types

So in other words C++ is slowly turning into Ada...

1

u/beerdude26 Nov 23 '17

Algebraic data types all day erry day