Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactorings based on Steffan153's work #40

Merged
merged 4 commits into from
Nov 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ defaults:
jobs:
test:
runs-on: ubuntu-latest
container: ingy/exercism-yamlscript-test-runner:0.1.80
container: ingy/exercism-yamlscript-test-runner:0.1.81

steps:
- name: Checkout repository
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ ROOT := $(shell pwd)

BIN := bin

YS_VERSION := 0.1.80
YS_VERSION := 0.1.81

YS_LOCAL := .local
YS_LOCAL_PREFIX := $(YS_LOCAL)/v$(YS_VERSION)
2 changes: 1 addition & 1 deletion common/gnumakefile.mk
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion common/meta-makefile.mk
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

2 changes: 1 addition & 1 deletion exercises/practice/acronym/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

3 changes: 1 addition & 2 deletions exercises/practice/acronym/.meta/acronym.ys
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
2 changes: 1 addition & 1 deletion exercises/practice/acronym/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/all-your-base/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

36 changes: 10 additions & 26 deletions exercises/practice/all-your-base/.meta/all-your-base.ys
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]
2 changes: 1 addition & 1 deletion exercises/practice/all-your-base/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/allergies/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

19 changes: 4 additions & 15 deletions exercises/practice/allergies/.meta/allergies.ys
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))
2 changes: 1 addition & 1 deletion exercises/practice/allergies/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/anagram/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

7 changes: 2 additions & 5 deletions exercises/practice/anagram/.meta/anagram.ys
Original file line number Diff line number Diff line change
@@ -2,8 +2,5 @@

defn find-anagrams(subject candidates):
filter _ candidates:
partial _ subject:
fn(*words):
word1 word2 =: words.map(lc)
word1 != word2 &&:
sort(word1) == sort(word2)
\((_:lc != subject:lc) &&
(_:lc:sort == subject:lc:sort))
2 changes: 1 addition & 1 deletion exercises/practice/anagram/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/armstrong-numbers/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

2 changes: 1 addition & 1 deletion exercises/practice/armstrong-numbers/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/atbash-cipher/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

10 changes: 4 additions & 6 deletions exercises/practice/atbash-cipher/.meta/atbash-cipher.ys
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)
2 changes: 1 addition & 1 deletion exercises/practice/atbash-cipher/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/bank-account/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

66 changes: 22 additions & 44 deletions exercises/practice/bank-account/.meta/bank-account.ys
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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a net negative. This loses the spirit of the exercise. The tests need to adjusted.

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
2 changes: 1 addition & 1 deletion exercises/practice/bank-account/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/binary-search/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

16 changes: 6 additions & 10 deletions exercises/practice/binary-search/.meta/binary-search.ys
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.++
2 changes: 1 addition & 1 deletion exercises/practice/binary-search/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/bob/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

2 changes: 1 addition & 1 deletion exercises/practice/bob/GNUmakefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION)
ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok))
2 changes: 1 addition & 1 deletion exercises/practice/bottle-song/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.80
export YS_VERSION := 0.1.81

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

Loading