Skip to content

Commit b29f622

Browse files
randomness and calendar concepts
1 parent d94bdd8 commit b29f622

66 files changed

Lines changed: 2704 additions & 3 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ This is enforced by the "Learning checks" workflow.
1515
bin/add-practice-exercise <exercise-slug>
1616
```
1717

18+
- An exercise should include a `.meta/tests.toml` file if, and only if, the exercise has a `canonical-data.json` in [problem-specifications](https://github.com/exercism/problem-specifications/tree/main/exercises).
19+
1820
#### **Do you want to report a bug?**
1921

2022
- **Ensure the bug was not already reported** by searching the [forum](https://forum.exercism.org/c/programming/factor).

bin/extract-known-words

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ x y z w m k i j p q r s u v n
2525
seq seq1 seq2 quot quot1 quot2 quot3 quot: quot:: assoc
2626
key value old elt elt1 elt2 bool string str
2727
old-x new-x 3x 2x n+1 n+10% n+1% length+kerf length+10%
28-
days new-days new-days-or-f symbol direction direction'
28+
new-days new-days-or-f symbol direction direction'
2929
amount rate years seconds planet period arr sortedstr count/f
3030
p' dx dy sx sy points transformed
3131
price prices item items base-speed
@@ -42,7 +42,7 @@ recipe-step depth >>slot slot>> change-slot
4242
test
4343
n*x n*y n+x n+y newseq newkey newvalue sortedseq headseq tailseq padded
4444
inputs outputs difference label
45-
hour minute hours minutes word words
45+
word words
4646
phrase\'
4747
""".split())
4848

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"blurb": "The calendar vocabulary: <date> and >date< build and split timestamps, day-of-week, leap-year?, and days-in-month query them, and durations like days shift them with time+."
6+
}

concepts/calendar/about.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# About
2+
3+
Factor's [`calendar`][calendar] vocabulary is built around two tuples:
4+
5+
- `timestamp` — a point in time (`year`, `month`, `day`, `hour`,
6+
`minute`, `second`, and a GMT offset).
7+
- `duration` — a span of time (the same fields, used as amounts).
8+
9+
## Timestamps
10+
11+
`<date>` builds a midnight timestamp; `>date<` splits one apart:
12+
13+
```
14+
<date> ( year month day -- timestamp )
15+
>date< ( timestamp -- year month day )
16+
```
17+
18+
```factor
19+
USING: calendar ;
20+
21+
2024 7 14 <date> >date< . ! => 14 (2024 and 7 below it)
22+
```
23+
24+
Common queries:
25+
26+
```factor
27+
USING: calendar calendar.english sequences ;
28+
29+
2024 1 8 <date> day-of-week . ! => 1 (0 = Sunday .. 6 = Saturday)
30+
2024 1 8 <date> day-of-week day-names nth . ! => "Monday"
31+
2024 leap-year? . ! => t
32+
2024 2 1 <date> days-in-month . ! => 29
33+
```
34+
35+
`day-names` (and `month-names`) live in
36+
[`calendar.english`][english].
37+
38+
## Durations and arithmetic
39+
40+
Words like `days`, `weeks`, `hours`, and `minutes` build a `duration`,
41+
and `time+` / `time-` shift a timestamp by one:
42+
43+
```
44+
days ( x -- duration )
45+
time+ ( timestamp duration -- timestamp )
46+
```
47+
48+
```factor
49+
2024 2 28 <date> 1 days time+ >date< . ! => 29 (2024-02-29)
50+
```
51+
52+
Arithmetic carries across month and year boundaries, and two
53+
timestamps naming the same instant compare equal with `=`.
54+
55+
Beyond these, `calendar` offers times of day (`set-time`, `+hour`),
56+
time-zone conversions (`>gmt`, `>local-time`), the current time
57+
(`now`), and — via `calendar.format` — parsing and formatting.
58+
59+
[calendar]: https://docs.factorcode.org/content/vocab-calendar.html
60+
[english]: https://docs.factorcode.org/content/vocab-calendar.english.html

concepts/calendar/introduction.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Introduction
2+
3+
Factor's [`calendar`][calendar] vocabulary represents a point in time as
4+
a `timestamp` and a span of time as a `duration`.
5+
6+
```factor
7+
USING: calendar ;
8+
9+
2024 7 14 <date> . ! a timestamp at midnight on 2024-07-14
10+
2024 7 14 <date> >date< . ! => 14 (2024 and 7 below it)
11+
```
12+
13+
`day-of-week` returns `0` for Sunday through `6` for Saturday;
14+
`leap-year?` and `days-in-month` answer the usual calendar questions:
15+
16+
```factor
17+
2024 1 8 <date> day-of-week . ! => 1
18+
2024 leap-year? . ! => t
19+
2024 2 1 <date> days-in-month . ! => 29
20+
```
21+
22+
Durations such as `days` and `weeks` shift a timestamp with `time+`:
23+
24+
```factor
25+
2024 2 28 <date> 1 days time+ >date< .
26+
! => 29 (2024-02-29)
27+
```
28+
29+
[calendar]: https://docs.factorcode.org/content/vocab-calendar.html

concepts/calendar/links.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://docs.factorcode.org/content/vocab-calendar.html",
4+
"description": "calendar vocabulary reference"
5+
},
6+
{
7+
"url": "https://docs.factorcode.org/content/vocab-calendar.english.html",
8+
"description": "calendar.english — day and month names"
9+
}
10+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"blurb": "The random vocabulary: random picks values and elements, randomize and sample reorder sequences, and with-random plus <mersenne-twister> make results reproducible."
6+
}

concepts/randomness/about.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# About
2+
3+
Factor concentrates randomness in the [`random`][random] vocabulary
4+
around a single generic word:
5+
6+
```
7+
random ( obj -- elt )
8+
```
9+
10+
It dispatches on its argument:
11+
12+
- an **integer** `n` yields a random integer in `[0, n)`;
13+
- a **sequence** yields a random element;
14+
- assocs, hash-sets, and `uniform-distribution` tuples are also
15+
supported.
16+
17+
Two sequence helpers build on it:
18+
19+
```factor
20+
USING: random ;
21+
22+
{ 1 2 3 4 5 } randomize . ! shuffle in place (Fisher–Yates)
23+
{ 1 2 3 4 5 } 3 sample . ! 3 distinct elements; error if too many
24+
```
25+
26+
`randomize` mutates and returns the sequence, so `clone` first if you
27+
need to keep the original order.
28+
29+
## Where the randomness comes from
30+
31+
`random` reads the generator held in the `random-generator` dynamic
32+
variable. By default that's a Mersenne Twister seeded from the system
33+
clock, so every process differs. You can install a different generator
34+
for the duration of a quotation:
35+
36+
```
37+
<mersenne-twister> ( seed -- rnd )
38+
with-random ( rnd quot -- )
39+
```
40+
41+
```factor
42+
USING: random random.mersenne-twister ;
43+
44+
42 <mersenne-twister> [ { 1 2 3 } randomize ] with-random .
45+
! => the same ordering on every run
46+
```
47+
48+
Because `with-random` simply binds `random-generator` for the dynamic
49+
extent of the quotation, a word that wraps it should be marked
50+
`inline` so the quotation can leave its result on the stack for the
51+
caller.
52+
53+
Beyond these, `random` offers `random-bits`, `random-unit`,
54+
`uniform-random`, and `normal-random` for other distributions, plus
55+
`system-random-generator` and `secure-random-generator` for
56+
non-reproducible and cryptographic needs.
57+
58+
[random]: https://docs.factorcode.org/content/vocab-random.html
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Introduction
2+
3+
Factor's [`random`][random] vocabulary provides one generic word,
4+
`random`, that adapts to its argument, plus helpers for shuffling and
5+
sampling sequences.
6+
7+
```factor
8+
USING: random ;
9+
10+
6 random . ! => a value from 0..5
11+
{ "rock" "paper" "scissors" } random . ! => a random element
12+
{ 1 2 3 4 5 } randomize . ! => the same cards, reordered
13+
{ 1 2 3 4 5 } 2 sample . ! => two distinct elements
14+
```
15+
16+
`random` of an integer `n` returns a value in `[0, n)`; of a sequence
17+
it returns a random element. `randomize` shuffles in place, and
18+
`sample` draws distinct elements.
19+
20+
By default these draw from a global generator seeded from the system
21+
clock. To make outcomes reproducible, install a seeded generator with
22+
`<mersenne-twister>` (from [`random.mersenne-twister`][mt]) and
23+
`with-random`:
24+
25+
```factor
26+
USING: random random.mersenne-twister ;
27+
28+
42 <mersenne-twister> [ 6 random ] with-random . ! => same value every run
29+
```
30+
31+
[random]: https://docs.factorcode.org/content/vocab-random.html
32+
[mt]: https://docs.factorcode.org/content/vocab-random.mersenne-twister.html

concepts/randomness/links.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://docs.factorcode.org/content/vocab-random.html",
4+
"description": "random vocabulary reference"
5+
},
6+
{
7+
"url": "https://docs.factorcode.org/content/vocab-random.mersenne-twister.html",
8+
"description": "random.mersenne-twister — seeded, reproducible generators"
9+
}
10+
]

0 commit comments

Comments
 (0)