Skip to content

Commit

Permalink
Refactorings for v0.1.77
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Sep 25, 2024
1 parent f731bf2 commit 5ccbda2
Show file tree
Hide file tree
Showing 49 changed files with 224 additions and 187 deletions.
4 changes: 3 additions & 1 deletion exercises/practice/acronym/.meta/acronym.ys
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
!yamlscript/v0

defn abbreviate(phrase):
uc(phrase).re-seq(/[A-Z']+/).map(first).str(*)
uc(phrase):
.re-seq(/[A-Z']+/)
.map(first):join
14 changes: 7 additions & 7 deletions exercises/practice/all-your-base/.meta/all-your-base.ys
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
!yamlscript/v0

defn rebase(input-base digits output-base):
:: Converts a sequence of digits given in input-base into a sequence of
digits in the desired output-base.
:: Converts a sequence of digits given in input-base
into a sequence of digits in the desired output-base.

cond:
input-base < 2: die('input base must be >= 2')
input-base < 2: die('input base must be >= 2')
output-base < 2: die('output base must be >= 2')
digits.some(neg?) || digits.some(\(_ >= input-base)):
digits.some(neg?) || digits.some(ge(input-base)):
die: 'all digits must satisfy 0 <= d < input base'

digits.every?(zero?) || digits.! :: [0]
digits.every?(zero?) || count(digits).eq(0) :: [0]

else: digits
.digits-to-decimal(input-base)
.decimal-to-digits(output-base)

defn digits-to-decimal(digits input-base):
reduce \((%1 * input-base) + %2): 0 digits
reduce \(%2 + (%1 * input-base)): digits

defn decimal-to-digits(number output-base):
loop digits nil, num number:
if num > 0:
recur:
conj: digits (num % output-base)
conj digits: num % output-base
quot: num output-base
else: digits
7 changes: 4 additions & 3 deletions exercises/practice/anagram/.meta/anagram.ys
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
defn find-anagrams(subject candidates):
filter _ candidates:
partial _ subject:
fn(*):
-[word1 word2] =: _.map(lc)
word1 != word2 &&: sort(word1) == sort(word2)
fn(*words):
word1 word2 =: words.map(lc)
word1 != word2 &&:
sort(word1) == sort(word2)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
!yamlscript/v0

defn is-armstrong-number(number):
number ==: map(\(_ ** str(number).#) digits(number)).sum()
raise =: \(_ ** digits(number).#)
number ==: digits(number).map(raise):sum
7 changes: 4 additions & 3 deletions exercises/practice/atbash-cipher/.meta/atbash-cipher.ys
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ dict =:
zipmap (\\a .. \\z): \\z .. \\a

defn encode(phrase):
lc(phrase).replace(/[^a-z0-9]/ '').str/escape(dict)
.partition(5 5 '').map(join).join(' ')
lc(phrase):
.replace(/[^a-z0-9]/).escape(dict)
.partition(5 5 '').map(join):joins

defn decode(phrase):
phrase.replace(' ' '').str/escape(dict)
phrase.replace(' ').escape(dict)
6 changes: 3 additions & 3 deletions exercises/practice/bank-account/.meta/bank-account.ys
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ defn- do-balance(op):
defn- do-deposit(op):
when-not deref(account):
die: 'account not open'
swap! account +: op.get-amount()
swap! account +: op:get-amount

defn- do-withdraw(op):
balance =: deref(account)
when-not balance:
die: 'account not open'
amount =: op.get-amount()
amount =: op:get-amount
when amount > balance:
die: 'amount must be less than balance'
swap! account -: amount

defn- get-amount(op):
amount =: op.amount
if amount <= 0:
if amount < 0:
die: 'amount must be greater than 0'
else: amount
5 changes: 3 additions & 2 deletions exercises/practice/binary-search/.meta/binary-search.ys
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
!yamlscript/v0

defn find(array value):
loop low 0, high array.#--:
loop low 0, high array.--:
when low > high:
die: 'value not in array'
mid =: quot((high + low) 2)
mid =: (high + low).quot(2)
item =: array.$mid

cond:
value == item : mid
value < item : recur(low mid.--)
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/bob/.meta/bob.ys
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
!yamlscript/v0

defn response(hey-bob):
condp re-matches hey-bob.trim():
condp re-matches hey-bob:trim:
/\s*/ : 'Fine. Be that way!'
/[^a-zA-Z]+\?/ : 'Sure.'
/[^a-z]+\?/ : "Calm down, I know what I'm doing!"
Expand Down
12 changes: 6 additions & 6 deletions exercises/practice/bottle-song/.meta/bottle-song.ys
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
!yamlscript/v0

defn recite(start-bottles take-down):
lines:
join "\n":
map verse: start-bottles .. (start-bottles - take-down).++
nums =: start-bottles ..
(start-bottles - take-down).++
nums: .map(verse).join("\n"):lines

defn- verse(num): |
$uc1(bottles(num)) hanging on the wall,
$uc1(bottles(num)) hanging on the wall,
And if one green bottle should accidentally fall,
There'll be $bottles(num - 1) hanging on the wall.
There'll be $bottles(num.--) hanging on the wall.

dict =:
nums =:
zipmap (2 .. 10):
qw(two three four five six seven eight nine ten)

defn- bottles(num):
case num:
0 : 'no green bottles'
1 : 'one green bottle'
else : "$(dict.$num) green bottles"
else : "$(nums.$num) green bottles"
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
!yamlscript/v0

defn steps(number):
when-not pos?(number):
die('Only positive integers are allowed')
number > 0 ||:
die: 'Only positive integers are allowed'

loop num number, steps 0:
cond:
num == 1: steps
even?(num): recur((num / 2), steps.++)
else: recur((num * 3).++, steps.++)
num.eq(1) : steps
num:even? : recur(num.div(2), steps.++)
else : recur(num.mul(3).++, steps.++)
14 changes: 10 additions & 4 deletions exercises/practice/diamond/.meta/diamond.ys
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
!yamlscript/v0

defn rows(letter):
loop C letter.0, I (' ' * (((-64 + C) * 2) - 3)), O '', dmnd '':
line =: if(I.? "$O$C$I$C$O\n" "$O$C$O\n")
dmnd =: if(dmnd.? "$line$dmnd$line" line)
if I.?: recur(C.--, I.drop(2).join(), "$O ", dmnd), dmnd
ipad =:
letter.0:N.sub(64).mul(2).sub(3) * ' '

loop C letter.0, I ipad, O '', dmnd '':
line =: I.?.if("$O$C$I$C$O\n" "$O$C$O\n")
dmnd =: dmnd.?.if("$line$dmnd$line" line)

if I.?:
recur: C.--, I.chop(2), "$O ", dmnd
else: dmnd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ defn square-of-sum(number):
sqr: sum(1 .. number)

defn sum-of-squares(number):
1 .. number: .map(sqr).sum()
sum: (1 .. number).map(sqr)

defn difference-of-squares(number):
square-of-sum(number) -: sum-of-squares(number)
square-of-sum(number) -
sum-of-squares(number)
6 changes: 3 additions & 3 deletions exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

defn egg-count(number):
loop num number, eggs 0:
if num.?:
recur: quot(num 2), (eggs + (num % 2))
else: eggs + num
if num > 0:
recur: quot(num 2), eggs.add(num % 2)
else: eggs
2 changes: 1 addition & 1 deletion exercises/practice/etl/.meta/etl.ys
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
defn transform(legacy):
into {}:
for [score letters] legacy, letter letters:
vector: lc(letter) score.to-num()
V: lc(letter) score:N
4 changes: 2 additions & 2 deletions exercises/practice/grains/.meta/grains.ys
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
!yamlscript/v0

defn square(square):
when-not 1 <= square <= 64:
(1 <= square <= 64) |||:
die: 'square must be between 1 and 64'

2 **: square.--
pow 2: square - 1
4 changes: 2 additions & 2 deletions exercises/practice/hamming/.meta/hamming.ys
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
!yamlscript/v0

defn distance(strand1 strand2):
when strand1.# != strand2.#:
strand1.# == strand2.# ||:
die: 'strands must be of equal length'

strand1: .map(ne _ strand2).filter(true?).#
len: map(ne strand1 strand2).filter(a)
7 changes: 3 additions & 4 deletions exercises/practice/isogram/.meta/isogram.ys
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
!yamlscript/v0

defn isogram(string):
string.! ||:
lc(string) \"Want to be case insensitive"
.split() \"Split string into chars"
.filter(\(/^\w$/)) \"Keep letters only"
empty?(string) ||:
lc(string):split \"Want to be case insensitive"
.filter(/^[a-z]$/) \"Keep letters only"
.distinct?(*) \"Check if distinct letters"
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

defn largest-product(digits span):
cond:
span < 0: die('span must not be negative')
span > digits.#: die('span must be smaller than string length')
digits =~ /[^0-9]/: die('digits input must only contain digits')
else: digits.split('').map(to-num)
.partition(span 1)
.map(\(_.mul(*))).max(*)
span < 0 : die('span must not be negative')
span > digits.# : die('span must be smaller than string length')
digits =~ /[^\d]/ : die('digits input must only contain digits')

digits.split(''):
.map(to-num)
.partition(span 1)
.map(\(_.mul(*)))
.max(*)
4 changes: 3 additions & 1 deletion exercises/practice/leap/.meta/leap.ys
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
!yamlscript/v0

defn is-leap-year(year):
(year % 4).! &&: (year % 100).? || (year % 400).!
(year % 4):zero? &&:
(year % 100):pos? ||:
(year % 400):zero?
12 changes: 6 additions & 6 deletions exercises/practice/list-ops/.meta/list-ops.ys
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ defn append(list1 list2):
list1 + list2

defn concat(lists):
lists.conj([]).clojure::core/concat(*)
lists.mapcat(a)

defn filter(list function):
list.filter(function)

defn length(list):
list.#
list:count

defn map(list function):
list.map(function)

defn foldl(list initial function):
reduce function: initial list
list.reduce(function initial)

defn foldr(list initial function):
if list.?:
function _ list.0:
if len(list) > 0:
function _ list:first:
foldr: rest(list) initial function
else: initial

defn reverse(list):
clojure::core/reverse(list)
core/reverse: list
16 changes: 9 additions & 7 deletions exercises/practice/luhn/.meta/luhn.ys
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
!yamlscript/v0

defn valid(value):
digits =: value.replace(' ' '')
digits =: value.replace(' ')

and:
digits.# >=: 2
digits !~: /[^0-9]/
digits: .split().remove(empty?).map(to-num)
.reverse().vec().conj(0).partition(2)
.map(munge).flatten().sum().rem(10).!
digits !~: /[^\d]/
digits:split:
.map(to-num):reverse:V
.conj(0).partition(2)
.map(munge):flat:sum
.mod(10).eq(0)

defn munge([a b]):
b =: 2 * b
vector a:
b *=: 2
V a:
if b > 9: (b - 9) b
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
!yamlscript/v0

pairs =: seq('()[]{}').reverse().to-map()
opening =: set(vals(pairs))
closing =: set(keys(pairs))
pairs =: reverse('()[]{}'):M
opening =: vals(pairs):set
closing =: keys(pairs):set

defn is-paired(value):
loop stack [], [char *chars] value:
cond:
char.!: stack.!
char:nil?: stack:count:zero?
opening(char):
recur: conj(stack char) chars
closing(char):
Expand Down
10 changes: 5 additions & 5 deletions exercises/practice/minesweeper/.meta/minesweeper.ys
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
!yamlscript/v0

defn annotate(minefield):
grid =: minefield.map(split).vec()
grid =: minefield.map(split):V
reduce-kv _ [] grid:
fn(acc x row):
height =: grid.#--
width =: grid.0.#--
height =: grid.--
width =: grid.0.--
conj acc:
reduce-kv _ '' row:
fn(acc y cell):
str acc:
or? _ cell:
when cell == ' ':
when cell:blank?:
sum:
? for i (-1 .. 1) j (-1 .. 1)
:let [row (x + i), col (y + j)]
: to-num:
((i != 0) || (j != 0)) &&
(i.ne(0) || j.ne(0)) &&
(0 <= row <= height) &&
(0 <= col <= width) &&
(grid.$row.$col == '*')
3 changes: 2 additions & 1 deletion exercises/practice/nth-prime/.meta/nth-prime.ys
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defn- lazy-primes(d=2 table={}):
recur d.++:
reduce _ table.dissoc(d) factors:
\(update-in(%1 [(%2 + d)] conj %2))

lazy-seq:
cons d:
lazy-primes d.++:
table.assoc(sqr(d) [d])
assoc table: sqr(d) [d]
6 changes: 4 additions & 2 deletions exercises/practice/pangram/.meta/pangram.ys
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
!yamlscript/v0

defn is-pangram(sentence):
sentence.lc().replace(/[^a-z]/ '')
.sort().distinct().len().eq(26)
26 ==:
len: lc(sentence)
.replace(/[^a-z]/)
.distinct()
Loading

0 comments on commit 5ccbda2

Please sign in to comment.