-
-
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 (#40)
* Refactorings based on Steffan153's work * No longer need dash before string keys for case/condp * Update for 0.1.81 * Small change based on vendethiel's review
- Loading branch information
1 parent
0391247
commit 084ef73
Showing
168 changed files
with
372 additions
and
609 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
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
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
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,28 +1,12 @@ | ||
!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. | ||
|
||
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?) || 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 \(%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 | ||
quot: num output-base | ||
else: digits | ||
defn rebase(input digits output): | ||
when input < 2: die('input base must be >= 2') | ||
when output < 2: die('output base must be >= 2') | ||
when-not digits.every?(\(0 <= _ <= input.--)): | ||
die('all digits must satisfy 0 <= d < input base') | ||
|
||
loop n reduce(\((%1 * input) + %2) 0 digits), res []: | ||
if n.?: | ||
=>: recur(quot(n output) res.cons(n % output)) | ||
res |||: -[0] |
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,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
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
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,12 +1,10 @@ | ||
!yamlscript/v0 | ||
|
||
dict =: | ||
zipmap (\\a .. \\z): \\z .. \\a | ||
|
||
defn encode(phrase): | ||
lc(phrase): | ||
.replace(/[^a-z0-9]/).escape(dict) | ||
lc(phrase):decode: | ||
.replace(/[^a-z0-9]/) | ||
.partition(5 5 '').map(join):joins | ||
|
||
defn decode(phrase): | ||
phrase.replace(' ').escape(dict) | ||
m =: (\\a .. \\z).zipmap(\\z .. \\a) | ||
phrase: .replace(' ').escape(m) |
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,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
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,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
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.