mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
Implemented 'add-ref' and 'zero' for Int/Float/Double. Allows nice 'Array.sum' function.
This commit is contained in:
parent
270f54a1de
commit
b0cdc65f4b
@ -12,6 +12,31 @@
|
||||
|
||||
(defn last [a]
|
||||
@(Array.nth a (Int.dec (Array.count a))))
|
||||
|
||||
(defn max [xs]
|
||||
(let [result (first xs)
|
||||
n (count xs)]
|
||||
(do
|
||||
(for [i 0 n]
|
||||
(let [x @(nth xs i)]
|
||||
(if (Int.< result x)
|
||||
(set! &result x)
|
||||
())))
|
||||
result)))
|
||||
|
||||
(defn min [xs]
|
||||
(let [result (first xs)
|
||||
n (count xs)]
|
||||
(do
|
||||
(for [i 0 n]
|
||||
(let [x @(nth xs i)]
|
||||
(if (Int.> result x)
|
||||
(set! &result x)
|
||||
())))
|
||||
result)))
|
||||
|
||||
(defn sum [xs]
|
||||
(Array.reduce add-ref (zero) xs))
|
||||
)
|
||||
|
||||
;; BUGS! These function definitions will not create the required typedef for Array, for instance:
|
||||
|
@ -52,4 +52,10 @@
|
||||
|
||||
(defn /= [x y]
|
||||
(not (Double.= x y)))
|
||||
|
||||
(defn zero []
|
||||
0.0)
|
||||
|
||||
(defn add-ref [x y]
|
||||
(+ @x @y))
|
||||
)
|
||||
|
@ -52,4 +52,11 @@
|
||||
(register mod (Fn [Float Float] Float))
|
||||
(register copy (Fn [(Ref Float)] Float))
|
||||
(register abs (Fn [Float] Float))
|
||||
|
||||
(defn zero []
|
||||
0.0f)
|
||||
|
||||
(defn add-ref [x y]
|
||||
(+ @x @y))
|
||||
|
||||
)
|
||||
|
@ -34,4 +34,11 @@
|
||||
|
||||
(defn even? [a] (= (mod a 2) 0))
|
||||
(defn odd? [a] (not (even? a)))
|
||||
|
||||
(defn zero []
|
||||
0)
|
||||
|
||||
(defn add-ref [x y]
|
||||
(+ @x @y))
|
||||
|
||||
)
|
||||
|
@ -1,6 +1,9 @@
|
||||
(definterface = (λ [a a] Bool))
|
||||
(definterface /= (λ [a a] Bool))
|
||||
|
||||
(definterface zero (λ [] a))
|
||||
(definterface add-ref (λ [&a &a] a))
|
||||
|
||||
;; These interfaces would be desirable also? Produces errors in Vector module for now...
|
||||
;; (definterface + (λ [a a] a))
|
||||
;; (definterface - (λ [a a] a))
|
||||
|
@ -21,6 +21,7 @@
|
||||
* [1.0] Lambdas (anonymous functions)
|
||||
|
||||
## Smaller Language Features ("niceties")
|
||||
* [0.3] Errors in macros should present the code both in the macro and in the use of it.
|
||||
* [0.3] Should be possible to read float literal without '.', eg. "3f" (because that's how they print sometimes)
|
||||
* [0.3] Some interfaces are missing info - Thing.str : (λ [(Ref Thing)] String) Defined at line 0, column 0 in 'dummy-file'
|
||||
* [0.3] Good string functions
|
||||
|
@ -15,28 +15,6 @@
|
||||
(use Double)
|
||||
(use Array)
|
||||
|
||||
(definterface zero (λ [] a))
|
||||
(definterface add-ref (λ [&a &a] a))
|
||||
|
||||
(defmodule Int
|
||||
(defn zero []
|
||||
0))
|
||||
|
||||
(defmodule Float
|
||||
(defn zero []
|
||||
0.0f))
|
||||
|
||||
(defmodule Double
|
||||
(defn zero []
|
||||
0.0))
|
||||
|
||||
(defmodule Array
|
||||
(defn sum [xs]
|
||||
(Array.reduce add-ref (zero) xs)))
|
||||
|
||||
(defn add-ref [x y]
|
||||
(+ @x @y))
|
||||
|
||||
(defn main []
|
||||
(do
|
||||
(println (refstr (sum &[1 2 3])))
|
||||
|
Loading…
Reference in New Issue
Block a user