8 ms·
The first typed programming language where I've seen pipe operator |> in action was in F#. You can write something like: sum 1 2 |> multiply 3 and it work
by sumeetdas 1y ago
The first typed programming language where I've seen pipe operator |> in action was in F#. You can write something like:
sum 1 2
|> multiply 3
and it works because |> pushes the output of the left expression as the last parameter into the right-hand function. multiply has to be defined as:
let multiply b c = b \* c
so that b becomes 3, and c receives the result of sum 1 2.
RHS can also be a lambda too:
sum 1 2 |> (fun x -> multiply 3 x)
|> is not a syntactic sugar but is actually defined in the standard library as:
let (|>) x f = f x
For function composition, F# provides >> (forward composition) and << (backward composition), defined respectively as:
let (>>) f g x = g (f x)
let (<<) f g x = f (g x)
We can use them to build reusable composed functions:
let add1 x = x + 1
let multiply2 x = x \* 2
let composed = add1 >> multiply2
F# is a beautiful language. Sad that M$ stopped investing into this language long back and there's not much interest in (typed) functional programming languages in general.
- christophilus 1y agoF# is excellent. It’s tooling, ecosystem, and compile times are the reason I don’t use it. I learned it alongside OCaml, and OCaml’s compilation speed spoiled me. It is indeed a shame that F# never became a first class citizen.
- cosmos64 1y agoLots of this, especially the tooling and ecosystem, improved considerably in the last couple of years. OCaml is a great language, as are others in the ML family. Isabelle is the first language that has introduced the |> pipe character, I think.
- dmead 1y agoHaskell seems pretty dead as well. Good think php has another option for line noise though.
- gylterud 1y agoWhat makes you believe Haskell is dead or even dying? New versions of GHC are coming out, and in my experience, developing Haskell has never been smoother (that’s not to say it is completely smooth).
- epolanski 1y agoAnd yet, while PHPs, Javas, and even nicher/newer languages like Kotlin, Clojure or Scala have plenty of killer software (software that makes it worth learning a language just to use that library/framework) Haskell has none after 30 years. Zero. Mind you, I know and like Haskell, but its issues are highly tied to the failure of the simple haskell initiative (also the dreadful state of its tooling).
- dmead 1y agoyea I agree. haskell was my primary language for several years in the 00s. it's since had almost zero industry uptake. Don't come at me with jane street or the one off startup. I thought for a while I'd be able to focus on getting jobs that liked haskell. it never happened.
- chriswarbo 1y agoI certainly wouldn't focus on getting a Haskell job. Yet they are out there; e.g. my current job is Haskell, and happens to be in the same sector (public transport) as my last job (which was mostly Scala). Also, I've found Haskell appropriate for some one-off tasks over the years, e.g. - Extracting a load of cross-referenced data from a huge XML file. I tried a few of our "common" languages/systems, but they all ran out of memory. Haskell let me quickly write something efficient-enough. Not sure if that's ever been used since (if so then it's definitely tech debt). - Testing a new system matched certain behaviours of the system it was replacing. This was a one-person task, and was thrown away once the old system was replaced; so no tech debt. In fact, this was at a PHP shop :)
- dmead 1y ago
- rpeden 1y agoIs |> actually an operator in F#? I think it's just a regular function in the standard library but maybe I'm remembering incorrectly.
- tracker1 1y agoIt's kind of wild that PHP gets a pipe(line) operator before JS finalizes its' version... of course they've had multiple competing proposals, of which I liked the F# inspired one the most... I've stopped relying on the likes of Babel (too much bloat) or I'd like to be able to use it. I used it for years for async functions and module syntax before they were implemented in node and browsers. Now it's hard to justify.
- Akronymus 1y agoThere are also ||> and |||> for automatically destructuring tuples and passing each part as a separate value along. And there are also the reverse pipes (<|, <|| and <|||) F# is, for me, the single most ergonomic language to work in. But yeah, M$ isn't investing in it, so there are very few oppurtunities to actually work with f# in the industry either.