News for hackers. Stuff that matters.
Re:From design patterns to category theory by sandGorgon on Thursday October 05, 2017 @04:41AM (#15407563)
The problem with category theory and monads, etc are that they are inaccessible for some of the popular tools. E.g. javascript, python, ruby . It invariably happens that learning these patterns needs you to learn haskell, lisp or erlang. Which is where accessibility breaks down.

If there was a conceptual framework that bridges these higher order designs to accessible languages .. even if partially so, that would be killer.

P.S. I love what dg for python has done - http://pyos.github.io/dg/

>Annoy advocates of the category theory!

>With Haskell's syntax but none of its type system, dg is the best way to make fans of static typing shut up already

Re:From design patterns to category theory by lolive on Thursday October 05, 2017 @04:52AM (#15407607)
I have learnt monads via the absolutely MARVELOUS book by Brian Lonsdorf. cf https://github.com/MostlyAdequate/mostly-adequate-guide

I agree that Haskell is built around those concepts. But the point of learning category theory is to change your mindset when programming. And even in Javascript, it is super useful. (basically because, while trying to learn category theory, you become fluent in functional programming ).

Re:From design patterns to category theory by sandGorgon on Thursday October 05, 2017 @04:55AM (#15407622)
pretty cool. Especially this.

>Part 3 will start to dance the fine line between practical programming and academic absurdity. We'll look at comonads, f-algebras, free monads, yoneda, and other categorical constructs.

I myself like this - https://github.com/valentjedi/ddd-dynamic - but I'm beginning to think maybe all of this is not the right paradigm for programming in languages like javascript or python. The success of tools like React or RxJava in Android makes me think that the tools define the paradigm and not the other way around... no matter how much we want it to be.

Re:From design patterns to category theory by BoiledCabbage on Thursday October 05, 2017 @11:35AM (#15409999)
I'm not certain, but isn't RxJava just a specific inplementation of a monadic pattern with IObservables?

I thought the original Rx was a Haskeller saying "Hey a 'dsl' built on a monad would be really powerful here in C#" and implemented it. Then it spread to other langs from there.

Re:From design patterns to category theory by tasuki on Thursday October 05, 2017 @05:18AM (#15407715)
Not sure what you mean by "inaccessible", but you can use monads pretty much anywhere.

In my opinion the biggest hurdle is the fact that getting to unrestand purely functional programming is pretty difficult. You start with monads, but quickly find out they do not compose. Move on to monad transformers, good for the simple stuff, but then you hit another wall because they don't compose either! Move on (up!) to tagless final and/or free monads. There are _a lot of_ concepts that don't have any equivalent in "the real world". The gang of four design patters are a child's toy in comparison.

Re:From design patterns to category theory by crimsonalucard on Thursday October 05, 2017 @10:33AM (#15409483)
He means that there's no type syntax to assist with monad creation or usage which indicates a sort of incomplete understanding of the concept.

As an example, a maybe monad in python can be implemented by having a monadic function return either:

  ["Just", 1]
  ["Nothing", None] 
and the bind operator (>>=) would be:

  bind = lambda a, f: a if a[0] == "Nothing" else f(a[1])
You should also understand that a list itself can be a monad. Monads are just burritos where ANY abstraction/design pattern can be the wrapping, it does not need a haskell type system to wrap something. Albeit the type system in haskell does really help you grok the concept.

Weirdly how someone chooses to implement a monadic value and the bind operator suffers from the same problem as what this article writer complains about in OOP design patterns. Both monads and design patterns really depend on previous understanding of a pattern, and a variation on how someone chooses to implement the bind operator or a design pattern can really throw off people.

Here is an example of an alternative variation of the "Maybe" monad in python, for the return values:

  ["Only", 1], None
And a variation on bind:

  bind = lambda a, f: f(a[1]) if a is not None else None
Re:From design patterns to category theory by runeks on Saturday October 07, 2017 @03:05PM (#15425155)
> You should also understand that a list itself can be a monad.

A list is not a monad — it’s just a container of values, ie. a functor. A monad is a data structure defines a computation in a specific context, and allows composing these in a sequential manner. For example, the Haskell IO monad is a computational context in which you can do input/output, and all of these IO operations are represented using a data structure of the type “IO <return_value>”.

For example, the function “readLine” has the type “IO String”, and represents a computation that reads a line from standard input and returns it as a string. At runtime, evaluating this value will result in the runtime system asking the user for some input, and at compile-time this is represented as the string (that the user enters at runtime) inside the IO monad.

Re:From design patterns to category theory by tome on Saturday October 07, 2017 @06:19PM (#15426007)
> A list is not a monad

Huh? List is very much a monad.

Re:From design patterns to category theory by runeks on Sunday October 08, 2017 @10:16AM (#15428282)
You're completely right. I had no idea.

But I can't make sense of the definition of >>= for the list in Haskell, which is:

    xs >>= f = [y | x <- xs, y <- f x]
It seems to imply that I get a list in return when I do xs >>= f, but I need to do the following in order for it to work:

    [1,2,3] >>= return . (+1)
Re:From design patterns to category theory by crimsonalucard on Monday October 09, 2017 @02:38PM (#15436315)
You're probably use to Monads that wrap a single object, like the maybe monad. Monads can wrap multiple objects or anything. This is the case for a list.
Re:From design patterns to category theory by tome on Sunday October 08, 2017 @02:39PM (#15429361)
Perhaps this might help (or perhaps not!)

    > [1,-2,3] >>= (\x -> replicate (abs x) x)
    [1,-2,-2,3,3,3]
Re:From design patterns to category theory by sandGorgon on Thursday October 05, 2017 @10:56AM (#15409657)
this is a super interesting reply - you kind of translated these monadic concepts to python.

Is there a resource for this ? or good old intuition ?

Re:From design patterns to category theory by crimsonalucard on Thursday October 05, 2017 @11:21AM (#15409886)
Nah no single resource, it took me a lot of time to understand monads, going over many resources.

Just remember Absolutely ANY design pattern / data structure can be a monad as long as you can get an internal value out of that abstraction and define a bind operator to compose monads together.

This rule tells it all:

  (>>=)            :: m a -> (a -> m b) -> m b
m is the abstraction wrapper and a is the internal thing that is wrapped. You can wrap it in a type string, a list. Even like a binary tree can be a monad.

You can define the bind operator to do whatever you want, it just needs to follow the type signature above and be associative.

Re:From design patterns to category theory by crimsonalucard on Thursday October 05, 2017 @01:53PM (#15411069)
Oh and nothing was translated. A monad is a general concept applicable to almost any language. Deep understanding of a monad is to understand it outside of the haskell ecosystem.
Re:From design patterns to category theory by sandGorgon on Thursday October 05, 2017 @08:46AM (#15408670)
genuine question - can this be done in python in a production ready manner ?

I'm have been trying to answer this question for quite some time. In Haskell, writing monads or applying category theory is part of the idiomatic language. Not so in js or python.

for example, if you want to apply haskell like concepts in javascript, the closest thing I know is purescript (http://www.purescript.org/) or Bucklescript. So you need to switch over to a different language to achieve the richness of these concepts.

I'm hardpressed to apply these concepts idiomatically to the mainstream languages. Compare this to design patterns or OO, or reactive programming (via rxjs or whatever) - which are so accessible that they are now the reason to learn a particular language!

Re:From design patterns to category theory by lmm on Thursday October 05, 2017 @10:20AM (#15409364)
> genuine question - can this be done in python in a production ready manner ?

It can, but without a type system you lose most of the benefits. You can put monads in there but if you refactor fearlessly the way you would in Haskell (without tests), you'll get production bugs.

> Compare this to design patterns or OO, or reactive programming (via rxjs or whatever) - which are so accessible that they are now the reason to learn a particular language!

Isn't that the same thing? Monads etc. are the reason to learn Haskell.

Re:From design patterns to category theory by tasuki on Thursday October 05, 2017 @10:13AM (#15409310)
Wrt your question about production-ready manner: I don't know.

However, there are many libraries which make functional programming in Python seem viable. See eg: https://pypi.python.org/pypi/PyMonad/

Re:From design patterns to category theory by crimsonalucard on Thursday October 05, 2017 @11:54AM (#15410164)
Functional programming is just imperative programming with heavy heavy restrictions. So yes this can be done, as long as you fully understand what functional programming really is...

A functional program is simply a program that is made up of a single mathematical expression. Complexity is achieved in functional programming by composing smaller expressions into a single big expression. It should be possible to write your entire functional program as a single expression or statement (not saying you should do it, but it should be possible). That's it. That means all your python functions should be a single expression only (use lambda always instead of def; or if you must use def make sure all your variables remain immuted)

If you want to apply like category theory to it, then just make sure your python functions always stick to specific types. Don't let a function take in either a string or an int and return a list or a iterator or any bullshit like that. Keep the typing consistent... If you define all your functions to behave mathematically like only taking in a string and only returning an int, not an int or a None... then you are following category theory.

So nothing like this:

  def someFunc(value):
     if value == "hello"
        return 1
     elif value == 123:
        return [1,"world"]
or this:

  blah = lambda a: "hello" if a is None else 304
If you wanted automated type checking at run time you can use a decorator to check the types of variables going into the function and coming out.

If you try this style at work, people will complain haha, I don't recommend it

Re:From design patterns to category theory by marcosdumay on Thursday October 05, 2017 @12:41PM (#15410565)
On the other hand, as soon as you know how to apply the concepts of CT, learning Haskell mostly is a matter of syntax. So, why not?

Besides, I don't think you would gain anything by using CT concepts in a language that won't resolve type polymorphism for you. It will lead to a mess of a code, where you'll have to make everything explicit.

By the way, great language. an await command in a where scope is just phenomenal. Between seamless lambdas, interdependent functional declarations, and forced async I don't think it missed any language at its trolling.

Re:From design patterns to category theory by edem on Thursday October 05, 2017 @04:43AM (#15407571)
How can you call lisp an inaccessible language? It has less syntax than anything else. It is as simple as a language gets.
Re:From design patterns to category theory by kryptiskt on Thursday October 05, 2017 @06:20AM (#15407988)
Brainfuck has even simpler syntax and is even more inaccessible. There's way more to languages than syntax.
Re:From design patterns to category theory by edem on Saturday October 07, 2017 @06:11AM (#15423156)
Brainfuck it unintuitive, but Lisp is. You can learn the lambda calculus in 10 minutes. You can'c tompare the two.
Re:From design patterns to category theory by hoorayimhelping on Thursday October 05, 2017 @07:22AM (#15408263)
How many stack overflow questions and answers are there for lisp(s)? How many for Java? How many applications on the web are written in a lisp? How many are written in Java (or Python or Ruby or PHP)?

There's a lot more to accessibility than how easy the syntax is to grok. This is just one simple and limited example.

Re:From design patterns to category theory by flavio81 on Friday October 06, 2017 @01:33PM (#15418589)
>How many stack overflow questions and answers are there for lisp

Yet there are a ton of books written about Lisp in all kinds of topics and applications. Not to mention really well-written tutorials like "Practical Common Lisp".

Plus, the documentation/reference of the language itself is comprehensive and well written, comparing favorably to the documentation for most programming languages out there.

Re:From design patterns to category theory by flavio81 on Friday October 06, 2017 @01:35PM (#15418612)
>How many applications on the web are written in a lisp?

You are using one right now: Hacker News is written on a Lisp: Arc.

Also, the first HTTP 1.1 compliant server and used by the W3C to debug the HTTP 1.1 reference implementation, was written in Common Lisp.

Re:From design patterns to category theory by 201709User on Thursday October 05, 2017 @06:30AM (#15408032)
It's better to have 50 keywords than 1000 nested brackets.
Re:From design patterns to category theory by empthought on Thursday October 05, 2017 @07:04AM (#15408196)
Personally I don't see the advantage of )}]]})} over ))))))).
Re:From design patterns to category theory by kazinator on Thursday October 05, 2017 @04:09PM (#15412213)
It's more time consuming to close )}]]})}. With ))) we just repeatedly type ))) until the cursor jumps to the target opening paren we are trying to close. With a mixture of different parens we have to interrupt what we are doing and determine the correct parens which will continue the closing sequence.
Re:From design patterns to category theory by flavio81 on Friday October 06, 2017 @01:38PM (#15418645)
>With ))) we just repeatedly type ))) until the cursor jumps to the target opening paren we are trying to close.

Exactly.

Or, the IDE can close all the parens for you: SLIME command "slime-close-all-parens-in-sexp", which you can map to whatever key combination you want.

Re:From design patterns to category theory by Udik on Thursday October 05, 2017 @08:05AM (#15408430)
I think there are people who value completeness- they enjoy working on systems with many rules tailored for specific use cases, and like accumulating knowledge about which rule is the correct one to apply in each situation; and there are people who value open-endedness- systems that have few simple rules that can be combined to obtain novel and unexpected results, even if these are not as optimized as they could be in a more complete system.

I picture the difference as that between a role playing game and chess: complex, domain-specific rules on one side, simple and abstract rules on the other.

Re:From design patterns to category theory by edem on Saturday October 07, 2017 @06:12AM (#15423158)
Not really. You can't comment on this until you tried a lisp. I know that you haven't tried it because if you had you would have known about paredit and how it solves the paren problem.