m373h4n
2021-11-30 16:18:35

hello is there anything like to move results of a function to next one like -> of clojure https://clojuredocs.org/clojure.core/-%3E as a sample ;; Arguably a bit cumbersome to read: user=> (first (.split (.replace (.toUpperCase "a b c d") "A" "X") " ")) "X" ;; Perhaps easier to read: user=> (-> "a b c d" .toUpperCase (.replace "A" "X") (.split " ") first) "X"


samth
2021-11-30 16:22:14

See the threading package.



m373h4n
2021-11-30 16:24:22

thnak you


ben.knoble
2021-11-30 16:29:47

Also the qi package, which offers a lot of similar ideas in addition to threading like macros


m373h4n
2021-11-30 16:31:27

I am checking it out now


badkins
2021-11-30 16:35:44

In many cases, you can just use the function name, but sometimes you’ll need to use _ as a placeholder. For example: (~> "a b c d" string-upcase (string-replace _ "A" "X") string-split first)


badkins
2021-11-30 16:36:21

(referring to threading)


badkins
2021-11-30 16:53:12

My bad, ~> threads into the first position, so the _ isn’t necessary above: (~> "a b c d" string-upcase (string-replace "A" "X") string-split first)


ben.knoble
2021-11-30 17:00:06

This is basically true of qi’s threading operators, though the syntax for the arguments is slightly different: (~> ("a b c d") …) This is because qi is multi-valued, so needs a way to delimit arguments being threaded and the functions (flows) being applied.


jcoo092
2021-11-30 19:49:56

I’m not familiar with the -> function. Does that work roughly like F#’s \|> operator, but made to fit into s-expressions?


seanbunderwood
2021-11-30 21:20:04

Yup