Advertisement
flalli

Clojure operators -> and ->> wrote in Scheme

Feb 17th, 2024
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.12 KB | Source Code | 0 0
  1.   ;;
  2.   ;; See https://clojuredocs.org/clojure.core/-%3E
  3.   ;; for more info.
  4.   ;;
  5.   ;; (string-append (string-titlecase "abracada bra") "a" "x")
  6.   ;; (-> "abracada bra" string-titlecase (string-append "a" "x"))
  7.   ;;
  8.   ;; (string-reverse (string-titlecase "xpto"))
  9.   ;; (-> "xpto" string-titlecase string-reverse)
  10.   ;;
  11.   ;; (string-titlecase (string-reverse (string-titlecase "xpto")))
  12.   ;; (-> "xpto" string-titlecase string-reverse string-titlecase)
  13.   ;;
  14.   (define-syntax ->
  15.     (syntax-rules ()
  16.       ((_ a) a)
  17.       ((_ a (b c ...)) (b a c ...))
  18.       ((_ a b) (b a))
  19.       ((_ a b c) (-> (-> a b) c))
  20.       ((_ a b ... c) (-> (-> a b ...) c))))
  21.  
  22.   ;;
  23.   ;; See https://clojuredocs.org/clojure.core/-%3E%3E
  24.   ;; for more info.
  25.   ;;
  26.   ;; (reduce + 0 (take '(1 2 3 4 5) 2))
  27.   ;; (->> 2 (take '(1 2 3 4 5)) (reduce + 0))
  28.   ;;
  29.   ;; (reduce / 1 (take '(4 3 2 1) 3))
  30.   ;; (->> 3 (take '(4 3 2 1)) (reduce / 1))
  31.   ;;
  32.   (define-syntax ->>
  33.     (syntax-rules ()
  34.       ((_ a) a)
  35.       ((_ a (b c ...)) (b c ... a))
  36.       ((_ a b) (b a))
  37.       ((_ a b c) (->> (->> a b) c))
  38.       ((_ a b ... c) (->> (->> a b ...) c))))
Tags: clojure SCHEME
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement