2017-06-26 12:15:03 +03:00
|
|
|
(defmodule Array
|
|
|
|
|
2017-10-17 09:57:36 +03:00
|
|
|
(defn reduce [f x xs]
|
|
|
|
(let [total x]
|
|
|
|
(do
|
|
|
|
(for [i 0 (count xs)]
|
|
|
|
(set! &total (f &total (nth xs i))))
|
|
|
|
total)))
|
2017-10-26 18:37:44 +03:00
|
|
|
|
2017-11-29 17:28:21 +03:00
|
|
|
(defn first [a]
|
|
|
|
@(Array.nth a 0))
|
|
|
|
|
|
|
|
(defn last [a]
|
|
|
|
@(Array.nth a (Int.dec (Array.count a))))
|
2017-10-26 18:37:44 +03:00
|
|
|
)
|
2017-10-12 22:27:36 +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:22:02 +03:00
|
|
|
|
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)))
|