|
|
Log in / Subscribe / Register

An introduction to the Julia language, part 2

An introduction to the Julia language, part 2

Posted Sep 5, 2018 7:18 UTC (Wed) by rsidd (subscriber, #2582)
Parent article: An introduction to the Julia language, part 2

Unlike much of Julia, the "chaining" of functions (which I hadn't yet encountered)

f(x) |> g |> h == h(g(f(x)))
seems non-intuitive and contrary to mathematical notation to me. Usually "function composition" is written left to right, ie

h ◦ g ◦ f(x) ≡ h(g(f(x)))

and Haskell, for example, follows that with dot-composition -- you would write

h . g . f (x)
in Haskell.


to post comments

An introduction to the Julia language, part 2

Posted Sep 5, 2018 7:33 UTC (Wed) by karkhaz (subscriber, #99844) [Link]

Ocaml also uses the left-associative |> notation. It's a nice homage to the POSIX shell pipe, i.e. piping output from one function into another. But it's certainly strange to see that notation in a language aimed more at scientists and mathematicians, who probably won't have seen a shell pipe anyway, and where they're likely to be more familiar with right-associative composition.

An introduction to the Julia language, part 2

Posted Sep 5, 2018 12:08 UTC (Wed) by bpearlmutter (subscriber, #14693) [Link] (1 responses)

In Haskell, >>= goes the other way.

An introduction to the Julia language, part 2

Posted Sep 17, 2018 15:04 UTC (Mon) by nybble41 (subscriber, #55106) [Link]

A closer parallel for right-to-left function composition would be (>>>) from Control.Category rather than monadic bind (>>=):

(f >>> g >>> h) x == h (g (f x))

However, this is not quite the same as the Julia code, which was using flipped function application (Haskell: Data.Function.&), not composition:

f x & g & h == h (g (f x))

The Julia |> operator matches the syntax for flipped function application in F#.

An introduction to the Julia language, part 2

Posted Sep 5, 2018 13:52 UTC (Wed) by leephillips (subscriber, #100450) [Link]

It works the same way as the threading macro in Clojure: https://clojure.org/guides/threading_macros (where, because it's a lisp, you only need to write the operator once).

The idea is more that you are sending data through a pipeline, rather than composing functions (although, of course, it's the same thing).


An introduction to the Julia language, part 2

Posted Sep 5, 2018 14:01 UTC (Wed) by leephillips (subscriber, #100450) [Link]

By the way, a function composition operator was recently added to Julia:

(f ∘ g)(x) == f(g(x))

You can get it by typing \circ<tab> in the REPL if you want.

An introduction to the Julia language, part 2

Posted Sep 5, 2018 15:06 UTC (Wed) by epithumia (subscriber, #23370) [Link] (2 responses)

I think this is one of those things where there isn't true consistency of notation. For example, in undergrad algebra I learned function composition in the order that Julia does it. Wikipedia even has a section about the notational disconnect: https://en.wikipedia.org/wiki/Function_composition#Altern...

An introduction to the Julia language, part 2

Posted Sep 8, 2018 19:44 UTC (Sat) by jrn (subscriber, #64214) [Link] (1 responses)

Just curious: what textbook did you use for undergrad algebra? E.g. was it Topics in Algebra by Herstein?

An introduction to the Julia language, part 2

Posted Oct 23, 2018 23:36 UTC (Tue) by epithumia (subscriber, #23370) [Link]

I took two years of undergrad algebra. We did use Herstein's text, and Hungerford's, and some random other things that I can't even remember, plus a ream of hand-typed notes from one professor. (And this was the 90s, so typewriters were getting pretty rare by then. He did not use an electric typewriter.) Given that this professor was eclectic enough to refuse to acknowledge daylight savings time, we got to see all sorts of interesting notational issues.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds