1
0
mirror of https://github.com/janet-lang/janet synced 2025-06-24 01:14:12 +00:00

Merge branch 'master' of github.com:bakpakin/dst

This commit is contained in:
Calvin Rose 2018-03-25 21:12:43 -04:00
commit 6ace978ab8
6 changed files with 128 additions and 61 deletions

View File

@ -136,8 +136,7 @@ body once, and then memoizes the result."
# data structures as their values are references to this
# data structures. Same is true for iterators
(defn filter2 [pred coll]
(tail (iter2lazy (filter pred coll))))
(defn filter2 [pred coll] (iter2lazy (filter pred coll)))
# be careful with the filter function. First element in (filter pos? arr) is nil
# last element is false

42
examples/maxtriangle.dst Normal file
View File

@ -0,0 +1,42 @@
# Find the maximum path from the top (root)
# of the triangle to the leaves of the triangle.
(defn vmap2 [f m1 m2]
(def len (length m1))
(def arr @[])
(for [i 0 len] (array-push arr (f (get m1 i) (get m2 i))))
arr)
(defn reduce [s f c]
(var res s)
(for [i 0 (length c)]
(:= res (f res (get c i))))
res)
(defn max1 [l r] (if (< l r) r l))
(defn myfold [xs ys]
(def xs1 (tuple-prepend xs 0))
(def xs2 (tuple-append xs 0))
(def m1 (vmap2 + xs1 ys))
(def m2 (vmap2 + xs2 ys))
(vmap2 max1 m1 m2))
(defn max [a]
(var m (get a 0))
(for [i 0 (length a)]
(if (< m (get a i))
(:= m (get a i))))
m)
(defn maxpath [t]
(max (reduce () myfold t)))
(def triangle @[
@[3]
@[7 10]
@[4 3 7]
@[8 9 1 3]
])
(print (maxpath triangle))

22
examples/utils.dst Normal file
View File

@ -0,0 +1,22 @@
(defn put-in [coll keys val]
(defn assoc [the-coll n]
(if-let [current-key (get keys n)
current-val (get the-coll current-key)]
(put the-coll current-key (assoc current-val (inc n)))
val))
(assoc coll 0))
(defn update-in [coll keys an-fn]
(def new-keys (array-slice coll 0 -2) )
(def last-key (get (array-slice coll -1 -2) 0))
(defn assoc [the-coll n]
(if-let [current-key (get keys n)
current-val (get the-coll current-key)]
(put the-coll current-key (assoc current-val (inc n)))
( update the-coll last-key an-fn )))
(assoc coll new-keys 0))
;; (defn update-in-test [ ] (update-in @{:a "x" :b {:y {"pipa" 3}}} [:b :y "pipa"] type))

View File

@ -24,8 +24,8 @@
(do
(def defn* (get (get _env 'defn) :value))
(fn [name & more]
(def args (array-concat @[] name :macro more))
(apply1 defn* args))))
(apply1 defn* (array-concat
@[name :macro] more)))))
(defmacro defmacro-
"Define a private macro that will not be exported."
@ -368,26 +368,30 @@ If no match is found, returns nil"
(tuple 'when (tuple not condition) exp-1))
(defmacro if-let
"Takes the first one or two forms in a vector and if true binds
"Takes the first one or two forms in a vector and if both are true binds
all the forms with let and evaluates the first expression else
evaluates the second"
[bindings then else]
(def head (ast-unwrap1 bindings))
(tuple 'let head
(tuple 'if (and (get head 1) (if (get head 2) (get head 3) true))
(tuple 'let bindings
(tuple 'if (tuple 'and (tuple 'get bindings 1)
(tuple 'if
(tuple '> (tuple 'length bindings) 2)
(tuple 'get bindings 3) 'true))
then
else)))
(defmacro when-let
"Takes the first one or two forms in vector and if true binds
all the forms with let and evaluates body"
all the forms with let and evaluates the body"
[bindings & body]
(def head (ast-unwrap1 bindings))
(tuple 'let head
(tuple 'let bindings
(tuple
'when
(and (get head 1) (if (get head 2) (get head 3) true))
(apply1 tuple (array-concat @['do] (ast-unwrap1 body))))))
(tuple 'and (tuple 'get bindings 1)
(tuple 'if
(tuple '> (tuple 'length bindings) 2)
(tuple 'get bindings 3) 'true))
(apply1 tuple (array-concat @['do] body)))))
(defn comp
"Takes multiple functions and returns a function that is the composition
@ -404,21 +408,21 @@ If no match is found, returns nil"
(tuple-slice functions 5 -1)))))
(defn zipcoll
"Creates an table or tuple from two arrays/tuples. Result is table if no
third argument is given"
"Creates an table or tuple from two arrays/tuples. If a third argument of
:struct is givent resault is struct else is table."
[coll-1 coll-2 the-type]
(var zipping-table @{})
(def {:more more1 :next next1} (iter coll-1))
(def {:more more2 :next next2} (iter coll-2))
(while (and (more1) (more2))
(put zipping-table (next1) (next2)))
(if (= :struct the-type)
(if (struct? the-type)
(table-to-struct zipping-table)
zipping-table))
(defn update
"Accepts a key argument and passes its associated value to a function.
The key, then is associated to that value"
"Accepts a key argument and passes its' associated value to a function.
The key then, is associated to the function's return value"
[coll a-key a-function & args]
(def old-value (get coll a-key))
(put coll a-key (apply a-function old-value args)))

View File

@ -614,7 +614,7 @@ DstSlot dstc_fn(DstFopts opts, DstAst *ast, int32_t argn, const Dst *argv) {
return ret;
}
/* Keep in lexographic order */
/* Keep in lexicographic order */
static const DstSpecial dstc_specials[] = {
{":=", dstc_varset},
{"ast-quote", dstc_astquote},

View File

@ -127,25 +127,25 @@ static int cfun_slice(DstArgs args) {
static int cfun_prepend(DstArgs args) {
const Dst *t;
int32_t len;
Dst *n;
if (args.n != 2) return dst_throw(args, "expected 2 arguments");
if (!dst_checktype(args.v[0], DST_TUPLE)) return dst_throw(args, "expected tuple");
t = dst_unwrap_tuple(args.v[0]);
n = dst_tuple_begin(dst_tuple_length(t) + 1);
memcpy(n + 1, t, sizeof(Dst) * dst_tuple_length(t));
if (!dst_seq_view(args.v[0], &t, &len)) return dst_throw(args, "expected tuple/array");
n = dst_tuple_begin(len + 1);
memcpy(n + 1, t, sizeof(Dst) * len);
n[0] = args.v[1];
return dst_return(args, dst_wrap_tuple(dst_tuple_end(n)));
}
static int cfun_append(DstArgs args) {
const Dst *t;
int32_t len;
Dst *n;
if (args.n != 2) return dst_throw(args, "expected 2 arguments");
if (!dst_checktype(args.v[0], DST_TUPLE)) return dst_throw(args, "expected tuple");
t = dst_unwrap_tuple(args.v[0]);
n = dst_tuple_begin(dst_tuple_length(t) + 1);
memcpy(n, t, sizeof(Dst) * dst_tuple_length(t));
n[dst_tuple_length(t)] = args.v[1];
if (!dst_seq_view(args.v[0], &t, &len)) return dst_throw(args, "expected tuple/array");
n = dst_tuple_begin(len + 1);
memcpy(n, t, sizeof(Dst) * len);
n[len] = args.v[1];
return dst_return(args, dst_wrap_tuple(dst_tuple_end(n)));
}