News for hackers. Stuff that matters.
Re:Show HN: Python library for functional programming by bryanwb on Wednesday December 13, 2017 @11:18PM (#15919906)
shameless plug, if you like this you may also like my library tryme which ports the Either and Maybe monads to Python http://tryme.readthedocs.io/en/latest/
Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @12:23AM (#15920135)
Without exhaustiveness checks for all your case analyses, what exactly is the point?
Re:Show HN: Python library for functional programming by bryanwb on Thursday December 14, 2017 @01:27AM (#15920344)
The point is that you can indicate whether an operation was successful or not by wrapping that value w/ a Success or Failure, rather returning a boolean, status code, and some subset of information about the operation.
Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @01:31AM (#15920357)
And how is that useful in the absence of pattern matching? If you write:

    if foo.is_success():
        ...
    else: # foo.is_failure()
        ...
Then you're back to branching on booleans. And we haven't even addressed the “lack of exhaustiveness checks” objection.
Re:Show HN: Python library for functional programming by ztjio on Thursday December 14, 2017 @03:28AM (#15920760)
These libraries in languages like python remind me of when Windows 95 was new and a bunch of Win3.1 compatible clones of the win95 task bar appeared.

People are often satisfied by the veneer of something better, because, they were told it was better but don't truly understand why. This is super common in the programming world, I'm afraid.

Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @03:46AM (#15920826)
Agreed. While algebraic data types are obviously a good idea, the best way to use Python is to accept it for what it is: a dynamically typed object-oriented language that's not very good at manipulating user-defined values (or even defining them to begin with), but has some pretty cool tricks of its own, like runtime metaprogramming with decorators and metaclasses.
Re:Show HN: Python library for functional programming by optimuspaul on Thursday December 14, 2017 @09:40AM (#15922536)
I think you place python in a much smaller box that it deserves. before it was object-oriented it was functional and procedural. For certain things I use it in a purely functional way but have never felt the need for one of these libraries to do so.
Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @11:01AM (#15923288)
Python has always had procedures, of course, but was it ever a functional language? Let's do a bit of fact-checkcing:

(0) Functional programming is expressing computation as the evaluation of functions. A language is “functional” to the extent it allows you to do functional programming.

(1) A function is a mapping from values to values. To express computation as the evaluation of functions, you need a rich universe of values: not just primitive ones like integers and object references, but also tuples, lists, trees, etc.

(2) Values are timeless entities that are distinguished from each other structurally. For example, it makes no sense to distinguish “this list [1,2,3]” from “that list [1,2,3]”, because both have the same structure - the same parts. Thus, what Python calls “tuples” and “lists” are not really tuple values and list values.

---

@_9jgl

Let's see how equal they remain in a timeless fashion:

    xs = [1,2,3]
    ys = [1,2,3]
    zs = ys
    send_to_another_thread(xs,ys,zs)
    print(xs == ys) # who knows
    print(ys == zs) # True
    print(xs is ys) # False
    print(ys is zs) # True
Turns out, the real equality testing operator in Python is `is`, not `==`. The only values Python gives you are object references.

---

@lmm

> This is by no means the only way to write Python

This is not the problem. The problem is that Python doesn't have compound values. Values are a matter of semantics, and semantics is a matter of how the language is defined, not how you choose to program in it.

Re:Show HN: Python library for functional programming by lmm on Thursday December 14, 2017 @12:28PM (#15924239)
Words are defined by consensus and use. To a lot of people "functional" means having first-class functions (in particular: functions can be defined anywhere (i.e. the language has closures) and functions are first-class values (i.e. functions can be passed as function parameters and returned from functions; function values can be expressed separately from assignment (i.e. the language has lambdas))) and programming in a style that makes use of map, reduce, and filter; more generally programming with immutable values and functions that return transformations of those values (as distinct from programming with pervasive state that is mutated). This is by no means the only way to write Python, but it's practical in Python to a much greater extent than it is in, say, Java.
Re:Show HN: Python library for functional programming by anowlcalledjosh on Thursday December 14, 2017 @12:07PM (#15924027)
> (2) Values are timeless entities that are distinguished from each other structurally. For example, it makes no sense to distinguish “this list [1,2,3]” from “that list [1,2,3]”, because both have the same structure - the same parts. Thus, what Python calls “tuples” and “lists” are not really tuple values and list values.

Would you mind expanding on this? I'm not sure I really understand what you mean – if we have two lists a = [1,2,3] and b = [1,2,3], then a == b, so I don't see how Python is distinguishing them from each other.

Re:Show HN: Python library for functional programming by masklinn on Thursday December 14, 2017 @03:52AM (#15920841)
Sum types make very little sense in dynamically typed languages even with good manipulation of user-defined values. For instance Erlang does not have "formal" sum types, it wouldn't really bring anything useful to the table, the compiler does not typecheck by default (so the sum type would just be a complication with no benefits) and Dialyzer can simulate them by using type/value unions e.g.

    {ok, integer()} | {err, string()}
Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @11:21AM (#15923467)
While I agree that there would be no point to user-defined sum types, constructor classes would be a strict improvement over what Erlang has.
Re:Show HN: Python library for functional programming by zimablue on Thursday December 14, 2017 @06:35AM (#15921482)
I don't use this but you could argue that it's useful to have a standard encapsulation of failure/missing that isn't null? Where some situation means that null already has a meaning, the alternative is to create a wrapper class for your situation (not general), or a general wrapper class which is basically what this is. So it's not completely useless whilst kind of crippled by no pattern matching?

Edit: although I guess that just using a tuple would be better for this maybe

Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @11:26AM (#15923527)
> I don't use this but you could argue that it's useful to have a standard encapsulation of failure/missing that isn't null?

I don't see what encapsulation you're talking about. Maybe and Either are very much concrete, not abstract, data types.

> Where some situation means that null already has a meaning, the alternative is to create a wrapper class for your situation (not general), or a general wrapper class which is basically what this is.

Unless you want to take advantage of existing plumbing infrastructure such as a monad transformer library, custom wrapper classes for your particular situation are precisely the right tool for the job.

Re:Show HN: Python library for functional programming by shmish111 on Thursday December 14, 2017 @10:49AM (#15923169)
It's useful if it's a monad (and functor) because then you have tools to avoid this check until the end of a series of possibly failing functions. It's only really useful with these tools though.

By itself you might as well use nulls or throw errors although you could argue that it's clearer having a maybe or an either.

Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @11:04AM (#15923306)
> It's useful if it's a monad (and functor) because then you have tools to avoid this check until the end of a series of possibly failing functions.

The monad laws already fail miserably in Haskell when you account for nontermination. Haskellers can only cope with this by eschewing partial functions as a matter of coding style. Now, eschewing partial functions, not to mention effectful procedures, is essentially impossible in a unityped language like Python. Are you sure you still want to play the monad game?

Of course, the decision to tolerate leaky abstractions is very much subjective. But constantly dealing with abstraction leaks is, in my experience, far more burdensome than not using abstractions to begin with. This is especially the case with mathematical abstractions whose meaning is given by equational / rewriting rules.

Re:Show HN: Python library for functional programming by kccqzy on Thursday December 14, 2017 @12:48PM (#15924443)
Which monad laws fail miserably in Haskell with non-termination, and for which monads?
Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @02:19PM (#15925840)
All three monad laws and all monads. Just use `seq`.
Re:Show HN: Python library for functional programming by lmm on Thursday December 14, 2017 @11:45AM (#15923761)
Python has lambdas so you can still write:

    foo.fold(lambda successResult: ..., lambda failResult: ...)
Even without that, returning a "foo" that has to be explicitly unwrapped makes for a safer API than having "foo" be sometimes a valid result and sometimes silently an error instead.
Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @02:28PM (#15925955)
> Python has lambdas so you can still write: (snippet)

Do you seriously consider this less of a hassle than branching on a frigging bit?

Re:Show HN: Python library for functional programming by lmm on Thursday December 14, 2017 @05:20PM (#15927739)
I think single entry/single exit is very valuable, and worth a bit of clunkiness for. And the branch version is also pretty clunky: either the long-winded "return ", abuse of "and"/"or", or the weirdly backwards expression version of "if" that Python has.
Re:Show HN: Python library for functional programming by catnaroek on Thursday December 14, 2017 @05:47PM (#15927984)
I don't disagree that branching on bits is clunky. That it's still less clunky than using Church or Scott-encodings speaks volumes of the practicality of the latter.
Re:Show HN: Python library for functional programming by shmish111 on Thursday December 14, 2017 @10:51AM (#15923189)
Also the link has a big article about why it's useful
Re:Show HN: Python library for functional programming by agumonkey on Thursday December 14, 2017 @12:23AM (#15920137)
Funny idea :)