Made inc, dec, max, min into interfaces. Changed Array.max/min to new

names. Fixed tests and removed special max/min from Statistics module.
This commit is contained in:
Erik Svedäng 2018-01-29 07:14:41 +01:00
parent 7660b32db3
commit 8281dfa109
11 changed files with 44 additions and 66 deletions

View File

@ -24,24 +24,24 @@
(break))))
eq)))
(defn max [xs]
(defn maximum [xs]
(let [result (first xs)
n (count xs)]
(do
(for [i 0 n]
(let [x @(nth xs i)]
(if (Int.< result x)
(if (< result x)
(set! &result x)
())))
result)))
(defn min [xs]
(defn minimum [xs]
(let [result (first xs)
n (count xs)]
(do
(for [i 0 n]
(let [x @(nth xs i)]
(if (Int.> result x)
(if (> result x)
(set! &result x)
())))
result)))

View File

@ -30,9 +30,6 @@
(register abs (λ [Int] Int))
(defn max [a b] (if (> a b) a b))
(defn min [a b] (if (< a b) a b))
(defn even? [a] (= (mod a 2) 0))
(defn odd? [a] (not (even? a)))

View File

@ -1,8 +1,10 @@
;; The 'copy' and 'str' interfaces are defined internally:
;;(definterface copy (λ [&a] a))
;;(definterface str (λ [a] String))
(definterface = (λ [a a] Bool))
(definterface /= (λ [a a] Bool))
(definterface format (λ [&String a] String))
(definterface zero (λ [] a))
(definterface add-ref (λ [&a &a] a))
(definterface sub-ref (λ [&a &a] a))
(definterface mul-ref (λ [&a &a] a))
@ -16,22 +18,22 @@
(definterface < (λ [a a] Bool))
(definterface > (λ [a a] Bool))
;; <=
;; >=
;; (definterface inc (λ [a] a))
;; (definterface dec (λ [a] a))
;; (definterface max (λ [a a] a))
;; (definterface min (λ [a a] a))
;;(definterface from-string (λ [a] String))
(definterface inc (λ [a] a))
(definterface dec (λ [a] a))
(definterface format (λ [&String a] String))
(definterface from-string (λ [&String] a))
(definterface zero (λ [] a))
(definterface random (Fn [] a))
(definterface random-between (Fn [a a] a))
(definterface pi a)
;; These interfaces are defined internally:
;;(definterface copy (λ [&a] a))
;;(definterface str (λ [a] String))
;; The following functions make use of the interfaces
(defn <= [a b]
(or (< a b)
@ -45,3 +47,9 @@
(if (= a b)
0
(if (< a b) -1 1)))
(defn max [a b]
(if (> a b) a b))
(defn min [a b]
(if (< a b) a b))

View File

@ -34,9 +34,6 @@
(register abs (λ [Long] Long))
(defn max [a b] (if (> a b) a b))
(defn min [a b] (if (< a b) a b))
(defn even? [a] (= (mod a 2l) 0l))
(defn odd? [a] (not (even? a)))
)

View File

@ -29,26 +29,6 @@
(defn mean [data]
(/ (Statistics.sum data) (from-int (Array.count data))))
(defn min [a]
(let [m (Double.copy (Array.nth a 0))]
(do
(for [i 1 (Array.count a)]
(let [el (Double.copy (Array.nth a i))]
(if (Double.< el m)
(set! &m el)
())))
m)))
(defn max [a]
(let [m (Double.copy (Array.nth a 0))]
(do
(for [i 1 (Array.count a)]
(let [el (Double.copy (Array.nth a i))]
(if (Double.> el m)
(set! &m el)
())))
m)))
(defn _pp [a mean]
(let [sum 0.0]
(do
@ -185,8 +165,8 @@
(defn summary [samples]
(Summary.init
(Statistics.sum samples)
(min samples)
(max samples)
(Array.minimum samples)
(Array.maximum samples)
(mean samples)
(median samples)
(variance samples)

View File

@ -12,6 +12,7 @@
## Big Language Features
* [0.4] Doc strings.
* [0.4] Kind-checking for types (make sure the type variables match, etc).
* [1.0] Tagged unions (also known as "sum types" or "enums")
* [1.0] Lambdas (anonymous functions) that compile on Windows and don't leak memory!
@ -22,6 +23,7 @@
* [0.3] Errors in macros should present the code location of both the macro and of the code that uses of it.
* [0.3] If main returns the value of a function returning a generic type it will fail (because there is no constraint for "Int or ()")
* [0.3] Optimization: Don't copy the whole array in Array.swap, Array.aupdate, etc.
* [0.3] When registering with an interface, make sure the registered function actually *can* unify with it's signature.
* [0.4] Reintroduce the p-string patch but with support for embedded string literals.
* [0.4] Should be possible to read float literal without '.', eg. "3f" (because that's how they print sometimes)

View File

@ -17,16 +17,16 @@
(use Box)
(use ArrayExtension)
(defn higherOrder [x] (fmap inc x))
(defn higherOrder [x] (fmap Int.inc x))
(defn main []
(do
(println &(str @(Box.x &(fmap inc (Box.init 100)))))
(println &(str @(Box.x &(fmap Int.inc (Box.init 100)))))
(println &(str @(Box.x &(Box.fmap inc (Box.init 100)))))
(println &(str &(ArrayExtension.fmap inc [10 20 30 40 50])))
(println &(str &(fmap inc [10 20 30 40 50])))
(println &(Array.str &(fmap inc [10 20 30 40 50])))
(println &(Array.str &(ArrayExtension.fmap inc [10 20 30 40 50])))
(println &(str &(fmap Int.inc [10 20 30 40 50])))
(println &(Array.str &(fmap Int.inc [10 20 30 40 50])))
(println &(Array.str &(ArrayExtension.fmap Int.inc [10 20 30 40 50])))
(println &(str &(higherOrder (Box.init 999))))
(println &(str &(higherOrder [9 99 999 9999])))
))

View File

@ -8,10 +8,10 @@
(load "sdl_image.carp")
(use Keycode)
(def max 400)
(def rand-max 400)
(defn r []
(the Int (random-between 0 max)))
(the Int (random-between 0 rand-max)))
(defn random-lines []
(let [p1 (make-point (r) (r))

View File

@ -104,7 +104,9 @@ concretizeXObj allowAmbiguityRoot typeEnv rootEnv visitedDefinitions root =
| envIsExternal foundEnv ->
let theXObj = binderXObj binder
Just theType = ty theXObj
Just typeOfVisited = t
typeOfVisited = case t of
Just something -> something
Nothing -> error ("Missing type on " ++ show xobj ++ " at " ++ prettyInfoFromXObj xobj)
in if --(trace $ "CHECKING " ++ getName xobj ++ " : " ++ show theType ++ " with visited type " ++ show typeOfVisited ++ " and visited definitions: " ++ show visitedDefinitions) $
typeIsGeneric theType && not (typeIsGeneric typeOfVisited)
then case concretizeDefinition allowAmbig typeEnv env visitedDefinitions theXObj typeOfVisited of
@ -161,7 +163,7 @@ concretizeXObj allowAmbiguityRoot typeEnv rootEnv visitedDefinitions root =
tys = map (typeFromPath env) interfacePaths
tysToPathsDict = zip tys interfacePaths
in case filter (matchingSignature actualType) tysToPathsDict of
[] -> return $ --(trace ("No matching signatures for interface lookup of " ++ name ++ " of type " ++ show actualType ++ " " ++ prettyInfoFromXObj xobj ++ ", options are:\n" ++ joinWith "\n" (map show tysToPathsDict)))
[] -> return $ -- (trace ("No matching signatures for interface lookup of " ++ name ++ " of type " ++ show actualType ++ " " ++ prettyInfoFromXObj xobj ++ ", options are:\n" ++ joinWith "\n" (map show tysToPathsDict))) $
--(Right xobj)
if allowAmbig
then (Right xobj) -- No exact match of types
@ -178,7 +180,7 @@ concretizeXObj allowAmbiguityRoot typeEnv rootEnv visitedDefinitions root =
where replace theType singlePath =
let Just t' = t
normalSymbol = XObj (Sym singlePath LookupGlobal) i (Just t')
in visitSymbol allowAmbig env $ -- (trace ("Disambiguated interface symbol " ++ pretty xobj ++ prettyInfoFromXObj xobj ++ " to " ++ show singlePath ++ " : " ++ show replaced ++ ", was " ++ show t' ++ ", mappings = " ++ show mappings))-- ++ ", options were:\n" ++ joinWith "\n" (map show tysToPathsDict)))
in visitSymbol allowAmbig env $ --(trace ("Disambiguated interface symbol " ++ pretty xobj ++ prettyInfoFromXObj xobj ++ " to " ++ show singlePath ++ " : " ++ show t'))
normalSymbol
Nothing ->

View File

@ -55,12 +55,12 @@
"reverse works as expected")
(assert-equal test
10
(max &(range 1 10 1))
"max works as expected")
(maximum &(range 1 10 1))
"maximum works as expected")
(assert-equal test
1
(min &(range 1 10 1))
"min works as expected")
(minimum &(range 1 10 1))
"minimum works as expected")
(assert-equal test
55
(sum &(range 1 10 1))

View File

@ -85,14 +85,6 @@
10.0
(sum &[2.5 5.0 2.0 0.5])
"sum works as expected")
(assert-equal test
0.5
(min &[2.5 5.0 2.0 0.5])
"min works as expected")
(assert-equal test
5.0
(max &[2.5 5.0 2.0 0.5])
"max works as expected")
(assert-equal test
40.0
(stdev-pct &[1.0 1.0 9.0 9.0])