Skip to content

Commit d3e4016

Browse files
authored
Merge pull request #12 from nomennescio/feature-margins
Feature: margins
2 parents 53fb333 + a8d54b6 commit d3e4016

14 files changed

+379
-6
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2022 nomennescio, see LICENCSE.md for license
22
.git
3+
.github
34
.dockerignore
45
Dockerfile
56
**/*.md

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ jobs:
1515
wget -q -O - https://downloads.factorcode.org/releases/0.98/factor-linux-x86-64-0.98.tar.gz | tar xzf -
1616
- name: Run Tests
1717
run: |
18-
PATH=~/opt/factor:$PATH python test/run-tests.py -v
18+
PATH=~/opt/factor:$PATH python3 test/run-tests.py -v

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ RUN set -ex; \
1818

1919
COPY tools/testest/testest.factor /opt/factor/work/tools/testest/testest.factor
2020
COPY codewars/imager/imager.factor /opt/factor/work/codewars/imager/imager.factor
21+
COPY math/margins/margins.factor /opt/factor/pre/math/margins/margins.factor
2122

2223
RUN set -ex; \
2324
cd /opt; \

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
Vocabulary to test Factor code on Codewars.
44

5-
### Example
5+
See [documentation](testest.md)
6+
7+
### Example testest setup
68

79
See [example](./example).
810

codewars/imager/imager.factor

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
! Copyright (C) 2022 nomennescio
22
! see LICENSE.md for license
33

4-
USING: memory system vocabs vocabs.hierarchy ;
4+
USING: memory namespaces sequences sequences.rotated system vectors vocabs vocabs.hierarchy vocabs.loader ;
55
IN: codewars.imager
66

77
: load-and-save-image ( -- )
8-
"resource:core" load-root "resource:basis" load-root "tools.testest" require
8+
"resource:pre" add-vocab-root vocab-roots [ -1 <rotated> >vector ] change-global
9+
"resource:extra" vocab-roots get remove [ load-root ] each
910
image-path save-image-and-exit
1011
;
1112

math/margins/authors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nomennescio

math/margins/margins.factor

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
! Copyright 2022 nomennescio
2+
! See LICENSE.md for license
3+
4+
! margins are like intervals with a central value and left and right acceptable margins
5+
! margins compare equal if one contains the other, to support inexact comparisons using equal ("=")
6+
7+
USING: accessors combinators.short-circuit kernel locals make math math.intervals math.parser present prettyprint.custom prettyprint.sections sequences ;
8+
IN: math.margins
9+
10+
<PRIVATE
11+
12+
: -.+ ( a b -- a-b a a+b ) [ - ] [ drop ] [ + ] 2tri ;
13+
14+
! class
15+
16+
TUPLE: margin { range interval read-only } { central real read-only } ; ! preferably margin would be a subclass of interval, but we can't reuse its constructor
17+
TUPLE: abs-margin < margin ; ! margin class with absolute margin
18+
TUPLE: rel-margin < margin ; ! margin class with margin as percentage of central value
19+
20+
! constructors
21+
22+
ERROR: unordered-margin ; ! guards invariant from<=central<=to
23+
24+
:: boa-margin ( from central to class -- margin ) from central <= central to <= and [ from to [a,b] central ] [ unordered-margin ] if class boa ; inline
25+
26+
PRIVATE>
27+
28+
: <margin> ( from central to -- margin ) margin boa-margin ;
29+
: [a-e,a+e] ( a epsilon -- margin ) abs -.+ abs-margin boa-margin ;
30+
: [a-%,a+%] ( a percent -- margin ) over * 100 / abs -.+ rel-margin boa-margin ;
31+
32+
ALIAS: ± [a-e,a+e]
33+
ALIAS: ±% [a-%,a+%]
34+
35+
! methods
36+
37+
! convert object to margin. prerequisite for comparisons
38+
39+
GENERIC: >margin ( obj -- margin )
40+
41+
M: object >margin drop f ;
42+
M: margin >margin ;
43+
M: real >margin dup dup <margin> ;
44+
45+
ALIAS: >± >margin
46+
47+
<PRIVATE
48+
49+
! margins compare equal if one contains the other (not a true equality because it's reflexive and symmetric, but not transitive)
50+
51+
M: margin hashcode* 2drop 0xbef001ed ; ! semi-unique singular value
52+
M: margin equal? over margin? [ [ range>> ] bi@ { [ interval-subset? ] [ swap interval-subset? ] } 2|| ] [ 2drop f ] if ;
53+
54+
! extract values from margin
55+
56+
: margin> ( margin -- from central to ) [ range>> [ from>> ] [ to>> ] bi [ first ] bi@ ] [ central>> ] bi swap ;
57+
58+
! >string conversions
59+
60+
! specific
61+
62+
: margin>string ( margin -- string ) margin> spin [ # "«" % # "»" % # ] "" make ;
63+
: abs-margin>string ( abs-margin -- string ) margin> spin drop [ - ] keep [ # "±" % # ] "" make ;
64+
: rel-margin>string ( rel-margin -- string ) margin> spin drop [ [ - ] keep abs / 100 * ] keep [ # "±" % # "%" % ] "" make ;
65+
66+
! generic
67+
68+
M: margin present margin>string ;
69+
M: abs-margin present abs-margin>string ;
70+
M: rel-margin present rel-margin>string ;
71+
72+
! custom prettyprinting
73+
74+
M: margin pprint* present text ;
75+
76+
PRIVATE>

test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ Simple tests to verify the Factor Testest framework is still working
33
Run all tests by executing
44
run-tests.py
55

6-
Requires Python 2 or 3 and Factor to be in your PATH
6+
Requires Python 3 and Factor to be in your PATH
77

88
To show more verbose output, use -v, to show less verbose output, use -q

test/run-tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
# Copyright 2019-2022 nomennescio
33

44
import glob, os, re, sys
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
<DESCRIBE::>math.margins
3+
4+
<IT::>constructors
5+
6+
<PASSED::>Test Passed
7+
8+
<PASSED::>Test Passed
9+
10+
<PASSED::>Test Passed
11+
12+
<COMPLETEDIN::>0.179599 ms
13+
14+
<IT::>constructors aliases
15+
16+
<PASSED::>Test Passed
17+
18+
<PASSED::>Test Passed
19+
20+
<COMPLETEDIN::>0.013899 ms
21+
22+
<IT::>conversion
23+
24+
<PASSED::>Test Passed
25+
26+
<PASSED::>Test Passed
27+
28+
<PASSED::>Test Passed
29+
30+
<COMPLETEDIN::>0.076472 ms
31+
32+
<IT::>equals
33+
34+
<PASSED::>Test Passed
35+
36+
<PASSED::>Test Passed
37+
38+
<PASSED::>Test Passed
39+
40+
<COMPLETEDIN::>0.050478 ms
41+
42+
<COMPLETEDIN::>0.659280 ms

0 commit comments

Comments
 (0)