1
0
forked from alex-eg/sex
Files
sex/tests/sex-programs/threading.sex

51 lines
1.4 KiB
Scheme

(input)
(output "arithmetic: 26"
"a bare step needs no parens: 20"
"through a path: 1 4"
"mixed with a method: 25"
"one step is just the value: 7")
(return 0)
;;; A step may be a call, a bare name, a member path or a method, and
;;; the value goes in as its first argument.
(include stdio.h)
(import threading-macros)
(struct point ((x int) (y int)))
(struct line ((a (* struct point)) (b (struct point))))
(fn double-it ((n int)) int
(return (* n 2)))
(fn add ((n int) (m int)) int
(return (+ n m)))
(ops point
(pub fn length-sq ((self (* const struct point))) int
(return (+ (* (. self x) (. self x))
(* (. self y) (. self y))))))
(pub fn main () int
;; (add (double-it 5) 3) doubled: inside out, written outside in
(printf "arithmetic: %d\n"
(-> 5
double-it
(add 3)
double-it))
(printf "a bare step needs no parens: %d\n" (-> 5 double-it double-it))
(var p (struct point) #(1 2))
(var l (struct line) #(struct line : .a (& p) .b #(3 4)))
(var lp (* struct line) (& l))
;; a member path is an ordinary step, and reads through both pointers
(printf "through a path: %d %d\n"
(-> lp (. a x))
(. lp b y))
(var q (struct point) #(3 4))
(printf "mixed with a method: %d\n" (-> q .length-sq))
(printf "one step is just the value: %d\n" (-> 7))
(return 0))