Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

calcit.cirru -diff linguist-generated
calcit.cirru diff -linguist-generated
yarn.lock -diff linguist-generated
57 changes: 32 additions & 25 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@ name: Tests

on:
push:
branches:
- main
branches: [main]
pull_request: {}

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v2

- name: ACTIONS_ALLOW_UNSECURE_COMMANDS
id: ACTIONS_ALLOW_UNSECURE_COMMANDS
run: echo 'ACTIONS_ALLOW_UNSECURE_COMMANDS=true' >> $GITHUB_ENV

- name: add cr
run: |
mkdir -p $GITHUB_WORKSPACE/bin
wget -O $GITHUB_WORKSPACE/bin/cr http://repo.calcit-lang.org/binaries/linux/cr
chmod +x $GITHUB_WORKSPACE/bin/cr
echo "::add-path::$GITHUB_WORKSPACE/bin"

- name: "prepare modules"
run: >
mkdir -p ~/.config/calcit/modules/ && cd ~/.config/calcit/modules/
&& git clone https://github.com/calcit-lang/calcit-test.git

- name: "test"
run: cr -1

- run: cr --emit-js -1 && yarn && node ./main.mjs
- uses: actions/checkout@v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@v6.5.0
with:
node-version: 24
package-manager-cache: false
- name: Enable Corepack
run: corepack enable
- uses: calcit-lang/setup-calcit@v1.5.0
with:
version: "0.27.0"
tools: calcit,caps
caps-version: "0.1.1"
- name: Install dependencies
run: |
caps --ci --strict
yarn install --immutable
caps verify --toolchain
- name: Check snapshot and public contracts
run: |
calcit edit format
git diff --exit-code -- calcit.cirru
calcit --check-only
calcit analyze check-public --ns 'arithmetic.$meta' --ns arithmetic.complex --ns arithmetic.test
- name: Run native and JavaScript arithmetic
run: yarn test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@

js-out/
node_modules
.calcit/
.yarn/
compact.cirru
package.cirru

example/
3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules
npmPreapprovedPackages:
- "@calcit/procs@0.27.0"
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@
Arithmetic
----

> for complex numbers
复数运算模块,使用 `[实部, 虚部]` 数字列表;不提供前端资源或服务部署。

### Usages

```cirru
ns demo :require
arithmetic.complex :as complex
ns demo $ :require (arithmetic.complex :as complex)

complex/&+ ([] 1 2) ([] 3 4)
complex/add ([] 1 2) ([] 3 4)
```

Functions:

- `&+ a b`
- `&- a b`
- `&* a b`
- `&/ a b`
- `add a b`
- `subtract a b`
- `multiply a b`
- `divide a b`
- `conjugate a`
- `scale a x-scalar`
- `divide-by a x-scalar`, scales down by factor
- `polar-point radian radius`, create a point from radian

### Workflow

需要正式 Calcit 0.27.0、caps 0.1.1、Node.js 24 和 Yarn 4.18.0:

```sh
caps --ci --strict
yarn install --immutable
yarn test
```

`yarn test` 在 native 和生成的 JavaScript 上运行同一组八种运算断言;
定义附属测试使用内置测试入口,不再依赖旧 `calcit-test` 模块。
默认入口为 native,`yarn compile` 显式生成 JS。仅维护 `calcit.cirru` / `deps.cirru`,
`js-out/` 不进入 Git。模块版本仍为 0.0.1,本次 PR 不代表已发布新版本。

迁移提示:旧 `&+`、`&-`、`&*`、`&/` 分别改为 `add`、`subtract`、`multiply`、`divide`,
调用方需要同步改名。极坐标计算使用跨后端 `cos` / `sin`,不再直接调用 JS Math。
复数运算保留原公式;代码中的 `List<Number>` 契约不证明列表长度,调用方仍需提供有效复数对。

https://github.com/calcit-lang/calcit-workflow

### License
Expand Down
145 changes: 145 additions & 0 deletions calcit.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

{}
:about "|Machine-generated snapshot. Do not edit directly — changes will be overwritten. Use `calcit query` to inspect and `calcit edit`/`calcit tree` to modify. Run `calcit docs agents --contract` before mutations; use `--full` for first orientation or changed contract digest. Manual edits must follow format and schema conventions, then run `calcit edit format`."
:package |arithmetic
:entries $ {} $ :default
{} (:description |) (:init-fn 'arithmetic.test/main!) (:mode :native) (:reload-fn 'arithmetic.test/reload!) (:target :native)
:feature-policy $ {}
:modules $ []
:type-slots $ {}
:files $ {}
'arithmetic.complex $ %{} 'FileEntry
:defs $ {}
'add $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn add (p1 p2)
let
a $ .unwrap $ nth p1 0
b $ .unwrap $ nth p1 1
x $ .unwrap $ nth p2 0
y $ .unwrap $ nth p2 1
[] (+ a x) (+ b y)
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] (:: 'List 'Number) (:: 'List 'Number)
:return $ :: 'List 'Number
'conjugate $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn conjugate (pair)
assoc pair 1 $ negate $ .unwrap (nth pair 1)
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] $ :: 'List 'Number
:return $ :: 'List 'Number
'divide $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn divide (value base) "|complex number division, renamed since naming collision"
let-sugar
[] x y
, value
([] a b) base
inverted $ / 1 $ + (* a a) (* b b)
[]
* inverted $ + (* x a) (* y b)
* inverted $ - (* y a) (* x b)
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] (:: 'List 'Number) (:: 'List 'Number)
:return $ :: 'List 'Number
'divide-by $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn divide-by (point x)
[]
/
.unwrap $ nth point 0
, x
/
.unwrap $ nth point $ dec (count point)
, x
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] (:: 'List 'Number) 'Number
:return $ :: 'List 'Number
'multiply $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn multiply (v1 v2)
let
a $ .unwrap $ nth v1 0
b $ .unwrap $ nth v1 1
x $ .unwrap $ nth v2 0
y $ .unwrap $ nth v2 1
[]
- (* a x) (* b y)
+ (* a y) (* b x)
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] (:: 'List 'Number) (:: 'List 'Number)
:return $ :: 'List 'Number
'polar-point $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn polar-point (angle r)
[]
* r $ cos angle
* r $ sin angle
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] 'Number 'Number
:return $ :: 'List 'Number
'scale $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn scale (pair v)
map pair $ fn (x) (* v x)
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] (:: 'List 'Number) 'Number
:return $ :: 'List 'Number
'subtract $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn subtract (v1 v2)
let
a $ .unwrap $ nth v1 0
b $ .unwrap $ nth v1 1
x $ .unwrap $ nth v2 0
y $ .unwrap $ nth v2 1
[] (- a x) (- b y)
:examples $ []
:schema $ :: 'Fn $ {}
:args $ [] (:: 'List 'Number) (:: 'List 'Number)
:return $ :: 'List 'Number
:ns $ %{} 'NsEntry (:doc |)
:code $ quote $ ns arithmetic.complex
'arithmetic.test $ %{} 'FileEntry
:defs $ {}
'main! $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn main! () (run-tests)
:examples $ []
:schema $ :: 'Fn $ {} (:return 'Unit)
:args $ []
'reload! $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn reload! () (run-tests)
:examples $ []
:schema $ :: 'Fn $ {} (:return 'Unit)
:args $ []
'run-tests $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn run-tests () (test-basic)
:examples $ []
:schema $ :: 'Fn $ {} (:return 'Unit)
:args $ []
'test-basic $ %{} 'CodeEntry (:doc |)
:code $ quote $ defn test-basic ()
assert= ([] 3 5)
2d/add ([] 1 2) ([] 2 3)
assert= ([] -1 -1)
2d/subtract ([] 1 2) ([] 2 3)
assert= ([] -5 10)
2d/multiply ([] 1 2) ([] 3 4)
assert= ([] 1 2)
2d/divide ([] -5 10) ([] 3 4)
assert= ([] 1 -2)
2d/conjugate $ [] 1 2
assert= ([] 3 6)
2d/scale ([] 1 2) 3
assert= ([] 1 2)
2d/divide-by ([] 3 6) 3
assert= ([] 2 0) (2d/polar-point 0 2)
:examples $ []
:schema $ :: 'Fn $ {} (:return 'Unit)
:args $ []
:tests $ [] $ %{} 'TestEntry (:name |complex-operations)
:code $ quote $ test-basic
:tags $ #{} :unit
:ns $ %{} 'NsEntry (:doc |)
:code $ quote $ ns arithmetic.test
:require $ arithmetic.complex :as 2d
77 changes: 0 additions & 77 deletions compact.cirru

This file was deleted.

3 changes: 3 additions & 0 deletions deps.cirru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{} (:calcit-version |0.27.0)
:version |0.0.1
:dependencies $ {}
2 changes: 1 addition & 1 deletion main.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { main_$x_, reload_$x_ } from "./js-out/arithmetic.test.mjs"
import { main_$x_ } from "./js-out/arithmetic.test.mjs"

main_$x_()
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"version": "0.0.1",
"dependencies": {
"@calcit/procs": "^0.5.41"
"@calcit/procs": "0.27.0"
},
"scripts": {},
"devDependencies": {}
"scripts": {
"compile": "calcit js",
"test": "calcit test --require-match && calcit && yarn compile && node main.mjs"
},
"packageManager": "yarn@4.18.0"
}
Loading