Added function 'zip' and 'enumerated' for Array.

This commit is contained in:
Erik Svedäng 2018-06-01 11:18:03 +02:00
parent f05efea2fe
commit 12dbe36adc
7 changed files with 43 additions and 8 deletions

View File

@ -166,7 +166,8 @@
(aset-uninitialized! &na i (f (nth a i))))
na))
(defn copy-map-2 [f a b]
(doc zip "Map over two arrays using a function that takes two arguments. Produces a new array with the length of the shorter input.")
(defn zip [f a b]
(let-do [l (Int.min (length a) (length b))
na (allocate l)]
(for [i 0 l]
@ -198,6 +199,9 @@
(set! j (+ j len))))
result))
(doc enumerated "Create a new array of Pair:s where the first position is the index and the second position is the element from the original array.")
(defn enumerated [xs]
())
(zip Pair.init-from-refs
&(range 0 (length xs) 1) ;; Inefficient, creates a temporary array.
xs))
)

View File

@ -65,4 +65,16 @@
(defn > [a b]
(Int.> @a @b))
(defn + [a b]
(Int.+ @a @b))
(defn - [a b]
(Int.- @a @b))
(defn * [a b]
(Int.* @a @b))
(defn / [a b]
(Int./ @a @b))
)

12
core/Tuples.carp Normal file
View File

@ -0,0 +1,12 @@
(deftype (Pair a b) [a a b b])
(defmodule Pair
(defn init-from-refs [r1 r2]
(Pair.init @r1 @r2))
;; BUG: Can't define this function because = resolves to Pair.= in the body.
;; (defn = [p1 p2]
;; (and (= (Pair.a p1) (Pair.a p2))
;; (= (Pair.b p1) (Pair.b p2))))
)

View File

@ -4,7 +4,6 @@
** 1.0 - The completed version of the language with all planned features and extra nice ergonomics.
* Unsorted Todo:s
** Syntax for pointer type, perhaps "^"?
* Critical Bugs
** 0.3
*** References must keep track of their origin and prevent usage of them if the origin has been given away.
@ -14,7 +13,7 @@
*** Parse error doesn't stop execution when running with -x flag from command line
* Big Language Features
** 0.4
*** Type annotations and private/hide flags should be honored.
*** Type annotations should affect the type, not just check it.
*** Stack allocated Array with an explicit size.
*** Deps function for templates should return an Either to enable errors when instantiating deps.
***
@ -56,7 +55,6 @@
** 0.4
*** Make the flags sent to the compiler be saved into different groups depending on actual compiler.
*** Hide instances of templates/generic functions when printing the environment (by default, allow it as a setting).
*** Entering the name of a symbol at the REPL prints nothing.
*** Show "call stack" when getting an error during concretization.
*** Allow echoing of infered type at the REPL (must be silenced when loading a file though)
*** Only reload files once, keep track of which ones have already been reloaded.
@ -64,14 +62,13 @@
*** Flycheck hangs and eats all resources sometimes.
*** Emacs-mode: Make foreach and deftype indent correctly.
*** Make foreach, and*, not*, etc. proper keywords.
*** Emacs-mode: Make foreach, and*, not*, etc. into keywords (highlight them).
** 1.0
*** Project templates.
*** Somehow make it possible to enter ":t foo" at the REPL (can't be done now because each atom is evaluated separately)
*** Preserve whitespace to allow saving forms back to disk.
*** Refactorings at the REPL. Rename, extract function, add/remove parameter?
*** --watch flag for recompiling when the files in the project change?
* Code generation
** [1.X] LLVM backend
** [?] Emit #LINE macros in the generated C code?
@ -97,6 +94,7 @@
** Reintroduce the p-string patch but with support for embedded string literals?
** Rename deftype to defstruct?
** Syntax for pointer type, perhaps "^"?
* Notes
** Travis
** Should depsForCopyFunc and depsForDeleteFunc really be needed in Array templates, they *should* instantiate automatically when used?

File diff suppressed because one or more lines are too long

View File

@ -22,6 +22,7 @@ coreModules carpDir = map (\s -> carpDir ++ "/core/" ++ s ++ ".carp") [ "Interfa
, "Long"
, "Double"
, "Float"
, "Tuples"
, "Array"
, "Char"
, "Bool"

View File

@ -125,4 +125,12 @@
&[1 2 3 4 5 6 7 8]
&(concat &[[1] [2 3] [4 5 6] [7 8]])
"concat works as expected")
(assert-equal test
&[11 22 33]
&(zip IntRef.+ &[1 2 3 4 5 6 7] &[10 20 30])
"zip works as expected")
(assert-equal test
"[(Pair 0 @\"a\") (Pair 1 @\"b\") (Pair 2 @\"c\")]"
&(str &(Array.enumerated &[@"a" @"b" @"c"]))
"enumerated works as expected")
(print-test-results test))))