diff --git a/exercises/practice/acronym/.meta/acronym.ys b/exercises/practice/acronym/.meta/acronym.ys index 4cfcb63..cb0bb94 100644 --- a/exercises/practice/acronym/.meta/acronym.ys +++ b/exercises/practice/acronym/.meta/acronym.ys @@ -1,6 +1,5 @@ !yamlscript/v0 defn abbreviate(phrase): - uc(phrase): - .re-seq(/[A-Z']+/) + uc(phrase).re-seq(/[A-Z']+/) .map(first):join diff --git a/exercises/practice/allergies/.meta/allergies.ys b/exercises/practice/allergies/.meta/allergies.ys index cce79f1..6852a78 100644 --- a/exercises/practice/allergies/.meta/allergies.ys +++ b/exercises/practice/allergies/.meta/allergies.ys @@ -1,20 +1,9 @@ !yamlscript/v0 -allergen-map =: - zipmap _ range():: - - eggs - - peanuts - - shellfish - - strawberries - - tomatoes - - chocolate - - pollen - - cats - defn allergic-to(item score): - bit-test score: allergen-map.$item + list-allergens(score).has?(item) defn list-allergens(score): - keep _ allergen-map: - fn([allergen num]): - bit-test(score num) &&& allergen + (0 .. 7).filter(P(bit-test score)) + .map(qw(eggs peanuts shellfish strawberries + tomatoes chocolate pollen cats)) diff --git a/exercises/practice/anagram/.meta/anagram.ys b/exercises/practice/anagram/.meta/anagram.ys index 5b3c4ec..ef17ba8 100644 --- a/exercises/practice/anagram/.meta/anagram.ys +++ b/exercises/practice/anagram/.meta/anagram.ys @@ -2,7 +2,7 @@ defn find-anagrams(subject candidates): filter _ candidates: - partial _ subject: + P _ subject: fn(*words): word1 word2 =: words.map(lc) word1 != word2 &&: diff --git a/exercises/practice/bank-account/.meta/bank-account.ys b/exercises/practice/bank-account/.meta/bank-account.ys index d7b446a..21e363a 100644 --- a/exercises/practice/bank-account/.meta/bank-account.ys +++ b/exercises/practice/bank-account/.meta/bank-account.ys @@ -1,50 +1,28 @@ !yamlscript/v0 -account =: atom(nil) +closed =: 'closed' +closed? =: P(== closed) defn reset-test-state(): - reset! account: nil defn bank-account(operations): - last: - for operation operations: - operation - .operation - .str('do-' _) - .call(operation) - -defn- do-open(op): - when deref(account): - die: 'account already open' - reset! account: 0 - -defn- do-close(op): - when-not deref(account): - die: 'account not open' - reset! account: nil - -defn- do-balance(op): - balance =: deref(account) - if-not balance: - die: 'account not open' - else: balance - -defn- do-deposit(op): - when-not deref(account): - die: 'account not open' - swap! account +: op:get-amount - -defn- do-withdraw(op): - balance =: deref(account) - when-not balance: - die: 'account not open' - 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: - die: 'amount must be greater than 0' - else: amount + reduce _ closed operations: + fn(bal op): + case op.operation: + -'open': + if closed?(bal): 0, die('account already open') + -'close': + if closed?(bal): die('account not open'), closed + -'balance': + if closed?(bal): die('account not open'), bal + -'deposit': + cond: + closed?(bal): die('account not open') + op.amount <= 0: die('amount must be greater than 0') + else: bal + op.amount + -'withdraw': + cond: + closed?(bal): die('account not open') + op.amount <= 0: die('amount must be greater than 0') + op.amount > bal: die('amount must be less than balance') + else: bal - op.amount diff --git a/exercises/practice/binary-search/.meta/binary-search.ys b/exercises/practice/binary-search/.meta/binary-search.ys index cb3baf9..e2efa5b 100644 --- a/exercises/practice/binary-search/.meta/binary-search.ys +++ b/exercises/practice/binary-search/.meta/binary-search.ys @@ -1,13 +1,9 @@ !yamlscript/v0 defn find(array value): - loop low 0, high array.--: - when low > high: - die: 'value not in array' - mid =: (high + low).quot(2) - item =: array.$mid - - cond: - value == item : mid - value < item : recur(low mid.--) - else : recur(mid.++ high) + when array.#.!: die('value not in array') + mid =: array.#.--.quot(2) + case compare(array.$mid value): + 0: mid + 1: find(take(mid array) value) + -1: find(drop(mid.++ array) value) + mid.++ diff --git a/exercises/practice/circular-buffer/.meta/circular-buffer.ys b/exercises/practice/circular-buffer/.meta/circular-buffer.ys index a69b82a..c464b44 100644 --- a/exercises/practice/circular-buffer/.meta/circular-buffer.ys +++ b/exercises/practice/circular-buffer/.meta/circular-buffer.ys @@ -1,7 +1,7 @@ !yamlscript/v0 defn run(capacity operations): - buffer =: M(:B V(capacity * [nil]), :P 0) + buffer =: -{:B V(capacity * [nil]), :P 0} reduce _ buffer operations: fn(buffer op): op item pass want =: @@ -10,14 +10,14 @@ defn run(capacity operations): args =: condp eq op: -'clear' : -[buffer] - -'read' : L(buffer pass want) - -'write' : L(buffer pass item) - -'overwrite' : L(buffer pass item) + -'read' : -[buffer pass want] + -'write' : -[buffer pass item] + -'overwrite' : -[buffer pass item] value("op-$op"): args* =>: -{} defn op-clear(buffer): - M: :B V(buffer.B.# * [nil]) :P 0 + hash-map: :B V(buffer.B.# * [nil]) :P 0 defn op-read(buffer pass want): cell =: buffer.B.nth(buffer.P) @@ -26,8 +26,9 @@ defn op-read(buffer pass want): die: S("Can't read:\ " buffer) when cell != want: die: "Bad read:\ $cell != $want ($buffer)" - M: :B update-in(buffer.B [buffer.P] constantly(nil)) - :P (buffer.P.++ % buffer.B.#) + hash-map: + :B update-in(buffer.B [buffer.P] constantly(nil)) + :P (buffer.P.++ % buffer.B.#) defn op-write(buffer pass item): B P =: buffer.slice(q(B P)) diff --git a/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys b/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys index e2e7993..9ef5cbe 100644 --- a/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys +++ b/exercises/practice/eliuds-eggs/.meta/eliuds-eggs.ys @@ -1,7 +1,4 @@ !yamlscript/v0 defn egg-count(number): - loop num number, eggs 0: - if num > 0: - recur: quot(num 2), eggs.add(num % 2) - else: eggs + len: P(bit-test number).filter(0 .. 31) diff --git a/exercises/practice/etl/.meta/etl.ys b/exercises/practice/etl/.meta/etl.ys index 47d15cd..e725c30 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: - V: lc(letter) score:N + -[lc(letter) score:N] diff --git a/exercises/practice/grains/.meta/grains.ys b/exercises/practice/grains/.meta/grains.ys index d5760ba..fabc7ae 100644 --- a/exercises/practice/grains/.meta/grains.ys +++ b/exercises/practice/grains/.meta/grains.ys @@ -3,5 +3,4 @@ defn square(square): (1 <= square <= 64) |||: die: 'square must be between 1 and 64' - - pow 2: square - 1 + 2 **: square.-- diff --git a/exercises/practice/hamming/.meta/hamming.ys b/exercises/practice/hamming/.meta/hamming.ys index 6ba3c67..1878c97 100644 --- a/exercises/practice/hamming/.meta/hamming.ys +++ b/exercises/practice/hamming/.meta/hamming.ys @@ -3,5 +3,4 @@ defn distance(strand1 strand2): strand1.# == strand2.# ||: die: 'strands must be of equal length' - - len: map(ne strand1 strand2).filter(a) + sum: map(ne strand1 strand2).map(N) diff --git a/exercises/practice/isogram/.meta/isogram.ys b/exercises/practice/isogram/.meta/isogram.ys index a352636..0fd4dcb 100644 --- a/exercises/practice/isogram/.meta/isogram.ys +++ b/exercises/practice/isogram/.meta/isogram.ys @@ -1,7 +1,4 @@ !yamlscript/v0 -defn isogram(string): - empty?(string) ||: - lc(string):split \"Want to be case insensitive" - .filter(/^[a-z]$/) \"Keep letters only" - .distinct?(*) \"Check if distinct letters" +defn isogram(phrase): + phrase !~ /(?i)(\w).*\1/ diff --git a/exercises/practice/list-ops/.meta/list-ops.ys b/exercises/practice/list-ops/.meta/list-ops.ys index 0c07974..c62c14e 100644 --- a/exercises/practice/list-ops/.meta/list-ops.ys +++ b/exercises/practice/list-ops/.meta/list-ops.ys @@ -19,10 +19,4 @@ defn foldl(list initial function): list.reduce(function initial) defn foldr(list initial function): - if len(list) > 0: - function _ list:first: - foldr: rest(list) initial function - else: initial - -defn reverse(list): - core/reverse: list + list:reverse.reduce(function initial) diff --git a/exercises/practice/luhn/.meta/luhn.ys b/exercises/practice/luhn/.meta/luhn.ys index c3b9698..2698f93 100644 --- a/exercises/practice/luhn/.meta/luhn.ys +++ b/exercises/practice/luhn/.meta/luhn.ys @@ -1,18 +1,6 @@ !yamlscript/v0 defn valid(value): - digits =: value.replace(' ') - - and: - digits.# >=: 2 - 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 - vector a: - if b > 9: (b - 9) b + digits =: value.replace(' ').map(\(_ - \\0)):reverse + digits.#.gt(1) &&: digits.every?(\(0 <= _ <= 9)) && + map(int.comp(mul) digits [1 2.2]:cycle):sum.mod(10).! diff --git a/exercises/practice/matching-brackets/.meta/matching-brackets.ys b/exercises/practice/matching-brackets/.meta/matching-brackets.ys index 64cb419..35f0b8a 100644 --- a/exercises/practice/matching-brackets/.meta/matching-brackets.ys +++ b/exercises/practice/matching-brackets/.meta/matching-brackets.ys @@ -1,17 +1,8 @@ !yamlscript/v0 -pairs =: reverse('()[]{}'):M -opening =: vals(pairs):set -closing =: keys(pairs):set - defn is-paired(value): - loop stack [], [char *chars] value: - cond: - char:nil?: stack:count:zero? - opening(char): - recur: conj(stack char) chars - closing(char): - if peek(stack) == pairs(char): - recur: pop(stack) chars - else: false - else: recur(stack chars) + x =: value.replace(/\[\]|\(\)|\{\}|[^(){}\[\]]/) + cond: + x == '': true + x == value: false + else: is-paired(x) diff --git a/exercises/practice/nth-prime/.meta/nth-prime.ys b/exercises/practice/nth-prime/.meta/nth-prime.ys index aad608f..f578b80 100644 --- a/exercises/practice/nth-prime/.meta/nth-prime.ys +++ b/exercises/practice/nth-prime/.meta/nth-prime.ys @@ -1,15 +1,9 @@ !yamlscript/v0 defn prime(number): - lazy-primes().nth(number.--) - -defn- lazy-primes(d=2 table={}): - if-let factors table.$d: - recur d.++: - reduce _ table.dissoc(d) factors: - \(update-in(%1 [(%2 + d)] conj %2)) - - lazy-seq: - cons d: - lazy-primes d.++: - assoc table: sqr(d) [d] + loop n 2, u 0: + cond: + u == number: n.-- + range(2 sqrt(n):I.++).every?(\((n % _).?)): + recur(n.++ u.++) + else: recur(n.++ u) diff --git a/exercises/practice/nucleotide-count/.meta/nucleotide-count.ys b/exercises/practice/nucleotide-count/.meta/nucleotide-count.ys index 187bad2..7c91796 100644 --- a/exercises/practice/nucleotide-count/.meta/nucleotide-count.ys +++ b/exercises/practice/nucleotide-count/.meta/nucleotide-count.ys @@ -1,10 +1,8 @@ !yamlscript/v0 -nucleotides =: zipmap(qw(A C G T) repeat(0)) - defn nucleotide-counts(strand): - when strand !~ /^[ACGT]*$/: + when strand =~ /[^ACGT]/: die: 'Invalid nucleotide in strand' - reduce-kv _ nucleotides frequencies(strand): - fn(map key val): map.assoc(str(key) val) + into zipmap(qw(A C G T) repeat(0)): + frequencies(strand.map(str)) diff --git a/exercises/practice/pangram/.meta/pangram.ys b/exercises/practice/pangram/.meta/pangram.ys index 081dafe..fe04ae4 100644 --- a/exercises/practice/pangram/.meta/pangram.ys +++ b/exercises/practice/pangram/.meta/pangram.ys @@ -1,6 +1,4 @@ !yamlscript/v0 defn is-pangram(sentence): - 26 ==: lc(sentence) - .replace(/[^a-z]/) - .distinct():len + B: sentence =~ /(?i)(?>.*?(\pL)(?!.*\1)){26}/ diff --git a/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys b/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys index 830e110..5f2b15d 100644 --- a/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys +++ b/exercises/practice/pascals-triangle/.meta/pascals-triangle.ys @@ -1,11 +1,5 @@ !yamlscript/v0 defn rows(count): - reduce _ [] range(1 count.++): - fn(tri row): - conj tri: - or _ [1]: - when row > 1: - mapv _ (-1 .. row.sub(2)): - \(last(tri).get(_):N.or(0) + - last(tri).get(_.++):N.or(0)) + \(mapv + _.cons(0) _.conj(0)) + .iterate([1]).take(count) diff --git a/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys b/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys index 4ee3f5a..f44dac8 100644 --- a/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys +++ b/exercises/practice/perfect-numbers/.meta/perfect-numbers.ys @@ -1,14 +1,13 @@ !yamlscript/v0 defn classify(number): - when number <= 0: + when number < 1: die: 'Classification is only possible for positive integers.' - sum-of-divisors =: - sum: - for n range(1 quot(number 2).++) :when (number % n).!: n + nums =: + for n range(1 quot(number 2).++) :when (number % n).!: n - case compare(sum-of-divisors, number): + case compare(nums:sum number): 0 :: perfect 1 :: abundant -1 :: deficient diff --git a/exercises/practice/pig-latin/.meta/pig-latin.ys b/exercises/practice/pig-latin/.meta/pig-latin.ys index e999835..2e48f05 100644 --- a/exercises/practice/pig-latin/.meta/pig-latin.ys +++ b/exercises/practice/pig-latin/.meta/pig-latin.ys @@ -1,17 +1,9 @@ !yamlscript/v0 -rules =:: - ^(?:[aeiou]|yt|xr).*:: - \(_ + 'ay') - ^(s?qu|thr|sch|[crst]h|[^aeiou])(.*):: - \("$(_.2)$(_.1)ay") - 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) + replace phrase: + /(?x) + ([^aeioquxy\s]* + (?:qu|q|x|y)?) + ([aeiouxy]\w*)/ + "$2$1ay" diff --git a/exercises/practice/prime-factors/.meta/prime-factors.ys b/exercises/practice/prime-factors/.meta/prime-factors.ys index 91fb5cc..e64107d 100644 --- a/exercises/practice/prime-factors/.meta/prime-factors.ys +++ b/exercises/practice/prime-factors/.meta/prime-factors.ys @@ -4,5 +4,5 @@ defn factors(value): or _ []: when value >= 2: divisor =: range(2 (value / 2).++) - .filter(\((value % _) == 0)):first || value + .filter(\((value % _).!)).0 || value into [divisor]: factors(quot(value divisor)) diff --git a/exercises/practice/protein-translation/.meta/protein-translation.ys b/exercises/practice/protein-translation/.meta/protein-translation.ys index 1fb58cc..dc75444 100644 --- a/exercises/practice/protein-translation/.meta/protein-translation.ys +++ b/exercises/practice/protein-translation/.meta/protein-translation.ys @@ -2,15 +2,14 @@ defn proteins(strand): proteins =: - strand.str('XX').partition(3) - .map(join).map(translate-codon) + strand.re-seq(/.{1,3}/).map(translate) .take-while(ne('STOP')) if proteins.has?('INVALID'): die: 'Invalid codon' else: proteins -defn translate-codon(codon): +defn translate(codon): case codon: ('AUG' ) :: Methionine ('UUU' 'UUC' ) :: Phenylalanine diff --git a/exercises/practice/queen-attack/.meta/queen-attack.ys b/exercises/practice/queen-attack/.meta/queen-attack.ys index 59c3562..42a4d8e 100644 --- a/exercises/practice/queen-attack/.meta/queen-attack.ys +++ b/exercises/practice/queen-attack/.meta/queen-attack.ys @@ -1,20 +1,17 @@ !yamlscript/v0 defn create(queen): - r c =: queen.position.map(qw(row column)) - + row col =: queen.position.map(qw(row column)) cond: - r < 0 : die('row not positive') - r > 7 : die('row not on board') - c < 0 : die('column not positive') - c > 7 : die('column not on board') - else : 0 + row < 0 : die('row not positive') + row > 7 : die('row not on board') + col < 0 : die('column not positive') + col > 7 : die('column not on board') + else : 0 defn can-attack(white-queen black-queen): - 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 - w-c ==: b-c - abs(w-r - b-r) ==: abs(w-c - b-c) + x =: white-queen.position.row - + black-queen.position.row + y =: white-queen.position.column - + black-queen.position.column + (x.! || y.!) ||: x:abs == y:abs diff --git a/exercises/practice/raindrops/.meta/raindrops.ys b/exercises/practice/raindrops/.meta/raindrops.ys index d4c64a7..87c04c6 100644 --- a/exercises/practice/raindrops/.meta/raindrops.ys +++ b/exercises/practice/raindrops/.meta/raindrops.ys @@ -1,9 +1,5 @@ !yamlscript/v0 defn convert(number): - ps =: M(3 'Pling' 5 'Plang' 7 'Plong') - words =: - mapcat _ [3 5 7]: - fn(n): number.rem(n).! &&& ps.$n - - if words.?: str(words*) str(number) + f =: fn([a b] (number % a).!.when(b)) + or?: map(f [3 5 7] qw(Pling Plang Plong)).str(*) number:S diff --git a/exercises/practice/rna-transcription/.meta/rna-transcription.ys b/exercises/practice/rna-transcription/.meta/rna-transcription.ys index 7fa7a7e..0945aed 100644 --- a/exercises/practice/rna-transcription/.meta/rna-transcription.ys +++ b/exercises/practice/rna-transcription/.meta/rna-transcription.ys @@ -1,7 +1,4 @@ !yamlscript/v0 -dna-to-rna =: - map(split ['GCAT' 'CGUA']).zipmap(*) - defn to-rna(dna): - split(dna).map(dna-to-rna):join + map(zipmap('GCTA' 'CGAU') dna).str(*) diff --git a/exercises/practice/roman-numerals/.meta/roman-numerals.ys b/exercises/practice/roman-numerals/.meta/roman-numerals.ys index 2255920..c1fedec 100644 --- a/exercises/practice/roman-numerals/.meta/roman-numerals.ys +++ b/exercises/practice/roman-numerals/.meta/roman-numerals.ys @@ -1,24 +1,8 @@ !yamlscript/v0 -rules =:: - 1000: M - 900: CM - 500: D - 400: CD - 100: C - 90: XC - 50: L - 40: XL - 10: X - 9: IX - 5: V - 4: IV - 1: I - defn roman(number): - when number > 0: - part letter =: - first: - filter _ rules: - le(number).comp(first) - str letter: roman(number - part) + nums =: qw(I II III IV V VI VII VIII IX).cons('') + reduce _ '' number:S: + fn(roman digit): + roman.map('IVXLC'.zipmap('XLCDM')) + .str(*).str(nums.nth(digit - \\0)) diff --git a/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys b/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys index 8590461..2151e41 100644 --- a/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys +++ b/exercises/practice/rotational-cipher/.meta/rotational-cipher.ys @@ -1,12 +1,6 @@ !yamlscript/v0 -UC =: \\A .. \\Z -LC =: \\a .. \\z - defn rotate(text shift-key): - char-map =: - \(zipmap(_ drop(shift-key cycle(_)))) - - text: - .escape(char-map(UC)) - .escape(char-map(LC)) + cmap =: \(zipmap(_ drop(shift-key cycle(_)))) + text.escape(cmap(\\A .. \\Z)): + .escape(cmap(\\a .. \\z)) diff --git a/exercises/practice/scrabble-score/.meta/scrabble-score.ys b/exercises/practice/scrabble-score/.meta/scrabble-score.ys index 7453de7..862d1ba 100644 --- a/exercises/practice/scrabble-score/.meta/scrabble-score.ys +++ b/exercises/practice/scrabble-score/.meta/scrabble-score.ys @@ -1,10 +1,6 @@ !yamlscript/v0 -defn score(word): - uc(word):split.map( - S(1 'AEIOULNRST') + S(2 'DG') + S(3 'BCMP') + - S(4 'FHVWY') + S(5 'K') + S(8 'JX') + S(10 'QZ') - ):sum - -defn S(score chars): - zipmap: chars:split repeat(score) +scores =: "1332142418513113:11114484:" +defn score(word): !:sum + map _ word:lc: + \((_ - \\a).nth(scores):N - \\0) diff --git a/exercises/practice/secret-handshake/.meta/secret-handshake.ys b/exercises/practice/secret-handshake/.meta/secret-handshake.ys index 0b94ee5..6df88c9 100644 --- a/exercises/practice/secret-handshake/.meta/secret-handshake.ys +++ b/exercises/practice/secret-handshake/.meta/secret-handshake.ys @@ -1,13 +1,6 @@ !yamlscript/v0 -cmds =:: [wink, double blink, close your eyes, jump] - defn commands(number): - commands =: - range(cmds.#) - .map(\(number.bit-test(_) && cmds.get(_))) - .filter(a) - - if number.bit-test(cmds.#): - commands:reverse - commands + a =: (0 .. 3).filter(bit-test.P(number)) + .map(['wink' 'double blink' 'close your eyes' 'jump']) + if number.bit-test(4): a:reverse a diff --git a/exercises/practice/sieve/.meta/sieve.ys b/exercises/practice/sieve/.meta/sieve.ys index 09ab5e4..5f0d25d 100644 --- a/exercises/practice/sieve/.meta/sieve.ys +++ b/exercises/practice/sieve/.meta/sieve.ys @@ -1,12 +1,6 @@ !yamlscript/v0 defn primes(limit): - loop idx 2, primes ([nil nil] + (2 ... limit.++)): - if idx < sqrt(limit): - recur idx.++: - if primes.$idx: - map-indexed _ primes: - fn(a b): - when (a < sqr(idx)) || (a % idx).?: b - else: primes - filter a: primes + nums =: range(2 limit.++) + remove _ nums: !:set + mapcat _ nums: \(range (_ * 2) limit.++ _) diff --git a/exercises/practice/space-age/.meta/space-age.ys b/exercises/practice/space-age/.meta/space-age.ys index 645bacb..7871ba8 100644 --- a/exercises/practice/space-age/.meta/space-age.ys +++ b/exercises/practice/space-age/.meta/space-age.ys @@ -1,20 +1,16 @@ !yamlscript/v0 -factors =:: - Mercury : 0.2408467 - Venus : 0.61519726 - Earth : 1.0 - Mars : 1.8808158 - Jupiter : 11.862615 - Saturn : 29.447498 - Uranus : 84.016846 - Neptune : 164.79132 - -seconds-in-year =: 365.25 * 24 * 60 * 60 +s =: 365.25 * 24 * 60 * 60 +planets =:: + Mercury :: s * 0.2408467 + Venus :: s * 0.61519726 + Earth :: s * 1.0 + Mars :: s * 1.8808158 + Jupiter :: s * 11.862615 + Saturn :: s * 29.447498 + Uranus :: s * 84.016846 + Neptune :: s * 164.79132 defn age(planet seconds): - factor =: factors.$planet || - die('not a planet') - - seconds / seconds-in-year / factor: + seconds.div(planets.$planet.or(die('not a planet'))) .format('%.2f'):F diff --git a/exercises/practice/strain/.meta/strain.ys b/exercises/practice/strain/.meta/strain.ys index ce5a740..a7d4bea 100644 --- a/exercises/practice/strain/.meta/strain.ys +++ b/exercises/practice/strain/.meta/strain.ys @@ -1,7 +1,7 @@ !yamlscript/v0 defn keep(list predicate): - mapcat \(when(predicate(_) [_])): list + list.filter(predicate) defn discard(list predicate): - mapcat \(when-not(predicate(_) [_])): list + list.remove(predicate) diff --git a/exercises/practice/word-count/.meta/word-count.ys b/exercises/practice/word-count/.meta/word-count.ys index a9c1ae9..f4c4755 100644 --- a/exercises/practice/word-count/.meta/word-count.ys +++ b/exercises/practice/word-count/.meta/word-count.ys @@ -1,8 +1,5 @@ !yamlscript/v0 defn count-words(sentence): - frequencies: - lc(sentence) - .split(/[^a-z0-9']+/) - .map(\(_.replace(/(^'|'$)/))) - .remove(empty?) + frequencies: lc(sentence) + .re-seq(/\w(?:[\w']*\w)?/) diff --git a/exercises/practice/yacht/.meta/yacht.ys b/exercises/practice/yacht/.meta/yacht.ys index af58a28..53885f7 100644 --- a/exercises/practice/yacht/.meta/yacht.ys +++ b/exercises/practice/yacht/.meta/yacht.ys @@ -1,34 +1,19 @@ !yamlscript/v0 defn score(dice category): - category.replace(/\ / '-') - .str('do-' _).call(dice).or(0) - -defn- do-yacht(dice): eq(dice*) && 50 - -defn- same-number(n): - fn(dice): dice.filter(eq(n)):sum - -do-ones =: same-number(1) -do-twos =: same-number(2) -do-threes =: same-number(3) -do-fours =: same-number(4) -do-fives =: same-number(5) -do-sixes =: same-number(6) - -defn- do-full-house(dice): - freqs =: frequencies(dice) - when freqs.#.eq(2) && freqs:vals.has?(2): - sum: dice - -defn- do-four-of-a-kind(dice): - freqs =: frequencies(dice):V:reverse:M - mul 4: freqs.get(4) || freqs.get(5) || 0 - -defn- do-little-straight(dice): - dice:sort == (1 .. 5) &&: 30 - -defn- do-big-straight(dice): - dice:sort == (2 .. 6) &&: 30 - -defn- do-choice(dice): sum(dice) + dice-set =: dice:distinct:sort + counts =: dice:frequencies + counts2 =: counts:set/map-invert + case category: + -'yacht': dice-set.#.eq(1).and(50).or(0) + -'choice': dice:sum + -'little straight': dice-set.eq([1 2 3 4 5]).and(30).or(0) + -'big straight': dice-set.eq([2 3 4 5 6]).and(30).or(0) + -'four of a kind': counts2(4 counts2(5 0)) * 4 + -'full house': (counts2.2 && counts2.3).and(dice:sum).or(0) + -'ones' : counts(1 0) * 1 + -'twos' : counts(2 0) * 2 + -'threes': counts(3 0) * 3 + -'fours' : counts(4 0) * 4 + -'fives' : counts(5 0) * 5 + -'sixes' : counts(6 0) * 6