I come to this strange piece of code from rhombus's doc about |>
[3, 1, 2] |> to_string.map(_)
Alternatively this strange code is to_string.map(_)([1,2,3]).
I find the code makes more sense with
(_.map(to_string))([3, 1, 2])
I come to this strange piece of code from rhombus's doc about |>
[3, 1, 2] |> to_string.map(_)
Alternatively this strange code is to_string.map(_)([1,2,3]).
I find the code makes more sense with
(_.map(to_string))([3, 1, 2])
The use of |> matches the operator available in ML like languages (OCaml, F#), Haskell doesn't have it natively, but many code bases define it as it is easy enough in Haskell to do.
It's used to create a pipeline:
numbers
|> List.filter(_ , x => (_ > 0))
|> List.sort(_)
|> List.take(_, 2)
This is often preferred to chaining the method calls. It also shines when the return value types change in the pipeline.
Yes and, actually, I think the code is also
to_string.map([1,2,3])
;;=> ["1", "2", "3"]
I think I finally figured the secret sauce here: this is not mapping over lists, but rather calling the Function.map method (based on an annotation error message I got). Here is the doc: 4.1 Functions
method (f :: Function).map(args0 :: List, args :: List, ...) :: List.of(Any.like_result(f))Applies
fto each element of eachargs(includingargs0), iterating through theargslists together, [...]
In short this is not a dynamically-dispatched map (so other data like MutableList or PairList won't work). The code is, after un-abbreviating everything,
[1,2,3] |> (fun lst: Function.map(to_string, lst))
So _ works in its expected manner but the code is actually different from List.sort(_) or [1,2,3] |> List.map(_, to_string) or [1,2,3].map(to_string).