Carp/examples/functor.carp
2017-09-08 12:24:57 +02:00

33 lines
745 B
Plaintext

(use IO)
(use Int)
(use Array)
(defmodule ArrayExtension
(defn fmap [f a] (Array.map f a))
)
(deftype Box [x Int])
(defmodule Box
(defn fmap [f box] (let [new-x (f (Box.x &box))]
(Box.set-x box new-x))))
(use Box)
(use ArrayExtension)
;;(defn higherOrder [x] (fmap inc x))
(defn main []
(do
(println &(str (Box.x (ref (fmap inc (Box.init 100))))))
(println &(str (Box.x (ref (Box.fmap inc (Box.init 100))))))
(println &(str (ref (ArrayExtension.fmap inc [10 20 30 40 50]))))
(println &(str (ref (fmap inc [10 20 30 40 50]))))
(println &(Array.str (ref (fmap inc [10 20 30 40 50]))))
(println &(Array.str (ref (ArrayExtension.fmap inc [10 20 30 40 50]))))
))
(build)
(run)
(quit)