Carp/core/Array.carp

37 lines
1.0 KiB
Plaintext
Raw Normal View History

2017-06-26 12:15:03 +03:00
(defmodule Array
(defn reduce [f x xs]
(let [total x]
(do
(for [i 0 (count xs)]
(set! &total (f &total (nth xs i))))
total)))
2017-10-12 21:13:53 +03:00
)
2017-10-19 19:34:44 +03:00
;; BUGS! These function definitions will not create the required typedef for Array, for instance:
;; typedef Array Array__int;
;; INSTEAD they will create the following (wrong) typedef:
;; typedef Array Array__int_MUL_;
2017-10-19 19:34:44 +03:00
;; ;; Extending the Int module with Array functions
;; (defmodule Int
;; (defn add-ref [x y]
;; (Int.+ (Int.copy x) (Int.copy y)))
;; (defn sum [xs]
;; (Array.reduce Int.add-ref 0 xs)))
;; ;; Extending the Float module with Array functions
;; (defmodule Float
;; (defn add-ref [x y]
;; (Float.+ (Float.copy x) (Float.copy y)))
;; (defn sum [xs]
;; (Array.reduce Float.add-ref 0.0f xs)))
;; ;; Extending the Double module with Array functions
;; (defmodule Double
;; (defn add-ref [x y]
;; (Double.+ (Double.copy x) (Double.copy y)))
;; (defn sum [xs]
;; (Array.reduce Double.add-ref 0.0 xs)))