Skip to content

Commit be1b68f

Browse files
committed
Refactorings based on Steffan153's work
1 parent 4374e99 commit be1b68f

File tree

40 files changed

+211
-434
lines changed

40 files changed

+211
-434
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
!yamlscript/v0
22

33
defn abbreviate(phrase):
4-
uc(phrase):
5-
.re-seq(/[A-Z']+/)
4+
uc(phrase).re-seq(/[A-Z']+/)
65
.map(first):join
Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
!yamlscript/v0
22

3-
defn rebase(input-base digits output-base):
4-
:: Converts a sequence of digits given in input-base
5-
into a sequence of digits in the desired output-base.
6-
7-
cond:
8-
input-base < 2: die('input base must be >= 2')
9-
output-base < 2: die('output base must be >= 2')
10-
digits.some(neg?) || digits.some(ge(input-base)):
11-
die: 'all digits must satisfy 0 <= d < input base'
12-
13-
digits.every?(zero?) || count(digits).eq(0) :: [0]
14-
15-
else: digits
16-
.digits-to-decimal(input-base)
17-
.decimal-to-digits(output-base)
18-
19-
defn digits-to-decimal(digits input-base):
20-
reduce \(%2 + (%1 * input-base)): digits
21-
22-
defn decimal-to-digits(number output-base):
23-
loop digits nil, num number:
24-
if num > 0:
25-
recur:
26-
conj digits: num % output-base
27-
quot: num output-base
28-
else: digits
3+
defn rebase(input digits output):
4+
when input < 2: die('input base must be >= 2')
5+
when output < 2: die('output base must be >= 2')
6+
when-not digits.every?(\(0 <= _ <= input.--)):
7+
die('all digits must satisfy 0 <= d < input base')
8+
9+
loop n reduce(\((%1 * input) + %2) 0 digits), res []:
10+
if n.?:
11+
=>: recur(quot(n output) res.cons(n % output))
12+
res |||: -[0]
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
!yamlscript/v0
22

3-
allergen-map =:
4-
zipmap _ range()::
5-
- eggs
6-
- peanuts
7-
- shellfish
8-
- strawberries
9-
- tomatoes
10-
- chocolate
11-
- pollen
12-
- cats
13-
143
defn allergic-to(item score):
15-
bit-test score: allergen-map.$item
4+
list-allergens(score).has?(item)
165

176
defn list-allergens(score):
18-
keep _ allergen-map:
19-
fn([allergen num]):
20-
bit-test(score num) &&& allergen
7+
(0 .. 7).filter(P(bit-test score))
8+
.map(qw(eggs peanuts shellfish strawberries
9+
tomatoes chocolate pollen cats))

exercises/practice/anagram/.meta/anagram.ys

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22

33
defn find-anagrams(subject candidates):
44
filter _ candidates:
5-
partial _ subject:
6-
fn(*words):
7-
word1 word2 =: words.map(lc)
8-
word1 != word2 &&:
9-
sort(word1) == sort(word2)
5+
\((_:lc != subject:lc) &&
6+
(_:lc:sort == subject:lc:sort))
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
!yamlscript/v0
22

3-
dict =:
4-
zipmap (\\a .. \\z): \\z .. \\a
5-
63
defn encode(phrase):
7-
lc(phrase):
8-
.replace(/[^a-z0-9]/).escape(dict)
4+
lc(phrase):decode:
5+
.replace(/[^a-z0-9]/)
96
.partition(5 5 '').map(join):joins
107

118
defn decode(phrase):
12-
phrase.replace(' ').escape(dict)
9+
m =: (\\a .. \\z).zipmap(\\z .. \\a)
10+
phrase: .replace(' ').escape(m)
Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,28 @@
11
!yamlscript/v0
22

3-
account =: atom(nil)
3+
closed =: 'closed'
4+
closed? =: P(== closed)
45

56
defn reset-test-state():
6-
reset! account: nil
77

88
defn bank-account(operations):
9-
last:
10-
for operation operations:
11-
operation
12-
.operation
13-
.str('do-' _)
14-
.call(operation)
15-
16-
defn- do-open(op):
17-
when deref(account):
18-
die: 'account already open'
19-
reset! account: 0
20-
21-
defn- do-close(op):
22-
when-not deref(account):
23-
die: 'account not open'
24-
reset! account: nil
25-
26-
defn- do-balance(op):
27-
balance =: deref(account)
28-
if-not balance:
29-
die: 'account not open'
30-
else: balance
31-
32-
defn- do-deposit(op):
33-
when-not deref(account):
34-
die: 'account not open'
35-
swap! account +: op:get-amount
36-
37-
defn- do-withdraw(op):
38-
balance =: deref(account)
39-
when-not balance:
40-
die: 'account not open'
41-
amount =: op:get-amount
42-
when amount > balance:
43-
die: 'amount must be less than balance'
44-
swap! account -: amount
45-
46-
defn- get-amount(op):
47-
amount =: op.amount
48-
if amount < 0:
49-
die: 'amount must be greater than 0'
50-
else: amount
9+
reduce _ closed operations:
10+
fn(bal op):
11+
case op.operation:
12+
-'open':
13+
if closed?(bal): 0, die('account already open')
14+
-'close':
15+
if closed?(bal): die('account not open'), closed
16+
-'balance':
17+
if closed?(bal): die('account not open'), bal
18+
-'deposit':
19+
cond:
20+
closed?(bal): die('account not open')
21+
op.amount <= 0: die('amount must be greater than 0')
22+
else: bal + op.amount
23+
-'withdraw':
24+
cond:
25+
closed?(bal): die('account not open')
26+
op.amount <= 0: die('amount must be greater than 0')
27+
op.amount > bal: die('amount must be less than balance')
28+
else: bal - op.amount
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
!yamlscript/v0
22

33
defn find(array value):
4-
loop low 0, high array.--:
5-
when low > high:
6-
die: 'value not in array'
7-
mid =: (high + low).quot(2)
8-
item =: array.$mid
9-
10-
cond:
11-
value == item : mid
12-
value < item : recur(low mid.--)
13-
else : recur(mid.++ high)
4+
when array.#.!: die('value not in array')
5+
mid =: array.#.--.quot(2)
6+
case compare(array.$mid value):
7+
0: mid
8+
1: find(take(mid array) value)
9+
-1: find(drop(mid.++ array) value) + mid.++

exercises/practice/circular-buffer/.meta/circular-buffer.ys

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
!yamlscript/v0
22

33
defn run(capacity operations):
4-
buffer =: M(:B V(capacity * [nil]), :P 0)
4+
buffer =: -{:B V(capacity * [nil]), :P 0}
55
reduce _ buffer operations:
66
fn(buffer op):
77
op item pass want =:
@@ -10,14 +10,14 @@ defn run(capacity operations):
1010
args =:
1111
condp eq op:
1212
-'clear' : -[buffer]
13-
-'read' : L(buffer pass want)
14-
-'write' : L(buffer pass item)
15-
-'overwrite' : L(buffer pass item)
13+
-'read' : -[buffer pass want]
14+
-'write' : -[buffer pass item]
15+
-'overwrite' : -[buffer pass item]
1616
value("op-$op"): args*
1717
=>: -{}
1818

1919
defn op-clear(buffer):
20-
M: :B V(buffer.B.# * [nil]) :P 0
20+
hash-map: :B V(buffer.B.# * [nil]) :P 0
2121

2222
defn op-read(buffer pass want):
2323
cell =: buffer.B.nth(buffer.P)
@@ -26,8 +26,9 @@ defn op-read(buffer pass want):
2626
die: S("Can't read:\ " buffer)
2727
when cell != want:
2828
die: "Bad read:\ $cell != $want ($buffer)"
29-
M: :B update-in(buffer.B [buffer.P] constantly(nil))
30-
:P (buffer.P.++ % buffer.B.#)
29+
hash-map:
30+
:B update-in(buffer.B [buffer.P] constantly(nil))
31+
:P (buffer.P.++ % buffer.B.#)
3132

3233
defn op-write(buffer pass item):
3334
B P =: buffer.slice(q(B P))
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
!yamlscript/v0
22

33
defn egg-count(number):
4-
loop num number, eggs 0:
5-
if num > 0:
6-
recur: quot(num 2), eggs.add(num % 2)
7-
else: eggs
4+
len: P(bit-test number).filter(0 .. 31)

exercises/practice/etl/.meta/etl.ys

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
defn transform(legacy):
44
into {}:
55
for [score letters] legacy, letter letters:
6-
V: lc(letter) score:N
6+
-[lc(letter) score:N]

0 commit comments

Comments
 (0)