3 ms·
The parallels to Unix are strong enough that having an actual pipe operator can be very useful. I frequently write code that looks something like this: Lis
by samdk 14y ago
The parallels to Unix are strong enough that having an actual pipe operator can be very useful. I frequently write code that looks something like this:
List.map data ~f:(fun elem -> ...)
|! List.find ~f:( ... )
|! Option.map ~f:( ... )
(This is OCaml, not Haskell, but the syntax is similar.) The definition of the '|!' operator is quite simple:
let (|!) x f = f x
- IvoDankolov 14y agoYes, in fact F# has that built in as |> , which I've taken to using in Haskell as well. Though it's a bit more intuitive to me when it has the lowest precedence like so: (|>) :: a -> (a->b) -> b (|>) x f = f x infixl 0 |> There infixl 0 means infix operator with left associativity and 0 precedence. Then you can use all sorts of expressions inbetween: (5 + 3 |> show) ++ " times" |> putStrLn Also, tying this back to one of the article's ideas, this piping-like syntax for doing things really manages to take away the emphasis from the function and put it back on the data. It isn't all about functions after all!
- minikomi 14y agoUsing -> and ->> in clojure also feels pretty close to this, and can clean up over-nested blobs very quickly. Eg: (reduce + (take 10 (filter even? (map #(* % %) (range))))) Can be expressed as: (->> (range) (map #(* % %)) (filter even?) (take 10) (reduce +))
- draegtun 14y agoIn Perl6 this is called the Feed Operator: http://perlcabal.org/syn/S03.html#Feed_operators http://perlcabal.org/syn/S03.html#Feed_operators