-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactorings based on Steffan153's work
- Loading branch information
1 parent
4374e99
commit a9edbba
Showing
34 changed files
with
145 additions
and
314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
!yamlscript/v0 | ||
|
||
defn abbreviate(phrase): | ||
uc(phrase): | ||
.re-seq(/[A-Z']+/) | ||
uc(phrase).re-seq(/[A-Z']+/) | ||
.map(first):join |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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).! |
19 changes: 5 additions & 14 deletions
19
exercises/practice/matching-brackets/.meta/matching-brackets.ys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
8 changes: 3 additions & 5 deletions
8
exercises/practice/nucleotide-count/.meta/nucleotide-count.ys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
!yamlscript/v0 | ||
|
||
defn is-pangram(sentence): | ||
26 ==: lc(sentence) | ||
.replace(/[^a-z]/) | ||
.distinct():len | ||
B: sentence =~ /(?i)(?>.*?(\pL)(?!.*\1)){26}/ |
10 changes: 2 additions & 8 deletions
10
exercises/practice/pascals-triangle/.meta/pascals-triangle.ys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.