diff --git a/exercises/practice/acronym/.meta/acronym.ys b/exercises/practice/acronym/.meta/acronym.ys index bdbf53b..4af9656 100644 --- a/exercises/practice/acronym/.meta/acronym.ys +++ b/exercises/practice/acronym/.meta/acronym.ys @@ -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() diff --git a/exercises/practice/all-your-base/.meta/all-your-base.ys b/exercises/practice/all-your-base/.meta/all-your-base.ys index 0d82d97..91b4ad5 100644 --- a/exercises/practice/all-your-base/.meta/all-your-base.ys +++ b/exercises/practice/all-your-base/.meta/all-your-base.ys @@ -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 diff --git a/exercises/practice/anagram/.meta/anagram.ys b/exercises/practice/anagram/.meta/anagram.ys index 7c0668a..5b3c4ec 100644 --- a/exercises/practice/anagram/.meta/anagram.ys +++ b/exercises/practice/anagram/.meta/anagram.ys @@ -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) diff --git a/exercises/practice/armstrong-numbers/.meta/armstrong-numbers.ys b/exercises/practice/armstrong-numbers/.meta/armstrong-numbers.ys index 031d33f..5efafc3 100644 --- a/exercises/practice/armstrong-numbers/.meta/armstrong-numbers.ys +++ b/exercises/practice/armstrong-numbers/.meta/armstrong-numbers.ys @@ -1,4 +1,7 @@ !yamlscript/v0 defn is-armstrong-number(number): - number ==: map(\(_ ** str(number).#) digits(number)).sum() + eq number: + sum: + map \(_ ** digits(number).#): + digits(number) diff --git a/exercises/practice/atbash-cipher/.meta/atbash-cipher.ys b/exercises/practice/atbash-cipher/.meta/atbash-cipher.ys index 64e9c0e..197c794 100644 --- a/exercises/practice/atbash-cipher/.meta/atbash-cipher.ys +++ b/exercises/practice/atbash-cipher/.meta/atbash-cipher.ys @@ -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) diff --git a/exercises/practice/bank-account/.meta/bank-account.ys b/exercises/practice/bank-account/.meta/bank-account.ys index 69a00cf..f8d18e8 100644 --- a/exercises/practice/bank-account/.meta/bank-account.ys +++ b/exercises/practice/bank-account/.meta/bank-account.ys @@ -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 diff --git a/exercises/practice/binary-search/.meta/binary-search.ys b/exercises/practice/binary-search/.meta/binary-search.ys index 32b5bed..cb3baf9 100644 --- a/exercises/practice/binary-search/.meta/binary-search.ys +++ b/exercises/practice/binary-search/.meta/binary-search.ys @@ -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.--) diff --git a/exercises/practice/bottle-song/.meta/bottle-song.ys b/exercises/practice/bottle-song/.meta/bottle-song.ys index b013399..01e12bb 100644 --- a/exercises/practice/bottle-song/.meta/bottle-song.ys +++ b/exercises/practice/bottle-song/.meta/bottle-song.ys @@ -3,15 +3,16 @@ defn recite(start-bottles take-down): lines: join "\n": - map verse: start-bottles .. (start-bottles - take-down).++ + map verse: + start-bottles .. (start-bottles - take-down).++ 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) @@ -19,4 +20,4 @@ defn- bottles(num): case num: 0 : 'no green bottles' 1 : 'one green bottle' - else : "$(dict.$num) green bottles" + else : "$(nums.$num) green bottles" diff --git a/exercises/practice/collatz-conjecture/.meta/collatz-conjecture.ys b/exercises/practice/collatz-conjecture/.meta/collatz-conjecture.ys index 5fa4d8a..69f8ec8 100644 --- a/exercises/practice/collatz-conjecture/.meta/collatz-conjecture.ys +++ b/exercises/practice/collatz-conjecture/.meta/collatz-conjecture.ys @@ -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.++) diff --git a/exercises/practice/diamond/.meta/diamond.ys b/exercises/practice/diamond/.meta/diamond.ys index 7a8ad8f..1981a69 100644 --- a/exercises/practice/diamond/.meta/diamond.ys +++ b/exercises/practice/diamond/.meta/diamond.ys @@ -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 diff --git a/exercises/practice/difference-of-squares/.meta/difference-of-squares.ys b/exercises/practice/difference-of-squares/.meta/difference-of-squares.ys index e4cccb6..f7c86b2 100644 --- a/exercises/practice/difference-of-squares/.meta/difference-of-squares.ys +++ b/exercises/practice/difference-of-squares/.meta/difference-of-squares.ys @@ -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) diff --git a/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys b/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys index bd76ac9..e2e7993 100644 --- a/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys +++ b/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys @@ -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 diff --git a/exercises/practice/etl/.meta/etl.ys b/exercises/practice/etl/.meta/etl.ys index f7e436c..80d515a 100644 --- a/exercises/practice/etl/.meta/etl.ys +++ b/exercises/practice/etl/.meta/etl.ys @@ -3,4 +3,4 @@ defn transform(legacy): into {}: for [score letters] legacy, letter letters: - vector: lc(letter) score.to-num() + +[lc(letter) score.N()] diff --git a/exercises/practice/grains/.meta/grains.ys b/exercises/practice/grains/.meta/grains.ys index 0acddb9..d5760ba 100644 --- a/exercises/practice/grains/.meta/grains.ys +++ b/exercises/practice/grains/.meta/grains.ys @@ -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 diff --git a/exercises/practice/hamming/.meta/hamming.ys b/exercises/practice/hamming/.meta/hamming.ys index 8048f51..6ba3c67 100644 --- a/exercises/practice/hamming/.meta/hamming.ys +++ b/exercises/practice/hamming/.meta/hamming.ys @@ -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) diff --git a/exercises/practice/isogram/.meta/isogram.ys b/exercises/practice/isogram/.meta/isogram.ys index e4ae8e8..0dd2c29 100644 --- a/exercises/practice/isogram/.meta/isogram.ys +++ b/exercises/practice/isogram/.meta/isogram.ys @@ -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" + .filter(/^[a-z]$/) \"Keep letters only" .distinct?(*) \"Check if distinct letters" + diff --git a/exercises/practice/largest-series-product/.meta/largest-series-product.ys b/exercises/practice/largest-series-product/.meta/largest-series-product.ys index 35e1491..8a6b3e1 100644 --- a/exercises/practice/largest-series-product/.meta/largest-series-product.ys +++ b/exercises/practice/largest-series-product/.meta/largest-series-product.ys @@ -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(*) diff --git a/exercises/practice/leap/.meta/leap.ys b/exercises/practice/leap/.meta/leap.ys index ba82f68..cfc5116 100644 --- a/exercises/practice/leap/.meta/leap.ys +++ b/exercises/practice/leap/.meta/leap.ys @@ -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) diff --git a/exercises/practice/list-ops/.meta/list-ops.ys b/exercises/practice/list-ops/.meta/list-ops.ys index 60ba76f..1440569 100644 --- a/exercises/practice/list-ops/.meta/list-ops.ys +++ b/exercises/practice/list-ops/.meta/list-ops.ys @@ -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 diff --git a/exercises/practice/luhn/.meta/luhn.ys b/exercises/practice/luhn/.meta/luhn.ys index 0a9d7f3..828f23a 100644 --- a/exercises/practice/luhn/.meta/luhn.ys +++ b/exercises/practice/luhn/.meta/luhn.ys @@ -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().vec() + .conj(0).partition(2).map(munge) + .flat().sum().mod(10).eq(0) defn munge([a b]): - b =: 2 * b - vector a: - if b > 9: (b - 9) b + b *=: 2 + vector: + a |: + if b > 9: (b - 9) b diff --git a/exercises/practice/matching-brackets/.meta/matching-brackets.ys b/exercises/practice/matching-brackets/.meta/matching-brackets.ys index e05e938..698ecbe 100644 --- a/exercises/practice/matching-brackets/.meta/matching-brackets.ys +++ b/exercises/practice/matching-brackets/.meta/matching-brackets.ys @@ -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.len() == 0 opening(char): recur: conj(stack char) chars closing(char): diff --git a/exercises/practice/minesweeper/.meta/minesweeper.ys b/exercises/practice/minesweeper/.meta/minesweeper.ys index c0bdea0..e692d87 100644 --- a/exercises/practice/minesweeper/.meta/minesweeper.ys +++ b/exercises/practice/minesweeper/.meta/minesweeper.ys @@ -4,19 +4,19 @@ defn annotate(minefield): grid =: minefield.map(split).vec() 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 == '*') diff --git a/exercises/practice/nth-prime/.meta/nth-prime.ys b/exercises/practice/nth-prime/.meta/nth-prime.ys index 4f3cef1..aad608f 100644 --- a/exercises/practice/nth-prime/.meta/nth-prime.ys +++ b/exercises/practice/nth-prime/.meta/nth-prime.ys @@ -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] diff --git a/exercises/practice/pangram/.meta/pangram.ys b/exercises/practice/pangram/.meta/pangram.ys index 8c3a8e2..e5ef73a 100644 --- a/exercises/practice/pangram/.meta/pangram.ys +++ b/exercises/practice/pangram/.meta/pangram.ys @@ -1,5 +1,7 @@ !yamlscript/v0 defn is-pangram(sentence): - sentence.lc().replace(/[^a-z]/ '') - .sort().distinct().len().eq(26) + eq 26: + len: lc(sentence) + .replace(/[^a-z]/) + .distinct() diff --git a/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys b/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys index 43365e0..b4de354 100644 --- a/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys +++ b/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys @@ -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(_).N().or(0) + + tri.last().get(_.++).N().or(0)) diff --git a/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys b/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys index f822599..6e8e043 100644 --- a/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys +++ b/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys @@ -12,5 +12,5 @@ defn classify(number): defn sum-of-divisors(number): sum: ? for n range(1 quot(number 2).++) - :when (number % n).! + :when (number % n).eq(0) : n diff --git a/exercises/practice/phone-number/.meta/phone-number.ys b/exercises/practice/phone-number/.meta/phone-number.ys index 45fefa6..3caa7ea 100644 --- a/exercises/practice/phone-number/.meta/phone-number.ys +++ b/exercises/practice/phone-number/.meta/phone-number.ys @@ -1,22 +1,22 @@ !yamlscript/v0 defn clean(phrase): - numstr =: phrase.replace(/[^\d]/ '') + numstr =: phrase.replace(/[^\d]/) + + phrase =~ /[a-z]/ &&: die('letters not permitted') + phrase.has?('@') &&: die('punctuations not permitted') - when phrase.lc() =~ /[a-z]/: die('letters not permitted') - when phrase =~ /@/: 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 diff --git a/exercises/practice/pig-latin/.meta/pig-latin.ys b/exercises/practice/pig-latin/.meta/pig-latin.ys index 4724f78..e999835 100644 --- a/exercises/practice/pig-latin/.meta/pig-latin.ys +++ b/exercises/practice/pig-latin/.meta/pig-latin.ys @@ -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) diff --git a/exercises/practice/protein-translation/.meta/protein-translation.ys b/exercises/practice/protein-translation/.meta/protein-translation.ys index 769dbde..e5064c3 100644 --- a/exercises/practice/protein-translation/.meta/protein-translation.ys +++ b/exercises/practice/protein-translation/.meta/protein-translation.ys @@ -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' diff --git a/exercises/practice/queen-attack/.meta/queen-attack.ys b/exercises/practice/queen-attack/.meta/queen-attack.ys index 1e92aeb..59c3562 100644 --- a/exercises/practice/queen-attack/.meta/queen-attack.ys +++ b/exercises/practice/queen-attack/.meta/queen-attack.ys @@ -1,6 +1,8 @@ !yamlscript/v0 -defn create({{r 'row' c 'column'} 'position'}): +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') @@ -9,8 +11,8 @@ defn create({{r 'row' c 'column'} 'position'}): else : 0 defn can-attack(white-queen black-queen): - -{w-r "row" w-c "column"} =: white-queen.position - -{b-r "row" b-c "column"} =: black-queen.position + w-r w-c =: white-queen.position.slice(q(row column)) + b-r b-c =: black-queen.position.slice(q(row column)) or: w-r ==: b-r diff --git a/exercises/practice/raindrops/.meta/raindrops.ys b/exercises/practice/raindrops/.meta/raindrops.ys index 7749c57..e1a39e3 100644 --- a/exercises/practice/raindrops/.meta/raindrops.ys +++ b/exercises/practice/raindrops/.meta/raindrops.ys @@ -1,7 +1,15 @@ !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) == 0 &&&: + get _ n:: + 3: Pling + 5: Plang + 7: Plong + + if words.?: + str(words*) + str(number) diff --git a/exercises/practice/resistor-color/.meta/resistor-color.ys b/exercises/practice/resistor-color/.meta/resistor-color.ys index 937b992..88c1ac9 100644 --- a/exercises/practice/resistor-color/.meta/resistor-color.ys +++ b/exercises/practice/resistor-color/.meta/resistor-color.ys @@ -1,19 +1,8 @@ !yamlscript/v0 -codes =:: - black: 0 - brown: 1 - red: 2 - orange: 3 - yellow: 4 - green: 5 - blue: 6 - violet: 7 - grey: 8 - white: 9 - defn color-code(color): - codes.$color + zipmap(colors() range()).$color defn colors(): - codes.keys() + qw(black brown red orange yellow + green blue violet grey white) diff --git a/exercises/practice/reverse-string/.meta/reverse-string.ys b/exercises/practice/reverse-string/.meta/reverse-string.ys index 3d57eb9..e948ea0 100644 --- a/exercises/practice/reverse-string/.meta/reverse-string.ys +++ b/exercises/practice/reverse-string/.meta/reverse-string.ys @@ -1,4 +1,7 @@ !yamlscript/v0 defn reverse(s): - str/reverse: s + loop s s, r '': + if s.?: + recur: rest(s) "$first(s)$r" + else: r diff --git a/exercises/practice/rna-transcription/.meta/rna-transcription.ys b/exercises/practice/rna-transcription/.meta/rna-transcription.ys index c3ec778..e006f7b 100644 --- a/exercises/practice/rna-transcription/.meta/rna-transcription.ys +++ b/exercises/practice/rna-transcription/.meta/rna-transcription.ys @@ -1,5 +1,7 @@ !yamlscript/v0 -dna-to-rna =: list('GCAT' 'CGUA').map(split).zipmap(*) +dna-to-rna =: + map(split ['GCAT' 'CGUA']).zipmap(*) -defn to-rna(dna): dna.split().map(dna-to-rna).join() +defn to-rna(dna): + split(dna).map(dna-to-rna).join() diff --git a/exercises/practice/robot-simulator/.meta/robot-simulator.ys b/exercises/practice/robot-simulator/.meta/robot-simulator.ys index 58be5cc..9129556 100644 --- a/exercises/practice/robot-simulator/.meta/robot-simulator.ys +++ b/exercises/practice/robot-simulator/.meta/robot-simulator.ys @@ -19,8 +19,7 @@ defn move(position direction instructions): else: robot defn advance(robot): - x =: robot.position.x - y =: robot.position.y + x y =: robot.position.slice(q(x y)) d =: robot.direction create _ d: condp eq d: @@ -39,4 +38,4 @@ defn turn-left(robot): defn turn(direction directions): directions.mul+(2): - .drop-while(\(_ != direction)).second() + .drop-while(ne(direction)).1 diff --git a/exercises/practice/roman-numerals/.meta/roman-numerals.ys b/exercises/practice/roman-numerals/.meta/roman-numerals.ys index 80fee94..2255920 100644 --- a/exercises/practice/roman-numerals/.meta/roman-numerals.ys +++ b/exercises/practice/roman-numerals/.meta/roman-numerals.ys @@ -1,6 +1,6 @@ !yamlscript/v0 -numeral-mapping =:: +rules =:: 1000: M 900: CM 500: D @@ -16,7 +16,9 @@ numeral-mapping =:: 1: I defn roman(number): - when pos?(number): - -[part letter] =: - numeral-mapping.filter(\(_.0 <= number)).0 + when number > 0: + part letter =: + first: + filter _ rules: + le(number).comp(first) str letter: roman(number - part) diff --git a/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys b/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys index 36a87d1..8590461 100644 --- a/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys +++ b/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys @@ -1,8 +1,12 @@ !yamlscript/v0 -upper =: \\A .. \\Z -lower =: \\a .. \\z +UC =: \\A .. \\Z +LC =: \\a .. \\z -defn rotate(text shift-key): text - .str/escape(lower.zipmap(cycle(lower).drop(shift-key))) - .str/escape(upper.zipmap(cycle(upper).drop(shift-key))) +defn rotate(text shift-key): + char-map =: + \(zipmap(_ drop(shift-key cycle(_)))) + + text: + .escape(char-map(UC)) + .escape(char-map(LC)) diff --git a/exercises/practice/run-length-encoding/.meta/run-length-encoding.ys b/exercises/practice/run-length-encoding/.meta/run-length-encoding.ys index c9bb86c..1598f66 100644 --- a/exercises/practice/run-length-encoding/.meta/run-length-encoding.ys +++ b/exercises/practice/run-length-encoding/.meta/run-length-encoding.ys @@ -3,18 +3,15 @@ defn encode(string): apply str: for part partition-by(a string): - str: - ((part.# > 1) &&& part.#) - part.0 + str: part.--.pos?().and?(part.#) part.0 defn decode(string): ? loop - [p1 p2 *parts] - re-seq(/\d+|[^\d]/ string) + [p1 p2 *parts] re-seq(/\d+|[^\d]/ string) val '' : if p1: if p1 =~ /\d/: - recur parts: "$val$(to-int(p1) * p2)" + recur parts: "$val$(I(p1) * p2)" recur cons(p2 parts): "$val$p1" else: val diff --git a/exercises/practice/scrabble-score/.meta/scrabble-score.ys b/exercises/practice/scrabble-score/.meta/scrabble-score.ys index 51bb6d9..160bf3a 100644 --- a/exercises/practice/scrabble-score/.meta/scrabble-score.ys +++ b/exercises/practice/scrabble-score/.meta/scrabble-score.ys @@ -6,4 +6,5 @@ defn score(word): S(4 'FHVWY') + S(5 'K') + S(8 'JX') + S(10 'QZ') ).sum() -defn S(score chars): zipmap(split(chars) repeat(score)) +defn S(score chars): + zipmap: chars.split() repeat(score) diff --git a/exercises/practice/secret-handshake/.meta/secret-handshake.ys b/exercises/practice/secret-handshake/.meta/secret-handshake.ys index e7aa52b..e35b34c 100644 --- a/exercises/practice/secret-handshake/.meta/secret-handshake.ys +++ b/exercises/practice/secret-handshake/.meta/secret-handshake.ys @@ -4,10 +4,11 @@ defn commands(number): cmds =:: [wink, double blink, close your eyes, jump] ->> range(cmds.#): - map fn([n] (number.bit-test(n) && cmds.$n)): - filter identity: + map: + fn(n): number.bit-test(n) && cmds.$n + filter: a call: fn(commands): if number.bit-test(cmds.#): - reverse(commands) + commands.reverse() commands diff --git a/exercises/practice/sieve/.meta/sieve.ys b/exercises/practice/sieve/.meta/sieve.ys index e9666f8..09ab5e4 100644 --- a/exercises/practice/sieve/.meta/sieve.ys +++ b/exercises/practice/sieve/.meta/sieve.ys @@ -1,11 +1,12 @@ !yamlscript/v0 defn primes(limit): - loop idx 2, primes ([nil nil] + range(2 limit.++)): + loop idx 2, primes ([nil nil] + (2 ... limit.++)): if idx < sqrt(limit): recur idx.++: if primes.$idx: map-indexed _ primes: - \(if ((_ >= sqr(idx)) && (_ % idx).!) nil %2) + fn(a b): + when (a < sqr(idx)) || (a % idx).?: b else: primes - primes: .remove(nil?) + filter a: primes diff --git a/exercises/practice/space-age/.meta/space-age.ys b/exercises/practice/space-age/.meta/space-age.ys index 13a3cb1..ead4310 100644 --- a/exercises/practice/space-age/.meta/space-age.ys +++ b/exercises/practice/space-age/.meta/space-age.ys @@ -17,4 +17,4 @@ defn age(planet seconds): die('not a planet') seconds / seconds-in-year / factor: - .mul(100).math/round().div(100) + .format('%.2f' _).F() diff --git a/exercises/practice/square-root/.meta/square-root.ys b/exercises/practice/square-root/.meta/square-root.ys index 9f1cb69..830a0a3 100644 --- a/exercises/practice/square-root/.meta/square-root.ys +++ b/exercises/practice/square-root/.meta/square-root.ys @@ -2,6 +2,6 @@ defn square-root(radicand): loop n 0: - if (n * n) != radicand: - recur: n + 1 + if sqr(n) != radicand: + recur: n.++ else: n diff --git a/exercises/practice/sum-of-multiples/.meta/sum-of-multiples.ys b/exercises/practice/sum-of-multiples/.meta/sum-of-multiples.ys index 42c18e4..30bfa5b 100644 --- a/exercises/practice/sum-of-multiples/.meta/sum-of-multiples.ys +++ b/exercises/practice/sum-of-multiples/.meta/sum-of-multiples.ys @@ -1,4 +1,7 @@ !yamlscript/v0 defn sum(factors limit): - factors.mapcat(\(when(_.? range(_ limit _))) _).distinct().std/sum() + factors + .mapcat(\(when(_.ne(0) range(_ limit _))) _) + .distinct() + .std/sum() diff --git a/exercises/practice/triangle/.meta/triangle.ys b/exercises/practice/triangle/.meta/triangle.ys index ef11241..0f66a2b 100644 --- a/exercises/practice/triangle/.meta/triangle.ys +++ b/exercises/practice/triangle/.meta/triangle.ys @@ -4,12 +4,12 @@ defn equilateral(sides): eq(sides*) &&: sides.0 > 0 defn isosceles(sides): - -[a b c] =: sort(sides) + a b c =: sort(sides) (a + b) > c &&: - sides.frequencies().# <= 2 + len(sides.frequencies()) <= 2 defn scalene(sides): boolean: when-not isosceles(sides): - -[a b c] =: sort(sides) + a b c =: sort(sides) a + b >: c diff --git a/exercises/practice/two-bucket/.meta/two-bucket.ys b/exercises/practice/two-bucket/.meta/two-bucket.ys index e447a2e..5884051 100644 --- a/exercises/practice/two-bucket/.meta/two-bucket.ys +++ b/exercises/practice/two-bucket/.meta/two-bucket.ys @@ -6,25 +6,25 @@ defn measure(bucket1 bucket2 goal start): goal == [0 bucket2 bucket1].$start: ret: 2 (start % 2).++ [0 bucket1 bucket2].$start else: - -[b1 b2] =: + b1 b2 =: if start == 1: - -[bucket1 bucket2] [bucket2 bucket1] + -[bucket1 bucket2] + [bucket2 bucket1] n =: keep-indexed \(when (== %2 goal) %1): if b1 < b2: - map \(_ % b2): range(b1 lcm(b1 b2).++ b1) - reverse: - map \(_ % b1): range(b2 lcm(b1 b2) b2) + range(b1 lcm(b1 b2).++ b1).map(\(_ % b2)) + range(b2 lcm(b1 b2) b2).map(\(_ % b1)).reverse() n |||: die('impossible') n =: n.0.++ e =: int((min(b1 b2) * n) / max(b1 b2)) - - to-num(goal < b1 < b2) + (goal < b1 < b2).N() moves =: 2 * (n + e) if goal < b1: ret: moves, start, b2 ret: moves, 2, 0 -defn ret(moves goal other): - -{'moves' moves, 'goalBucket' goal, 'otherBucket' other} defn gcd(x y): if((x % y).? gcd(y (x % y)) y) defn lcm(x y): (x * y) / gcd(x y) +defn ret(moves goal other): + -{'moves' moves, 'goalBucket' goal, 'otherBucket' other} diff --git a/exercises/practice/word-count/.meta/word-count.ys b/exercises/practice/word-count/.meta/word-count.ys index 1617acd..35cce70 100644 --- a/exercises/practice/word-count/.meta/word-count.ys +++ b/exercises/practice/word-count/.meta/word-count.ys @@ -1,6 +1,8 @@ !yamlscript/v0 defn count-words(sentence): - lc(sentence).split(/[^a-z0-9']+/) - .map(\(replace _, /(^'|'$)/, '')) - .remove(empty?).frequencies() + lc(sentence) + .split(/[^a-z0-9']+/) + .map(\(_.replace(/(^'|'$)/))) + .remove(empty?) + .frequencies() diff --git a/exercises/practice/yacht/.meta/yacht.ys b/exercises/practice/yacht/.meta/yacht.ys index 9c26b93..d57b49d 100644 --- a/exercises/practice/yacht/.meta/yacht.ys +++ b/exercises/practice/yacht/.meta/yacht.ys @@ -7,7 +7,7 @@ defn score(dice category): defn- do-yacht(dice): eq(dice*) && 50 defn- same-number(n): - fn(dice): dice.filter(\(_ == n)).sum() + fn(dice): dice.filter(eq(n)).sum() do-ones =: same-number(1) do-twos =: same-number(2) @@ -19,10 +19,11 @@ do-sixes =: same-number(6) defn- do-full-house(dice): freqs =: frequencies(dice) when freqs.#.eq(2) && freqs.vals().has?(2): - sum(dice) + sum: dice defn- do-four-of-a-kind(dice): - freqs =: frequencies(dice).set/map-invert() + freqs =: + frequencies(dice).V().reverse().M() mul 4: freqs.get(4) || freqs.get(5) || 0 defn- do-little-straight(dice):