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 22, 2024
1 parent ecde613 commit 25b12c7
Show file tree
Hide file tree
Showing 46 changed files with 164 additions and 138 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()
6 changes: 3 additions & 3 deletions exercises/practice/all-your-base/.meta/all-your-base.ys
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
!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')
output-base < 2: die('output base must be >= 2')
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?) || digits.#.eq(0) :: [0]

else: digits
.digits-to-decimal(input-base)
Expand Down
3 changes: 2 additions & 1 deletion exercises/practice/anagram/.meta/anagram.ys
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ defn find-anagrams(subject candidates):
partial _ subject:
fn(*words):
-[word1 word2] =: words.map(lc)
word1 != word2 &&: sort(word1) == sort(word2)
word1 != word2 &&:
sort(word1) == sort(word2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
defn is-armstrong-number(number):
eq number:
sum:
map \(_ ** len(str(number))):
digits(number)
map \(_ ** number.digits().len()):
number.digits()
2 changes: 1 addition & 1 deletion exercises/practice/atbash-cipher/.meta/atbash-cipher.ys
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dict =:
defn encode(phrase):
lc(phrase):
.replace(/[^a-z0-9]/).escape(dict)
.partition(5 5 '').map(join).join(' ')
.partition(5 5 '').map(join).joins()

defn decode(phrase):
phrase.replace(' ').escape(dict)
2 changes: 1 addition & 1 deletion exercises/practice/bank-account/.meta/bank-account.ys
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ defn- do-withdraw(op):

defn- get-amount(op):
amount =: op.amount
if amount <= 0:
if amount < 0:
die: 'amount must be greater than 0'
else: amount
1 change: 1 addition & 0 deletions exercises/practice/binary-search/.meta/binary-search.ys
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defn find(array value):
die: 'value not in array'
mid =: (high + low).quot(2)
item =: array.$mid

cond:
value == item : mid
value < item : recur(low mid.--)
Expand Down
8 changes: 4 additions & 4 deletions exercises/practice/bottle-song/.meta/bottle-song.ys
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
!yamlscript/v0

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

defn- verse(num): |
$uc1(bottles(num)) hanging on the wall,
Expand Down
5 changes: 3 additions & 2 deletions exercises/practice/diamond/.meta/diamond.ys
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
!yamlscript/v0

defn rows(letter):
ipad =: letter.0.to-num().sub(64).mul(2).sub(3) * ' '
ipad =:
letter.0.to-num().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.--, join(drop(2 I)), "$O ", dmnd
recur: C.--, I.drop(2).join(), "$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)
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()
+[lc(letter) score.to-num()]
6 changes: 3 additions & 3 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:
die: 'square must be between 1 and 64'
(1 <= square <= 64) |||:
die('square must be between 1 and 64')

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

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

strand1: .map(ne _ strand2).filter(true?).#
strand1.# == strand2.# ||:
die('strands must be of equal length')
len: map(ne strand1 strand2).filter(a)
3 changes: 2 additions & 1 deletion exercises/practice/isogram/.meta/isogram.ys
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
!yamlscript/v0

defn isogram(string):
string.! ||:
empty?(string) ||:
lc(string) \"Want to be case insensitive"
.split() \"Split string into chars"
.filter(/^\w$/) \"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).eq(0) &&:
(year % 100).pos?() ||
(year % 400).eq(0)
8 changes: 4 additions & 4 deletions exercises/practice/list-ops/.meta/list-ops.ys
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ 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.?:
if list.# > 0:
function _ list.0:
foldr: rest(list) initial function
else: initial

defn reverse(list):
clojure::core/reverse(list)
core/reverse: list
9 changes: 5 additions & 4 deletions exercises/practice/luhn/.meta/luhn.ys
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ defn valid(value):
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.split():
.map(to-num).reverse().vec()
.conj(0).partition(2).map(munge)
.flatten().sum().mod(10).eq(0)

defn munge([a b]):
b =: 2 * b
b *=: 2
vector 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('()[]{}').to-map()
opening =: vals(pairs).set()
closing =: keys(pairs).set()

defn is-paired(value):
loop stack [], [char *chars] value:
cond:
char.!: stack.!
char.nil?(): stack.# == 0
opening(char):
recur: conj(stack char) chars
closing(char):
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/minesweeper/.meta/minesweeper.ys
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ defn annotate(minefield):
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]
4 changes: 2 additions & 2 deletions exercises/practice/pangram/.meta/pangram.ys
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
!yamlscript/v0

defn is-pangram(sentence):
sentence.lc().replace(/[^a-z]/)
.sort().distinct().len().eq(26)
26 ==: lc(sentence).replace(/[^a-z]/)
.sort().distinct().len()
5 changes: 3 additions & 2 deletions exercises/practice/pascals-triangle/.meta/pascals-triangle.ys
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ defn rows(count):
conj tri:
or _ [1]:
when row > 1:
mapv _ (-1 .. (row - 2)):
\(to-num(get(last(tri) _)) + to-num(get(last(tri) _.++)))
mapv _ (-1 .. row.sub(2)):
\(tri.last().get(_).to-num() +
tri.last().get(_.++).to-num())
4 changes: 1 addition & 3 deletions exercises/practice/perfect-numbers/.meta/perfect-numbers.ys
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ defn classify(number):

defn sum-of-divisors(number):
sum:
? for n range(1 quot(number 2).++)
:when (number % n).!
: n
for n range(1 quot(number 2).++) :when (number % n).eq(0): n
20 changes: 10 additions & 10 deletions exercises/practice/phone-number/.meta/phone-number.ys
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
defn clean(phrase):
numstr =: phrase.replace(/[^\d]/)

when phrase.lc() =~ /[a-z]/: die('letters not permitted')
when phrase =~ /@/: die('punctuations not permitted')
phrase =~ /[a-z]/ &&: die('letters not permitted')
phrase.has?('@') &&: die('punctuations not permitted')

case numstr.#:
9: die('must not be fewer than 10 digits')
range(8): die('incorrect number of digits')
11:
when numstr.0 != \\1: die('11 digits must start with 1')
else: nil
when numstr.# > 11: die('must not be greater than 11 digits')
0 .. 9: die('must not be fewer than 10 digits')
10: nil
11: (numstr.0 == \\1) || die('11 digits must start with 1')
else: die('must not be greater than 11 digits')

numstr .=: take-last(10).join()

cond:
numstr =~ /^0/: die('area code cannot start with zero')
numstr =~ /^1/: die('area code cannot start with one')
numstr.0 == \\0: die('area code cannot start with zero')
numstr.0 == \\1: die('area code cannot start with one')
numstr.3 == \\0: die('exchange code cannot start with zero')
numstr.3 == \\1: die('exchange code cannot start with one')
else: numstr
24 changes: 14 additions & 10 deletions exercises/practice/pig-latin/.meta/pig-latin.ys
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
!yamlscript/v0

trans =:
-{ /^([aeiou]|yt|xr).*/ \(_.0 + 'ay')
/^(s?qu|thr|sch|[crst]h|[^aeiou])(.*)/ \("$(_.2)$(_.1)ay") }
rules =::
^(?:[aeiou]|yt|xr).*::
\(_ + 'ay')
^(s?qu|thr|sch|[crst]h|[^aeiou])(.*)::
\("$(_.2)$(_.1)ay")

defn translate(phrase): words(phrase).map(pig-latin).join(' ')

defn pig-latin(word):
reduce-kv _ nil trans:
fn(trans regex function):
match =: word =~ regex
trans ||: match && function(match)
defn translate(phrase):
join ' ':
map _ words(phrase):
fn(word):
reduce-kv _ nil rules:
fn(translated regex function):
match =: word =~ qr(regex)
translated ||:
match && function(match)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defn proteins(strand):
proteins =:
strand.str('XX').partition(3)
.map(join).map(translate-codon)
.take-while(\(_ != 'STOP'))
.take-while(/^(?!STOP)/)

if proteins.has?('INVALID'):
die: 'Invalid codon'
Expand Down
1 change: 1 addition & 0 deletions exercises/practice/queen-attack/.meta/queen-attack.ys
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

defn create(queen):
-[r c] =: queen.position.map(qw(row column))

cond:
r < 0 : die('row not positive')
r > 7 : die('row not on board')
Expand Down
14 changes: 9 additions & 5 deletions exercises/practice/raindrops/.meta/raindrops.ys
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
!yamlscript/v0

dict =:: { 3: Pling, 5: Plang, 7: Plong }

defn convert(number):
words =: mapcat(\((number % _).!
&&& dict.get(_)) [3 5 7])
if words.?: str(words*) str(number)
words =:
mapcat _ [3 5 7]:
fn(n):
(number % n).eq(0) &&&:
get({3 'Pling' 5 'Plang' 7 'Plong'} n)

if words.?:
str(words*)
str(number)
Loading

0 comments on commit 25b12c7

Please sign in to comment.