doug-moen / openscad2

better abstraction mechanisms for OpenSCAD
Boost Software License 1.0
25 stars 3 forks source link

Functions #11

Closed gwadej closed 9 years ago

gwadej commented 9 years ago

In the section Right-Associative Function Calls, I like the operator approach to mitigating the paren pile-up. As a C++ programmer, I'm mildly startled by the stream insertion operator (<<) being used this way. However, I'm not bothered enough to argue about it. I believe Ruby also uses this operator in a similar way.

I only point it out because others may comment.

doug-moen commented 9 years ago

In Microsoft's functional language F#, <| is the pipeline operator (aka "pipe backward"), where f <| x is equivalent to f x. This operator has been widely copied by other functional languages, and now appears in Apple's new Swift language, as well as OCaml, Elm, Ela, and probably others. The operator name is supposed to remind you of a Unix pipe (|) combined with < to indicate the direction of data flow (the argument flows into the function). There is also a |> operator (pipe forward), where x |> f is f x, and this lets you write your pipelines in the traditional Unix order, where data flows left to right.

I think <| is too hard to type, so I chose << instead. I am thinking about adding >> for symmetry.

Also note that in F#, Swift, OCaml, Elm and Ela, << and >> are function composition. I haven't decided if OpenSCAD needs function composition. It seems that these operators are used far less frequently than the pipeline operators, especially if you aren't well versed in functional programming idioms.

In Haskell, the "pipe left" operator is spelled $, but compared to the F# syntax, it's ugly and non-intuitive, and I don't know any languages that copied this.

gwadej commented 9 years ago

The more I think about it the more I think I could argue that the C++ insertion is a special case of this pipeline notation. It struck me as odd at first read, but less so as I had a chance to let it simmer.

Given your other considerations, I have to agree that << and >> are reasonable choices.

Perl 6 added pipeline operators <== and ==> to try to handle the same things. They are very different from any other operators from other languages. However, taking the principle that operators you use a lot should be shorter to type, I prefer 2 characters to 3 on something I expect to use a lot.

doug-moen commented 9 years ago

Updated Functions.md to make it clearer. The << operator is now called the pipeline operator, and the OpenSCAD notation for a chain of transformations is now called a pipeline.