/dpt/ - Daily Programming Thread

Good times edition.

Previous thread: What are you working on Jow Forums?

Attached: code_master.png (1164x1738, 2.17M)

Other urls found in this thread:

s3.amazonaws.com/mozilla-games/emscripten/packages/llvm/tag/linux_64bit/emscripten-llvm-e1.38.12.tar.gz':
philipnilsson.github.io/Badness10k/posts/2017-05-07-escaping-hell-with-monads.html
ncbi.nlm.nih.gov/pubmed/7689734
hackage.haskell.org/package/base-4.11.1.0/docs/Control-Monad.html#t:Monad
math.mit.edu/~dspivak/CT4S.pdf
homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf
twitter.com/SFWRedditVideos

NEW /DPT/ RULES

>(A)GPL3 only
>No Windows
>No Nvidia
>No OOP

Lenses are just coalgebra over the costate comonad. What's the problem?

Attached: karen haskell.png (1280x719, 818K)

>What's the problem?
OOPsies

Who else has preordered The Little Typer and is waiting for it to arrive?

Are monads just a meme

Attached: 1534125506988.jpg (961x816, 239K)

dumb frogposter

Attached: find your Monado.png (503x60, 38K)

No. They're extremely useful in programming

Wrong.

Attached: the Monado useless.png (396x70, 68K)

good goy

Have fun writing the same code over and over again, then.

don't @ me ever again

I've been learning Haskell for a month and I still don't understand what a Monad is.

Attached: 7c9.png (790x773, 83K)

A monad is a monoid in the category of endofunctors. What's the problem?

Attached: haskell anime.png (498x707, 368K)

How are they useful?

I mean, monads are a concept, like functions. So all programming languages have them, and even other domains other than programming have monads.

But thats not what what programmers really mean when they talk about monads. Usually they mean some programming language feature. Im not sure how this all fits together but..

0 Types like Maybe are great and immensely useful.
1 Typeclasses are not useful at all.

Can i write code in C or C++ and then compile it to webassembly with emsdk and render it in browser? For example some squares or shit in opengl, would it render in browser?
Also i have problem with installing emsdk, error:
Error downloading URL 's3.amazonaws.com/mozilla-games/emscripten/packages/llvm/tag/linux_64bit/emscripten-llvm-e1.38.12.tar.gz': [Errno 104] Connection reset by peer
Installation failed!

A monad is like an applicative functor but more powerful. What's the problem?

weeb tard

upvoted

philipnilsson.github.io/Badness10k/posts/2017-05-07-escaping-hell-with-monads.html

>So all programming languages have them
Sort of true. It's possible to analyze the computational behaviour of a language by interpreting it in a particular monad, but you're locked into a particular set of effects - which ever ones are primitive in the language. In the few languages where monads are first class, you can program generically with respect to effects, too, and you can declare which effects a function may have to make it easier to reason about your programs.

>monad
It's when you only have one gonad.

Figured I should repost. Have five decent projects I have up on my git, been learning since April, and I'm trying to find open source projects to contribute towards, how long should I wait before trying to apply? I have no problem relocating if I should just put in any and everywhere.

Typeclasses are very useful. They let you write generic, reusable code. You only have to write >=> once, and then you can use it with any type which has a monad instance. Maybe, Either, IO, State, ... .

Im really unconvinced. But I would like to learn, and I would like you to convince me.

Firstly, I doubt there is much value in reusing this code. How often do I want to write "identical" code regarding two different types? Never as far as I can tell. Being different types comes with pertaining to different things requiring different operations. The idea that I am going to re-use code across types seems like a contradiction to me because two different types means two different roles in the code.

Secondly, could you show some code samples demonstrating this code re-use? I really would love to see a use case.

Have you used C?
In C, did you use operators like ==, *, +, etc for more than one type?

I'm not closer to understanding anything.

Ive only done C a little bit.

Yeah, I get that you can use `==` for more than one type.

I think I know what you are saying. Theres a typeclass in all programming languages of things that are comparable, hence why == works on many types. But thats not what excites people about typeclasses. In Haskell you can make your own typeclasses. Why would want to make your own typeclass in Haskell?

The Ord typeclass lets you implement sorting for lists with elements of any type with an instance.

Prelude Data.List> :t sort
sort :: Ord a => [a] -> [a]
Prelude Data.List> sort [3, 5, 4, 2, 1]
[1,2,3,4,5]
Prelude Data.List> sort ["foo", "bar", "baz", "quux"]
["bar","baz","foo","quux"]


You only have to implement sort once and it works for all of them.

Typeclasses can reuse other typeclasses. If you have Ord a, then you have Ord (Maybe a).

Prelude Data.List> sort [Nothing, Just 3, Just 2]
[Nothing,Just 2,Just 3]
Prelude Data.List> sort [Just "foo", Nothing, Just "baz"]
[Nothing,Just "baz",Just "foo"]


There aren't separate Ord instances for Maybe Int and Maybe String. Only one is needed, Ord a => Ord (Maybe a). It's similar for pairs: if you have Ord a and Ord b, then you have Ord (a, b).

Prelude Data.List> sort [(3, "foo"), (2, "bar"), (3, "baz"), (2, "quux")]
[(2,"bar"),(2,"quux"),(3,"baz"),(3,"foo")]

Attached: monad-id-law.png (358x260, 20K)

Yeah but you could have a programming languages, where numbers and strings are odrinal, without having typeclasses as a feature of the language, or any of that Ord => a syntax.

Why have all this fancy stuff to signal that something is ordinal then things could just be ordinal?

>Why would want to make your own typeclass in Haskell?
If we couldn't, then the Haskell ecosystem would likely be in a very sorry state indeed. Being limited to only built-in typeclasses would be crippling. There aren't just a few useful typeclasses. We'd have had to wait for the language to add all the mtl typeclasses, all the semigroupoid typeclasses, Applicative, etc. Allowing users to define their own typeclasses has allowed Haskell to thrive.

One killer use case for defining your own typeclasses is creating your own DSLs à la tagless final. Create your own typeclass with functions, use those instead of concrete ones, and you can run your code with a pure implementation for incredibly easy testing, or with a custom type around IO for production.

Ord is just one example. You could special case it in the language but why bother when we can handle it just fine with a more generic treatment? Parametricity is a powerful technique to help reason about code and reduce possible bugs.

Consider sequence instead. If you have a list of "things in some effect", where effect could be Maybe, or Either, or something else with an monad instance, then it lets you turn it into an effect of a list.

Prelude> :t sequence
sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
Prelude> sequence [Just 3, Just 4, Just 5]
Just [3,4,5]
Prelude> sequence [Just 3, Nothing, Nothing]
Nothing
Prelude> sequence [Right "foo", Right "bar", Right "baz"]
Right ["foo","bar","baz"]
Prelude> sequence [Right "foo", Left "bar failed", Right "baz"]
Left "bar failed"


We can even use it when a function is the "effect".

Prelude> let f = sequence [\a -> a + 1, \a -> a + 2, \a -> a + 4]
Prelude> :t f
f :: Num a => a -> [a]
Prelude> f 3
[4,5,7]


We only have to write sequence once - but we can use it with all these different types! It's not even limited to lists!

Prelude> sequence (Just (Right 3))
Right (Just 3)
Prelude> sequence (Just (Left 4))
Left 4

> creating your own DSLs à la tagless final

This seems like the closest youve come to articulating a real concrete case for typeclasses, and unfortunately I dont know what that is. Thanks for indulging me, but could you explain what that is and why making your own typeclasses are necessary?

I want to make a robot Iori Jow Forums.

Attached: 056858461f4a16f4131b581991214f69.png (420x420, 430K)

> Go doesn't recommend that you use relative imports.
THE UTTER ABSOLUTE STATE OF GO BOOMERS

Attached: 1536063434957.jpg (1000x1000, 80K)

Why are World of Warcraft pirate servers so fucking good? How can somebody just copy an MMO’s server and have not only the performance of the original one but is also able to work with the official game clients?
Just who the fuck develops these servers? They aren’t the work of hobbyists for sure

Why cant I get this code to sort my list properly?

for i in flex:
flextext.append(str(i.points-(round(mean(i.points for i in flex[:15])))) + " PAR - " + i.name + " = " + str("%.2f" % (i.points)))
flextext.sort(reverse=True)
print(*flextext, sep = "\n", file=open("flex.txt", "w"))


i also tried putting the sort command outside of the for loop and it was the same result

Does anyone know of a microcontroller that has an IDE with debugging available on macOS?

PIC18/24/32 use MPLAB X, which is based on netbeans, so it should work fine.

Also literally any ARM-based MCU as long as you have j-link

Sort before casting to a string

Psychic discrepancies between animals and humans. A new light on the "autistic monad"

ncbi.nlm.nih.gov/pubmed/7689734

I thought strings were sortable? Ok good to know. Thank you

based and redpilled

i would have no idea how to fix that clusterfuck. Good luck

a good language should just treat strings as ranges, so all range-based operations can just werk.
["b", "a", "c"].sort.writeln;

sure, just write your own browser

thats not a string

i fucking hate mathsfags

#include

void read(int[], int&);
const int SIZE = 100;

int main()
{
int a[SIZE] = {0}, size; // Explain this line
read(a,size);
std::cout

Attached: 1534193041920.png (1198x432, 1.16M)

yes, design patterns are just a meme

be honest, you only do that when implementing matrix functions

"size" is being declared right there.
the code may be rewritten as

int a[SIZE] = {0};
int size;

Oh my goodness... thank you user. I should rewrite it so that it makes sense to me.

>partial functions
So monads are a solution to a self-inflicted problem?

>you only do that for matrices

Attached: DcSZGc_WsAEVyOD.jpg (800x450, 35K)

That line can be viewed as the declaration of to integers, one is a which is an array of integers and is assigned the value {0} and the other is size which is not initialized.

Here is an example of another declaration of multiple variables

int a = 5, b = 6, c;


a & b are initialized where as c is declared but not initialized

yeah i find myself implementing ==, *, +, on types all the time.

oh wait no i don't

That's probably because you're writing OO garbage. Numeric operations obviously aren't always used but equality and ordering are frequent. Nevermind all the other stuff like showing stuff as a string or parsing it.

i still haven't seen an explanation of monads without (i) a bafflingly shit analogy, or (b) an assumption of prior expertise in category theory, or (three) an assumption of already knowing the details of monads

Monads are monads. You need to see multiple shit analogies before you start to understand.

Stuck on this noob python question

I'm trying to execute functions from client server which worked at first.
when I moved both scripts into classes, I'm getting
"Unbound method must be called with instance as first argument (got str)"

So... how do I target the specific server instance? I'm definitely connected by ip and can communicate

Also... I can make it a static method which fixes the problem, but then I can't access the init variables which I need

Thanks for any help lol

>an assumption of prior expertise in category theory
That's like saying you haven't seen an explanation of the + operator without an assumption of prior expertise in addition. If you don't know category theory (the bare basics) you're biting more than you can chew

let's take an imaginary type in an imaginary language
type T: record {x: int; y: int};


this imaginary language doesn't have generics. how would generics order this without having the programmer actually at some point implement >,

The concept of a monad, by itself, is nothing without the category theoretic definition. Because that's all it is. It's just an interface, but probably the most important interface in computer science.

you need to see the thing in itself desu

Attached: .png (550x550, 286K)

Have you even used Haskell before?
Equality and comparison can be derived by the compiler, else you can write your own instance - e.g. to give it a reverse order or ignore some element. Even if you don't have (parametric) polymorphism you still need to know that the things on the left and right can have the operator applied, so it's just as if you have a less flexible version.

each analogy just causes more confusion. "a monad is like a little box with legs and you put things in the box rofl xD", "a monad is like a burrito rofl xD", nah mate fuck off.

it seems like a monad is just whatever the person explaining it thinks it is, but that is mutually incompatible with everyone else's personal idea of a monad.

i've read a few times that it's a very simple pattern often unknowingly used by programmers of all stripes, but obfuscated with category theory. feels like academic trolling to be quite honest.

The OOPsies are out in force tonight.

This is a way of representing monads in Haskell: hackage.haskell.org/package/base-4.11.1.0/docs/Control-Monad.html#t:Monad

there are a bunch of shitty explanations
in Haskell Monad is a typeclass, like an interface
class Applicative m => Monad m where
return :: forall a. a -> m a
(>>=) :: forall a b. m a -> (a -> m b) -> m b

A -> B is a function from A to B (i.e. it takes A and returns B)
A -> B -> C is like (A, B) -> C
the Applicative m thing means it also needs to satisfy this other "interface" (typeclass)
m a is like m in C++ or Java or C#

why?

Don't forget the laws. They're an essential part of it.

ya i have no idea. I geuss ill make a function to calculate par? i dont know

i've only used haskell to the extent of following LYAH's examples to the letter until the monads chapter. but this was five years ago

>can be derived by the compiler
so what would be the primary sorting key of T? T.x?

for instance, we could write a specific implementation/instance of the monad typeclass/interface for the Identity functor:
instance Monad Identity where
return = returnId
(>>=) = bindId
-- where
returnId :: a -> Identity a
bindId :: Identity a -> (a -> Identity b) -> Identity b

returnId = Identity
bindId (Identity a) f = f a

You can make analogies about many things in computer science that make it easier to visualise it by giving it real world context. "a pointer is like an address on a house"

A monad is something that exists only in computer science and only makes sense if you have understanding of like 5 other fundamental things.

I'm not sure if you know, but people not understanding what a monad is and the only people that do being unable to explain it is a running joke.
There's a Stack Overflow question with 800 points asking it is, and the highest rated answer was basically just agreed to not be helpful at all.
It continues to hold true in this thread some 30 years after Haskell was invented.

In Haskell the derived ord instance is lexicographic, so it would sort by x first and then y
If you wanted other behaviour you could either write your own instance or use a newtype wrapper like Down

Not being able to understand monads in Haskell is like not being able to understand semicolons in C.

This isn't going to help anyone that doesn't have a substantial knowledge of Haskell

Haskell is one of the weirdest and most esoteric languages in existence

>Haskell is one of the weirdest and most esoteric languages in existence
No. No, it really isn't.

It would be helpful if mainstream languages actually looked at any fucking programming language research from the past half a century

I think "pointers" is a better example.

They're everywhere, and you can use them without knowing it but you don't really understand the language until you do.

>guy that originally asked here, I'm about 2% closer to understanding what a monad is

i do not understand

but why?

that's well and good

so where do monads come into this?

No, semicolons are a perfect example, because monads are programmable semicolons.

It seems all the c++ jobs want 5+ years of professional experience. Is it better to just give up and become a web dev instead?

that would be too difficult for the OOPsies

it's the weirdest and most esoteric language which purports to be a Not Esoteric language

Read monoid and monad explains begin pure math definition
math.mit.edu/~dspivak/CT4S.pdf
Monads in programming, monads for imperative,IO,exceptions or concurrency.
homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf

Type systems monads begin for Haskell.

It's hardly esoteric unless you've been writing Java under a rock for the last 15 years.

If you want to understand monads, you must first understand functors. Monads are only a bit more complicated than functors are.

I cant add it to the class so I guess Ill do an outside function?

Shut up you elitist toolbag. It's a hard language, it spits in the face of basic intuition about computer science in a dozen different ways

It's a language with no loops or variables, literally everything is a function, you can define things before they exist, operators are functions, types themselves have "types" and you can have infinitely large arrays if you want

Tell me what you think an "obtuse" language is if it isn't Haskell

This. Applicative functors are a good middle step, too.

>it's a hard language
No it isn't

Thank goodness they largely ignored fart-huffers. I prefer languages to not require expertise in set theory just to print "hello" to standard output.

And a hard language would be what?

You're really bleeding edge.

Not Haskell, obviously.

>In mathematics, a functor is a map between categories
ah, brilliant. categories.