mirror of
https://github.com/janet-lang/janet
synced 2025-01-12 16:40:27 +00:00
Add triangles examples.
This commit is contained in:
parent
b70d6cad1b
commit
080caf31a7
39
examples/maxtriangle.dst
Normal file
39
examples/maxtriangle.dst
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
(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))
|
@ -127,25 +127,25 @@ static int cfun_slice(DstArgs args) {
|
|||||||
|
|
||||||
static int cfun_prepend(DstArgs args) {
|
static int cfun_prepend(DstArgs args) {
|
||||||
const Dst *t;
|
const Dst *t;
|
||||||
|
int32_t len;
|
||||||
Dst *n;
|
Dst *n;
|
||||||
if (args.n != 2) return dst_throw(args, "expected 2 arguments");
|
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");
|
if (!dst_seq_view(args.v[0], &t, &len)) return dst_throw(args, "expected tuple/array");
|
||||||
t = dst_unwrap_tuple(args.v[0]);
|
n = dst_tuple_begin(len + 1);
|
||||||
n = dst_tuple_begin(dst_tuple_length(t) + 1);
|
memcpy(n + 1, t, sizeof(Dst) * len);
|
||||||
memcpy(n + 1, t, sizeof(Dst) * dst_tuple_length(t));
|
|
||||||
n[0] = args.v[1];
|
n[0] = args.v[1];
|
||||||
return dst_return(args, dst_wrap_tuple(dst_tuple_end(n)));
|
return dst_return(args, dst_wrap_tuple(dst_tuple_end(n)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cfun_append(DstArgs args) {
|
static int cfun_append(DstArgs args) {
|
||||||
const Dst *t;
|
const Dst *t;
|
||||||
|
int32_t len;
|
||||||
Dst *n;
|
Dst *n;
|
||||||
if (args.n != 2) return dst_throw(args, "expected 2 arguments");
|
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");
|
if (!dst_seq_view(args.v[0], &t, &len)) return dst_throw(args, "expected tuple/array");
|
||||||
t = dst_unwrap_tuple(args.v[0]);
|
n = dst_tuple_begin(len + 1);
|
||||||
n = dst_tuple_begin(dst_tuple_length(t) + 1);
|
memcpy(n, t, sizeof(Dst) * len);
|
||||||
memcpy(n, t, sizeof(Dst) * dst_tuple_length(t));
|
n[len] = args.v[1];
|
||||||
n[dst_tuple_length(t)] = args.v[1];
|
|
||||||
return dst_return(args, dst_wrap_tuple(dst_tuple_end(n)));
|
return dst_return(args, dst_wrap_tuple(dst_tuple_end(n)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user