Skip to content

Commit

Permalink
implement destruct functions; tag 0.7.9
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jul 31, 2023
1 parent d0bd682 commit cf5df55
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit"
version = "0.7.8"
version = "0.7.9"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
13 changes: 7 additions & 6 deletions calcit/test-map.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@

&let
m $ &{} :a 1 :b 2 :c 3 :d 4
assert= (first m) (first m)
assert= (rest m) (rest m)
assert= 3 (count $ rest m)
assert= (first $ &map:destruct m) (first $ &map:destruct m)
assert= (last $ &map:destruct m) (last $ &map:destruct m)
assert= 3 (count $ last $ &map:destruct m)

assert= 10
foldl m 0 $ fn (acc pair)
Expand Down Expand Up @@ -365,13 +365,14 @@
#{} 1 2 3
.values $ &{} :a 1 :b 2 :c 3

println $ .destruct $ &{} :a 1 :b 2 :c 3
assert= true
list? $ .first $ &{} :a 1 :b 2 :c 3
list? $ nth (.destruct $ &{} :a 1 :b 2 :c 3) 0
assert= 2
count $ .first $ &{} :a 1 :b 2 :c 3
count $ nth (.destruct $ &{} :a 1 :b 2 :c 3) 0

assert= 2
.count $ .rest $ &{} :a 1 :b 2 :c 3
.count $ last $ .destruct $ &{} :a 1 :b 2 :c 3

assert=
&{} :c 3
Expand Down
56 changes: 56 additions & 0 deletions calcit/test-tuple.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

{} (:package |test-tuple)
:configs $ {} (:init-fn |test-tuple.main/main!) (:reload-fn |test-tuple.main/reload!)
:files $ {}
|test-tuple.main $ {}
:ns $ quote
ns test-tuple.main $ :require
util.core :refer $ log-title
:defs $ {}

|main! $ quote
defn main! ()
log-title "|Testing tuple"

assert=
:: :parts |1 |23
tag-match (destruct-str "|123")
(:none) (:: :empty)
(:some s0 ss) (:: :parts s0 ss)

assert=
:: :empty
tag-match (destruct-str "|")
(:none) (:: :empty)
(:some s0 ss) (:: :parts s0 ss)

assert=
:: :parts 1 $ [] 2 3
tag-match (destruct-list $ [] 1 2 3)
(:none) (:: :empty)
(:some l0 ls) (:: :parts l0 ls)

assert=
:: :empty
tag-match (destruct-list $ [])
(:none) (:: :empty)
(:some l0 ls) (:: :parts l0 ls)

assert=
:: :parts true 2
tag-match (destruct-set $ #{} 1 2 3)
(:none) (:: :empty)
(:some l0 ls) (:: :parts (number? l0) (count ls))

assert=
:: :empty
tag-match (destruct-set $ #{})
(:none) (:: :empty)
(:some l0 ls) (:: :parts (number? l0) (count ls))

assert=
:: :empty
tag-match (destruct-map $ &{})
(:none) (:: :empty)
(:some m0 ms) (:: :parts (count m0) (count ms))

4 changes: 3 additions & 1 deletion calcit/test.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
, |./test-lens.cirru |./test-list.cirru |./test-macro.cirru |./test-map.cirru
, |./test-math.cirru |./test-recursion.cirru |./test-set.cirru
, |./test-string.cirru |./test-js.cirru |./test-record.cirru
, |./test-nil.cirru |./test-fn.cirru |./test-algebra.cirru
, |./test-nil.cirru |./test-fn.cirru |./test-tuple.cirru |./test-algebra.cirru
, |./util.cirru
:files $ {}
|app.main $ {}
Expand All @@ -25,6 +25,7 @@
test-record.main :as test-record
test-nil.main :as test-nil
test-fn.main :as test-fn
test-tuple.main :as test-tuple
test-algebra.main :as test-algebra
util.core :refer $ log-title inside-eval: inside-js:
:defs $ {}
Expand Down Expand Up @@ -380,6 +381,7 @@
test-record/main!
test-nil/main!
test-fn/main!
test-tuple/main!
test-algebra/main!

test-buffer
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.7.8",
"version": "0.7.9",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^20.4.4",
Expand Down
3 changes: 1 addition & 2 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ fn handle_proc_internal(name: CalcitProc, args: &CalcitItems, call_stack: &CallS
CalcitProc::NativeMapEmpty => maps::empty_ques(args),
CalcitProc::NativeMapContains => maps::contains_ques(args),
CalcitProc::NativeMapIncludes => maps::includes_ques(args),
CalcitProc::NativeMapFirst => maps::first(args),
CalcitProc::NativeMapRest => maps::rest(args),
CalcitProc::NativeMapDestruct => maps::destruct(args),
CalcitProc::NativeMapGet => maps::get(args),
CalcitProc::NativeMapAssoc => maps::assoc(args),
CalcitProc::NativeMapDissoc => maps::dissoc(args),
Expand Down
24 changes: 7 additions & 17 deletions src/builtins/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,31 +195,21 @@ pub fn includes_ques(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
}
}

/// use builtin function since maps need to be handled specifically
pub fn first(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
match xs.get(0) {
Some(Calcit::Map(ys)) => match ys.iter().next() {
// TODO order may not be stable enough
Some((k, v)) => Ok(Calcit::List(TernaryTreeList::from(&[k.to_owned(), v.to_owned()]))),
None => Ok(Calcit::Nil),
},
Some(a) => CalcitErr::err_str(format!("map:first expected a map, got: {a}")),
None => CalcitErr::err_str("map:first expected 1 argument"),
}
}

pub fn rest(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
pub fn destruct(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
match xs.get(0) {
Some(Calcit::Map(ys)) => match ys.keys().next() {
// order not stable
Some(k0) => {
let mut zs = ys.to_owned();
zs.remove_mut(k0);
Ok(Calcit::Map(zs))
Ok(Calcit::List(
vec![Calcit::List(vec![k0.to_owned(), ys[k0].to_owned()].into()), Calcit::Map(zs)].into(),
))
}
None => Ok(Calcit::Nil),
},
Some(a) => CalcitErr::err_str(format!("map:rest expected a map, got: {a}")),
None => CalcitErr::err_str("map:rest expected 1 argument"),
Some(a) => CalcitErr::err_str(format!("&map:destruct expected a map, got: {a}")),
None => CalcitErr::err_str("&map:destruct expected 1 argument"),
}
}

Expand Down
37 changes: 26 additions & 11 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -1517,30 +1517,34 @@
|&core-set-class $ quote
defrecord! &core-set-class
:add include
:contains? &set:includes?
:count &set:count
:destruct &set:destruct
:difference difference
:exclude exclude
:empty $ defn &set:empty (x) (#{})
:empty? &set:empty?
:exclude exclude
:filter &set:filter
:include include
:includes? &set:includes?
:contains? &set:includes?
:intersection intersection
:mappend union
:max &set:max
:min &set:min
:to-list &set:to-list
:union union
:destruct &set:destruct
:to-set identity
:mappend union
:union union

|&core-map-class $ quote
defrecord! &core-map-class
:add &map:add-entry
:assoc &map:assoc
:common-keys &map:common-keys
:contains? &map:contains?
:count &map:count
:destruct &map:destruct
:diff-keys &map:diff-keys
:diff-new &map:diff-new
:dissoc &map:dissoc
:empty $ defn &map:empty (x) (&{})
:empty? &map:empty?
Expand All @@ -1556,14 +1560,9 @@
:mappend merge
:merge merge
:to-list &map:to-list
:to-map identity
:to-pairs to-pairs
:values vals
:first &map:first
:rest &map:rest
:diff-new &map:diff-new
:diff-keys &map:diff-keys
:common-keys &map:common-keys
:to-map identity

|&core-record-class $ quote
defrecord! &core-record-class
Expand Down Expand Up @@ -1841,6 +1840,22 @@
if (&= xs $ [])
:: :none
:: :some (nth xs 0) (&list:slice xs 1)

|destruct-set $ quote
defn destruct-set (xs)
&let
pair $ &set:destruct xs
if (nil? pair)
:: :none
:: :some (nth pair 0) (nth pair 1)

|destruct-map $ quote
defn destruct-map (xs)
&let
pair $ &map:destruct xs
if (nil? pair)
:: :none
:: :some (nth pair 0) (nth pair 1)

|optionally $ quote
defn optionally (s)
Expand Down
9 changes: 3 additions & 6 deletions src/primes/proc_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ pub enum CalcitProc {
NativeMapEmpty,
NativeMapContains,
NativeMapIncludes,
NativeMapFirst,
NativeMapRest,
NativeMapDestruct,
NativeMapAssoc,
NativeMapDiffNew,
NativeMapDiffKeys,
Expand Down Expand Up @@ -324,8 +323,7 @@ impl FromStr for CalcitProc {
"&map:empty?" => Ok(Self::NativeMapEmpty),
"&map:contains?" => Ok(Self::NativeMapContains),
"&map:includes?" => Ok(Self::NativeMapIncludes),
"&map:first" => Ok(Self::NativeMapFirst),
"&map:rest" => Ok(Self::NativeMapRest),
"&map:destruct" => Ok(Self::NativeMapDestruct),
"&map:assoc" => Ok(Self::NativeMapAssoc),
"&map:diff-new" => Ok(Self::NativeMapDiffNew),
"&map:diff-keys" => Ok(Self::NativeMapDiffKeys),
Expand Down Expand Up @@ -496,8 +494,7 @@ impl Display for CalcitProc {
Self::NativeMapEmpty => write!(f, "&map:empty?"),
Self::NativeMapContains => write!(f, "&map:contains?"),
Self::NativeMapIncludes => write!(f, "&map:includes?"),
Self::NativeMapFirst => write!(f, "&map:first"),
Self::NativeMapRest => write!(f, "&map:rest"),
Self::NativeMapDestruct => write!(f, "&map:destruct"),
Self::NativeMapAssoc => write!(f, "&map:assoc"),
Self::NativeMapDiffNew => write!(f, "&map:diff-new"),
Self::NativeMapDiffKeys => write!(f, "&map:diff-keys"),
Expand Down
41 changes: 12 additions & 29 deletions ts-src/calcit.procs.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// CALCIT VERSION
export const calcit_version = "0.7.8";
export const calcit_version = "0.7.9";

import { parse, ICirruNode } from "@cirru/parser.ts";
import { writeCirruCode } from "@cirru/writer.ts";
Expand Down Expand Up @@ -497,26 +497,27 @@ export let _$n_str_$o_first = (xs: CalcitValue): CalcitValue => {
console.error(xs);
throw new Error("Expected a string");
};
export let _$n_map_$o_first = (xs: CalcitValue): CalcitValue => {

export let _$n_map_$o_destruct = (xs: CalcitValue): CalcitValue => {
if (xs instanceof CalcitMap || xs instanceof CalcitSliceMap) {
// TODO order may not be stable enough
let ys = xs.pairs();
if (ys.length > 0) {
return new CalcitSliceList(ys[0]);
// order not stable
if (xs.len() > 0) {
let pair = xs.pairs()[0];
let k0 = pair[0];
return new CalcitSliceList([new CalcitSliceList(pair), xs.dissoc(k0)]);
} else {
return null;
}
}
console.error(xs);
throw new Error("Expected a map");
};
export let _$n_set_$o_first = (xs: CalcitValue): CalcitValue => {
if (xs instanceof CalcitSet) {
return xs.first();
}

export let _$n_set_$o_destruct = (xs: CalcitValue): CalcitValue => {
if (xs instanceof CalcitSet) return xs.destruct();

console.error(xs);
throw new Error("Expected a set");
throw new Error("Expect a set");
};

export let timeout_call = (duration: number, f: CalcitFn): null => {
Expand Down Expand Up @@ -547,24 +548,6 @@ export let _$n_str_$o_rest = (xs: CalcitValue): CalcitValue => {
console.error(xs);
throw new Error("Expects a string");
};
export let _$n_set_$o_rest = (xs: CalcitValue): CalcitValue => {
if (xs instanceof CalcitSet) return xs.rest();

console.error(xs);
throw new Error("Expect a set");
};
export let _$n_map_$o_rest = (xs: CalcitValue): CalcitValue => {
if (xs instanceof CalcitMap || xs instanceof CalcitSliceMap) {
if (xs.len() > 0) {
let k0 = xs.pairs()[0][0];
return xs.dissoc(k0);
} else {
return new CalcitSliceMap([]);
}
}
console.error(xs);
throw new Error("Expected map");
};

export let recur = (...xs: CalcitValue[]): CalcitRecur => {
return new CalcitRecur(xs);
Expand Down
Loading

0 comments on commit cf5df55

Please sign in to comment.