by btown · 1y ago · 0 comments
It's reasonably type-safe, and there's no need to "close" your chain - every outputted value as you write the chain can have a primitive type.
x = Model.objects.all() >> by_pk >> call(dict.values) >> to_list >> get('name') >> _map(str.title) >> log_debug >> to_json
It shines in notebooks and live coding, where you might want to type stream-of-thought in the same order of operations that you want to take place. Need to log where something might be going wrong? Tee it like you're on a command line!Idiomatic? Absolutely not. Something to push to production? Not unless you like being stabbed with pitchforks. Actually useful for prototyping? 1000%.
Looks a lot like function composition with the arguments flipped, which in Haskell is `>>>`. Neat!
But since you’re writing imperative code and binding the result to a variable, you could also compare to `>>=`.
(https://downloads.haskell.org/~ghc/7.6.2/docs/html/libraries...)
I think there's a big gap pedagogically here. Once a person understands functional programming, these kinds of composition shorthands make for very straightforward and intuitive code.
But, if you're just understanding basic Haskell/Clojure syntax, or stuck in the rabbit hole of "monad is a monoid" style introductions, a beginner could easily start to think: "This functional stuff is really making me need to think in reverse, to need to know my full pipeline before I type a single character, and even perhaps to need to write Lisp-like (g (f x)) style constructs that are quite the opposite of the way my data is flowing."
I'm quite partial to tutorials like Railway Oriented Programming [1] which start from a practical imperative problem, embrace the idea that data and code should feel like they flow in the same direction, and gradually guide the reader to understanding the power of the functional tools they can bring to bear!
If anything, I hope this hack sparks good conversations :)
[0] https://github.com/tc39/proposal-pipeline-operator/issues/91 - 6 years and 793 comments!
> =dd-{"Todo","es","bueno"} /call("gsub", "[aeiou]", "i") /call("rep", 2) /call "lower"
{"tiditidi", "isis", "biinibiini"}
> ps={{x=5, y=7}, {x=5, y=4}, {x=3, y=4}}
> =dd-ps/get"x"
{5, 5, 3}
> =dd-ps/where {y=4}
{[2] = {y=4, x=5}, [3] = {y=4, x=3}}
> =dd-ps/group "x"
{[5] = {{x=5, y=7}, {x=5, y=4}}, [3] = {{x=3, y=4}}}
> =dd-ps/group "x"/get(1)
{[5] = {x=5, y=7}, [3] = {x=3, y=4}}
> =dd-ps/group "x"/map(get"y")
{[5] = {7, 4}, [3] = {4}
> =dd-ps/group "x"/map(get"y")/map(max)
{[5] = 7, [3] = 4}
> =dd-ps/group "x"/map(get"y")/map(max)/min
4
> =dd-ps/group "x"/map(get"y")/map(max)/sum
11
> =dd-ps/where(function(p) return 9 < p.x+p.y end)
{[2] = {y=6, x=8}}
> function bytes(s) return {s:byte(1,#s)} end
> =dd-{"hey", "you"}/map(bytes)
{{104, 101, 121}, {121, 111, 117}}
> =dd-{"hey", "you"}/map(bytes)/flat/freq
{[111] = 1, [101] = 1, [104] = 1, [121] = 2, [117] = 1} x = Model.objects.all() | by_pk | call(dict.values) | to_list | get('name') | _map(str.title) | log_debug | to_jsonIn practice, rshift gives a lot more flexibility! And you’d rarely chain after a numeric value.
class more:
def __ror__(self, other):
import pydoc
pydoc.pager(str(other))
more = more()
and here the low precedence of | is useful.It's a sign of the design quality of a programming language when 2 arbitrary features A and B of that language can be combined and the combination will not explode in your face. In python and C++ (and plenty of other languages) you constantly have the risk that 2 features don't combine. Both python and C++ are full of examples where you will learn the hard way: "ah yes, this doesn't work." Or "wow, this is really unexpected".
It's usually a good idea for operators to have a specific meaning and not randomly change that meaning depending on the context. If you want to add new operators with new meanings, that's fine. Haskell does that. The downside is people find it really tempting and you end up with a gazillian difficult-to-google custom operators that you have to learn.
I wanted to wash my eyes the first time I saw it.