Skip to content

Commit b2adfca

Browse files
committed
Add arithmetic functions
1 parent 4e8aa67 commit b2adfca

File tree

3 files changed

+105
-36
lines changed

3 files changed

+105
-36
lines changed

README.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,39 @@ This project is build for fun :)
77
## Functions
88

99
1. Most functions have two arities: `fn($list)` or `fn($list...)`.
10-
2. Anonymous functions use polish notation (`(add 1 2 3)`) and allow `_` or `_1`, `_2` ... to represent the first, the second arguments and so on.
10+
2. Anonymous functions use polish notation (`(plus 1 2 3)`) and allow `_` or `_1`, `_2` ... to represent the first, the second arguments and so on.
1111

1212
### Math Functions
1313

14-
#### `add`
14+
#### `plus`
1515

1616
```scss
17-
add(1, 2, 3) // 6
18-
add("#", "a", "b", "c") // "#abc"
17+
plus(1, 2, 3) // 6
18+
plus("#", "a", "b", "c") // "#abc"
1919

20-
add([1, 2, 3]) // 6
21-
add(["#", "a", "b", "c"]) // "#abc"
20+
plus([1, 2, 3]) // 6
21+
plus(["#", "a", "b", "c"]) // "#abc"
22+
```
23+
24+
#### `minus`
25+
26+
```scss
27+
minus(1, 2, 3) // -4
28+
minus([1, 2, 3]) // -4
29+
```
30+
31+
#### `multiply`
32+
33+
```scss
34+
multiply(1, 2, 3) // 6
35+
multiply([1, 2, 3]) // 6
36+
```
37+
38+
#### `divide`
39+
40+
```scss
41+
divide(1, 2, 3) // 1/6
42+
divide([1, 2, 3]) // 1/6
2243
```
2344

2445
#### `inc`
@@ -168,10 +189,10 @@ conj((a: 1, b: 2), (c: 3)); // (a: 1, b: 2, c: 3)
168189

169190
```scss
170191
map(inc, 1, 2, 3) // (2, 3, 4)
171-
map((add _ 1), 1, 2, 3) // (2, 3, 4)
192+
map((plus _ 1), 1, 2, 3) // (2, 3, 4)
172193

173194
map(inc, [1, 2, 3]) // (2, 3, 4)
174-
map((add _ 1), [1, 2, 3]) // (2, 3, 4)
195+
map((plus _ 1), [1, 2, 3]) // (2, 3, 4)
175196
```
176197

177198
#### `filter`
@@ -189,11 +210,11 @@ filter((greater _ 1), 1, 2, 3) // (2 3)
189210
`reduce($fn, $init, $list)` or `redure($fn, $init, $list...)`
190211

191212
```scss
192-
reduce((add _1 _2), 0, 1, 2, 3) // 6
193-
reduce((add _1 _2 _2), "#", "a", "b", "c") // "#aabbcc"
213+
reduce((plus _1 _2), 0, 1, 2, 3) // 6
214+
reduce((plus _1 _2 _2), "#", "a", "b", "c") // "#aabbcc"
194215

195-
reduce((add _1 _2), 0, [1, 2, 3]) // 6
196-
reduce((add _1 _2 _2), "#", ["a", "b", "c"]) // "#aabbcc"
216+
reduce((plus _1 _2), 0, [1, 2, 3]) // 6
217+
reduce((plus _1 _2 _2), "#", ["a", "b", "c"]) // "#aabbcc"
197218
```
198219

199220
#### `some`
@@ -223,21 +244,21 @@ every(odd, [-1, 0, 9]) // false
223244
Same as the `as->` macro in Clojure.
224245

225246
```scss
226-
thread-as(1, inc, (add 10 _ 5), inc, dec) // 17
247+
thread-as(1, inc, (plus 10 _ 5), inc, dec) // 17
227248
```
228249

229250
#### `thread-first`
230251

231252
Same as the `->` macro in Clojure.
232253

233254
```scss
234-
thread-first(1, inc, (add 10 5), inc, dec) // 17
255+
thread-first(1, inc, (plus 10 5), inc, dec) // 17
235256
```
236257

237258
#### `thread-last`
238259

239260
Same as the `->>` macro in Clojure.
240261

241262
```scss
242-
thread-last(1, inc, (add 10 5), inc, dec) // 17
263+
thread-last(1, inc, (plus 10 5), inc, dec) // 17
243264
```

__tests__/test.scss

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,43 @@
2828

2929
@include test("__call-fn") {
3030
@include assert-equal(__call-fn("inc", 1), 2);
31-
@include assert-equal(__call-fn("add", 1, 2, 3), 6);
31+
@include assert-equal(__call-fn("plus", 1, 2, 3), 6);
3232
}
3333

3434
@include test("__call-lambda") {
35-
@include assert-equal(__call-lambda((add _ 10), 1), 11);
36-
@include assert-equal(__call-lambda((add _1 _2), 1, 2), 3);
35+
@include assert-equal(__call-lambda((plus _ 10), 1), 11);
36+
@include assert-equal(__call-lambda((plus _1 _2), 1, 2), 3);
3737
}
3838

3939
@include test("__auto-call") {
4040
@include assert-equal(__auto-call("inc", 1), 2);
41-
@include assert-equal(__auto-call("add", 1, 2, 3), 6);
42-
@include assert-equal(__auto-call((add _ 10), 1), 11);
43-
@include assert-equal(__auto-call((add _1 _2), 1, 2), 3);
41+
@include assert-equal(__auto-call("plus", 1, 2, 3), 6);
42+
@include assert-equal(__auto-call((plus _ 10), 1), 11);
43+
@include assert-equal(__auto-call((plus _1 _2), 1, 2), 3);
4444
}
4545
}
4646

4747
@include test-module("Test math functions:") {
48-
@include test("add") {
49-
@include assert-equal(add([1, 2, 3]), 6);
50-
@include assert-equal(add([ "#", "a", "b", "c" ]), "#abc");
51-
@include assert-equal(add(1, 2, 3), 6);
52-
@include assert-equal(add("#", "a", "b", "c"), "#abc");
48+
@include test("plus") {
49+
@include assert-equal(plus([1, 2, 3]), 6);
50+
@include assert-equal(plus(1, 2, 3), 6);
51+
@include assert-equal(plus([ "#", "a", "b", "c" ]), "#abc");
52+
@include assert-equal(plus("#", "a", "b", "c"), "#abc");
53+
}
54+
55+
@include test("minus") {
56+
@include assert-equal(minus([1, 2, 3]), -4);
57+
@include assert-equal(minus(1, 2, 3), -4);
58+
}
59+
60+
@include test("multiply") {
61+
@include assert-equal(multiply([1, 2, 3]), 6);
62+
@include assert-equal(multiply(1, 2, 3), 6);
63+
}
64+
65+
@include test("divide") {
66+
@include assert-equal(divide([1, 2, 3]), 1/6);
67+
@include assert-equal(divide(1, 2, 3), 1/6);
5368
}
5469

5570
@include test("inc") {
@@ -217,10 +232,10 @@
217232
@include test-module("Test iterator functions:") {
218233
@include test("map") {
219234
@include assert-equal(map(inc, [1 2 3]), (2 3 4));
220-
@include assert-equal(map((add 1 _), [1 2 3]), (2 3 4));
235+
@include assert-equal(map((plus 1 _), [1 2 3]), (2 3 4));
221236
@include assert-equal(map(first, [[1] [2] [3]]), (1 2 3));
222237
@include assert-equal(map(inc, 1, 2, 3), (2 3 4));
223-
@include assert-equal(map((add 1 _), 1, 2, 3), (2 3 4));
238+
@include assert-equal(map((plus 1 _), 1, 2, 3), (2 3 4));
224239
@include assert-equal(map(first, [1], [2], [3]), (1 2 3));
225240
}
226241

@@ -232,14 +247,14 @@
232247
}
233248

234249
@include test("reduce") {
235-
@include assert-equal(reduce((add _1 _2), 0, (1 2 3)), 6);
250+
@include assert-equal(reduce((plus _1 _2), 0, (1 2 3)), 6);
236251
@include assert-equal(
237-
reduce((add _1 _2 _2), "#", [ "a", "b", "c" ]),
252+
reduce((plus _1 _2 _2), "#", [ "a", "b", "c" ]),
238253
"#aabbcc"
239254
);
240-
@include assert-equal(reduce((add _1 _2), 0, 1, 2, 3), 6);
255+
@include assert-equal(reduce((plus _1 _2), 0, 1, 2, 3), 6);
241256
@include assert-equal(
242-
reduce((add _1 _2 _2), "#", "a", "b", "c"),
257+
reduce((plus _1 _2 _2), "#", "a", "b", "c"),
243258
"#aabbcc"
244259
);
245260
}
@@ -261,14 +276,14 @@
261276

262277
@include test-module("Test thread functions:") {
263278
@include test("thread-as") {
264-
@include assert-equal(thread-as(1, inc, (add 10 _ 5), inc, dec), 17);
279+
@include assert-equal(thread-as(1, inc, (plus 10 _ 5), inc, dec), 17);
265280
}
266281

267282
@include test("thread-first") {
268-
@include assert-equal(thread-first(1, inc, (add 10 5), inc, dec), 17);
283+
@include assert-equal(thread-first(1, inc, (plus 10 5), inc, dec), 17);
269284
}
270285

271286
@include test("thread-last") {
272-
@include assert-equal(thread-last(1, inc, (add 10 5), inc, dec), 17);
287+
@include assert-equal(thread-last(1, inc, (plus 10 5), inc, dec), 17);
273288
}
274289
}

lambda.scss

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
// Math functions //
8181
// ---------------- //
8282

83-
@function add($list...) {
83+
@function plus($list...) {
8484
$_list: __arglist($list);
8585
$_result: list.nth($_list, 1);
8686

@@ -91,6 +91,39 @@
9191
@return $_result;
9292
}
9393

94+
@function minus($list...) {
95+
$_list: __arglist($list);
96+
$_result: list.nth($_list, 1);
97+
98+
@for $_i from 2 through list.length($_list) {
99+
$_result: $_result - list.nth($_list, $_i);
100+
}
101+
102+
@return $_result;
103+
}
104+
105+
@function multiply($list...) {
106+
$_list: __arglist($list);
107+
$_result: list.nth($_list, 1);
108+
109+
@for $_i from 2 through list.length($_list) {
110+
$_result: $_result * list.nth($_list, $_i);
111+
}
112+
113+
@return $_result;
114+
}
115+
116+
@function divide($list...) {
117+
$_list: __arglist($list);
118+
$_result: list.nth($_list, 1);
119+
120+
@for $_i from 2 through list.length($_list) {
121+
$_result: $_result / list.nth($_list, $_i);
122+
}
123+
124+
@return $_result;
125+
}
126+
94127
@function inc($n) {
95128
@return $n + 1;
96129
}

0 commit comments

Comments
 (0)