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
+1-2
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
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]
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

+2-5
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))
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)
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
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

+8-7
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))
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

+1-1
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]

exercises/practice/grains/.meta/grains.ys

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
defn square(square):
44
(1 <= square <= 64) |||:
55
die: 'square must be between 1 and 64'
6-
7-
pow 2: square - 1
6+
2 **: square.--

exercises/practice/hamming/.meta/hamming.ys

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
defn distance(strand1 strand2):
44
strand1.# == strand2.# ||:
55
die: 'strands must be of equal length'
6-
7-
len: map(ne strand1 strand2).filter(a)
6+
sum: map(ne strand1 strand2).map(N)
+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
!yamlscript/v0
22

3-
defn isogram(string):
4-
empty?(string) ||:
5-
lc(string):split \"Want to be case insensitive"
6-
.filter(/^[a-z]$/) \"Keep letters only"
7-
.distinct?(*) \"Check if distinct letters"
3+
defn isogram(phrase):
4+
phrase !~ /(?i)(\w).*\1/

exercises/practice/list-ops/.meta/list-ops.ys

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,4 @@ defn foldl(list initial function):
1919
list.reduce(function initial)
2020

2121
defn foldr(list initial function):
22-
if len(list) > 0:
23-
function _ list:first:
24-
foldr: rest(list) initial function
25-
else: initial
26-
27-
defn reverse(list):
28-
core/reverse: list
22+
list:reverse.reduce(function initial)

exercises/practice/luhn/.meta/luhn.ys

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
!yamlscript/v0
22

33
defn valid(value):
4-
digits =: value.replace(' ')
5-
6-
and:
7-
digits.# >=: 2
8-
digits !~: /[^\d]/
9-
digits:split:
10-
.map(to-num):reverse:V
11-
.conj(0).partition(2)
12-
.map(munge):flat:sum
13-
.mod(10).eq(0)
14-
15-
defn munge([a b]):
16-
b *=: 2
17-
vector a:
18-
if b > 9: (b - 9) b
4+
digits =: value.replace(' ').map(\(_ - \\0)):reverse
5+
digits.#.gt(1) &&: digits.every?(\(0 <= _ <= 9)) &&
6+
map(int.comp(mul) digits [1 2.2]:cycle):sum.mod(10).!
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
!yamlscript/v0
22

3-
pairs =: reverse('()[]{}'):M
4-
opening =: vals(pairs):set
5-
closing =: keys(pairs):set
6-
73
defn is-paired(value):
8-
loop stack [], [char *chars] value:
9-
cond:
10-
char:nil?: stack:count:zero?
11-
opening(char):
12-
recur: conj(stack char) chars
13-
closing(char):
14-
if peek(stack) == pairs(char):
15-
recur: pop(stack) chars
16-
else: false
17-
else: recur(stack chars)
4+
x =: value.replace(/\[\]|\(\)|\{\}|[^(){}\[\]]/)
5+
cond:
6+
x == '': true
7+
x == value: false
8+
else: is-paired(x)
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
!yamlscript/v0
22

33
defn prime(number):
4-
lazy-primes().nth(number.--)
5-
6-
defn- lazy-primes(d=2 table={}):
7-
if-let factors table.$d:
8-
recur d.++:
9-
reduce _ table.dissoc(d) factors:
10-
\(update-in(%1 [(%2 + d)] conj %2))
11-
12-
lazy-seq:
13-
cons d:
14-
lazy-primes d.++:
15-
assoc table: sqr(d) [d]
4+
loop n 2, u 0:
5+
cond:
6+
u == number: n.--
7+
range(2 sqrt(n):I.++).every?(\((n % _).?)):
8+
recur(n.++ u.++)
9+
else: recur(n.++ u)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
!yamlscript/v0
22

3-
nucleotides =: zipmap(qw(A C G T) repeat(0))
4-
53
defn nucleotide-counts(strand):
6-
when strand !~ /^[ACGT]*$/:
4+
when strand =~ /[^ACGT]/:
75
die: 'Invalid nucleotide in strand'
86

9-
reduce-kv _ nucleotides frequencies(strand):
10-
fn(map key val): map.assoc(str(key) val)
7+
into zipmap(qw(A C G T) repeat(0)):
8+
frequencies(strand.map(str))
+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
!yamlscript/v0
22

33
defn is-pangram(sentence):
4-
26 ==: lc(sentence)
5-
.replace(/[^a-z]/)
6-
.distinct():len
4+
B: sentence =~ /(?i)(?>.*?(\pL)(?!.*\1)){26}/
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
!yamlscript/v0
22

33
defn rows(count):
4-
reduce _ [] range(1 count.++):
5-
fn(tri row):
6-
conj tri:
7-
or _ [1]:
8-
when row > 1:
9-
mapv _ (-1 .. row.sub(2)):
10-
\(last(tri).get(_):N.or(0) +
11-
last(tri).get(_.++):N.or(0))
4+
\(mapv + _.cons(0) _.conj(0))
5+
.iterate([1]).take(count)

0 commit comments

Comments
 (0)