Skip to content

Commit

Permalink
github build
Browse files Browse the repository at this point in the history
  • Loading branch information
emadb committed Nov 2, 2023
1 parent 7ca0c52 commit a8a5e4b
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 168 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Elixir CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
name: OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
container:
image: elixir:1.15
env:
MIX_ENV: test
steps:
- uses: actions/checkout@v3
- name: Install Dependencies
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get
- name: Check Formatting
run: mix format --check-formatted
- name: Run Tests
run: mix test

5 changes: 2 additions & 3 deletions lib/chapter_02.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule LittleAlchemist.Chapter02 do

# def lat?(lat) do
# cond do
# null?(lat) -> true
Expand All @@ -9,8 +8,8 @@ defmodule LittleAlchemist.Chapter02 do
# end

def lat?([]), do: true
def lat?([[_|_]|_]), do: false
def lat?([_|t]), do: lat?(t)
def lat?([[_ | _] | _]), do: false
def lat?([_ | t]), do: lat?(t)

# def member?(a, lat) do
# cond do
Expand Down
1 change: 0 additions & 1 deletion lib/chapter_03.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ defmodule LittleAlchemist.Chapter03 do
def firsts([]), do: []
def firsts([[h | _] | t]), do: [h | firsts(t)]


# def insertR(new, old, lat) do
# cond do
# null?(lat) -> []
Expand Down
17 changes: 5 additions & 12 deletions lib/chapter_04.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule LittleAlchemist.Chapter04 do

def zero?(n), do: n == 0

def add1(n), do: n + 1
Expand All @@ -15,25 +14,22 @@ defmodule LittleAlchemist.Chapter04 do
def add(n, 0), do: n
def add(n, m), do: 1 + add(n, m - 1)


def sub(n, 0), do: n
def sub(n, m), do: sub(n, m - 1) - 1


def addtup([]), do: 0
def addtup([h | t]), do: add(h, addtup(t))

def addtup(t1, []), do: t1
def addtup([], t2), do: t2
def addtup([h1|t1], [h2|t2]), do: [h1 + h2 | addtup(t1, t2)]
def addtup([h1 | t1], [h2 | t2]), do: [h1 + h2 | addtup(t1, t2)]

# def addtup([], l), do: l
# def addtup(l, []), do: l
# def addtup(a, b), do: cons(add(car(a), car(b)), addtup(cdr(a), cdr(b)))

def mul(n, 1), do: n
def mul(n, m), do: n + mul(n, m-1)

def mul(n, m), do: n + mul(n, m - 1)

def greater_than(0, _), do: false
def greater_than(_, 0), do: true
Expand All @@ -43,7 +39,6 @@ defmodule LittleAlchemist.Chapter04 do
def less_than(0, _), do: true
def less_than(x, y), do: less_than(sub1(x), sub1(y))


# def equals(n, m) do
# cond do
# greater_than(n, m) -> false
Expand All @@ -55,7 +50,7 @@ defmodule LittleAlchemist.Chapter04 do
def equals(0, 0), do: true
def equals(_, 0), do: false
def equals(0, _), do: false
def equals(n, m), do: equals(n-1, m-1)
def equals(n, m), do: equals(n - 1, m - 1)

def power(_, 0), do: 1
def power(n, m), do: mul(n, power(n, sub1(m)))
Expand All @@ -71,7 +66,7 @@ defmodule LittleAlchemist.Chapter04 do
# end

def divide(n, m) when n < m, do: 0
def divide(n, m), do: 1 + divide(n-m, m)
def divide(n, m), do: 1 + divide(n - m, m)

def size([]), do: 0
def size([_ | t]), do: 1 + size(t)
Expand All @@ -87,8 +82,7 @@ defmodule LittleAlchemist.Chapter04 do
def pick(n, [_ | t]), do: pick(n - 1, t)

def rempick(1, [_ | t]), do: t
def rempick(n, [h | t]), do: [h | rempick(n-1, t)]

def rempick(n, [h | t]), do: [h | rempick(n - 1, t)]

def nonums([]), do: []
def nonums([h | t]) when is_number(h), do: nonums(t)
Expand All @@ -107,5 +101,4 @@ defmodule LittleAlchemist.Chapter04 do

def one?(1), do: true
def one?(_), do: false

end
90 changes: 46 additions & 44 deletions lib/chapter_05.ex
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
defmodule LittleAlchemist.Chapter05 do
import LittleAlchemist.Chapter01

def rember_star(_, []), do: []
def rember_star(a, [a | t]), do: rember_star(a, t)
def rember_star(a, [h | t]) when is_atom(h), do: [h | rember_star(a, t)]
def rember_star(a, [h | t]), do: [rember_star(a, h) | rember_star(a, t)]

def insertR_star(_, _, []), do: []
def insertR_star(n, o, [o | t]), do: [o, n | insertR_star(n, o, t)]
def insertR_star(n, o, [h | t]) when is_atom(h), do: [h | insertR_star(n, o, t)]
def insertR_star(n, o, [h | t]), do: [insertR_star(n, o, h) | insertR_star(n, o, t)]

def occur_star(_, []), do: 0
def occur_star(a, [a | t]), do: 1 + occur_star(a, t)
def occur_star(a, [h | t]) when is_atom(h), do: occur_star(a, t)
def occur_star(a, [h | t]), do: occur_star(a, h) + occur_star(a, t)

def subst_star(_, _, []), do: []
def subst_star(n, o, [o | t]), do: [n | subst_star(n, o, t)]
def subst_star(n, o, [h | t]) when is_atom(h), do: [h | subst_star(n, o, t)]
def subst_star(n, o, [h | t]), do: [subst_star(n, o, h) | subst_star(n, o, t)]

def insertL_star(_, _, []), do: []
def insertL_star(n, o, [o | t]), do: [n, o | insertL_star(n, o, t)]
def insertL_star(n, o, [h | t]) when is_atom(h), do: [h | insertL_star(n, o, t)]
def insertL_star(n, o, [h | t]), do: [insertL_star(n, o, h) | insertL_star(n, o, t)]

def member_star(_, []), do: false
def member_star(a, [a | _]), do: true
def member_star(a, [h | t]) when is_atom(h), do: member_star(a, t)
def member_star(a, [h | t]), do: member_star(a, h) || member_star(a, t)

def leftmost([]), do: []
def leftmost([h | _]) when is_atom(h), do: h
def leftmost([h | _]), do: leftmost(h)

def eqlist?([], []), do: true
def eqlist?([], _), do: false
def eqlist?(_, []), do: false
def eqlist?([h1 | _], [h2 | _]) when is_atom(h1) and not is_atom(h2), do: false
def eqlist?([h1 | _], [h2 | _]) when not is_atom(h1) and is_atom(h2), do: false
def eqlist?([h1 | t1], [h2 | t2]) when is_atom(h1) and is_atom(h2), do: h1 == h2 && eqlist?(t1,t2)
def eqlist?([h1 | t1], [h2 | t2]), do: eqlist?(h1, h2) && eqlist?(t1,t2)

import LittleAlchemist.Chapter01

def rember_star(_, []), do: []
def rember_star(a, [a | t]), do: rember_star(a, t)
def rember_star(a, [h | t]) when is_atom(h), do: [h | rember_star(a, t)]
def rember_star(a, [h | t]), do: [rember_star(a, h) | rember_star(a, t)]

def insertR_star(_, _, []), do: []
def insertR_star(n, o, [o | t]), do: [o, n | insertR_star(n, o, t)]
def insertR_star(n, o, [h | t]) when is_atom(h), do: [h | insertR_star(n, o, t)]
def insertR_star(n, o, [h | t]), do: [insertR_star(n, o, h) | insertR_star(n, o, t)]

def occur_star(_, []), do: 0
def occur_star(a, [a | t]), do: 1 + occur_star(a, t)
def occur_star(a, [h | t]) when is_atom(h), do: occur_star(a, t)
def occur_star(a, [h | t]), do: occur_star(a, h) + occur_star(a, t)

def subst_star(_, _, []), do: []
def subst_star(n, o, [o | t]), do: [n | subst_star(n, o, t)]
def subst_star(n, o, [h | t]) when is_atom(h), do: [h | subst_star(n, o, t)]
def subst_star(n, o, [h | t]), do: [subst_star(n, o, h) | subst_star(n, o, t)]

def insertL_star(_, _, []), do: []
def insertL_star(n, o, [o | t]), do: [n, o | insertL_star(n, o, t)]
def insertL_star(n, o, [h | t]) when is_atom(h), do: [h | insertL_star(n, o, t)]
def insertL_star(n, o, [h | t]), do: [insertL_star(n, o, h) | insertL_star(n, o, t)]

def member_star(_, []), do: false
def member_star(a, [a | _]), do: true
def member_star(a, [h | t]) when is_atom(h), do: member_star(a, t)
def member_star(a, [h | t]), do: member_star(a, h) || member_star(a, t)

def leftmost([]), do: []
def leftmost([h | _]) when is_atom(h), do: h
def leftmost([h | _]), do: leftmost(h)

def eqlist?([], []), do: true
def eqlist?([], _), do: false
def eqlist?(_, []), do: false
def eqlist?([h1 | _], [h2 | _]) when is_atom(h1) and not is_atom(h2), do: false
def eqlist?([h1 | _], [h2 | _]) when not is_atom(h1) and is_atom(h2), do: false

def eqlist?([h1 | t1], [h2 | t2]) when is_atom(h1) and is_atom(h2),
do: h1 == h2 && eqlist?(t1, t2)

def eqlist?([h1 | t1], [h2 | t2]), do: eqlist?(h1, h2) && eqlist?(t1, t2)
end
13 changes: 10 additions & 3 deletions test/chapter_01_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule LittleAlchemist.Chapter01Test do

test "car" do
assert car([:a, :b, :c]) == :a
assert car([[:a, :b, :c], :x, :y]) == [:a, :b, :c]
assert car([[:a, :b, :c], :x, :y]) == [:a, :b, :c]
end

test "cdr" do
Expand All @@ -19,7 +19,15 @@ defmodule LittleAlchemist.Chapter01Test do

test "cons" do
assert cons(:peanut, [:butter, :and, :jelly]) == [:peanut, :butter, :and, :jelly]
assert cons([:banana, :and], [:peanut, :butter, :and, :jelly]) == [[:banana, :and], :peanut, :butter, :and, :jelly]

assert cons([:banana, :and], [:peanut, :butter, :and, :jelly]) == [
[:banana, :and],
:peanut,
:butter,
:and,
:jelly
]

assert cons([:a, [:b, [:c]]], []) == [[:a, [:b, [:c]]]]
end

Expand All @@ -42,5 +50,4 @@ defmodule LittleAlchemist.Chapter01Test do
assert eq?(:a, :a) == true
assert eq?(:a, :b) == false
end

end
9 changes: 4 additions & 5 deletions test/chapter_02_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ defmodule LittleAlchemist.Chapter02Test do
import LittleAlchemist.Chapter02

test "lat?" do
res = lat? [:jack, :sprat, :could, :eat, :no, :chicken]
res = lat?([:jack, :sprat, :could, :eat, :no, :chicken])
assert res == true

res = lat? [[:jack], :sprat, :could, :eat, :no, :chicken]
res = lat?([[:jack], :sprat, :could, :eat, :no, :chicken])
assert res == false
end

test "member?" do
res = member? :c, [:a, :b, :c, :d]
res = member?(:c, [:a, :b, :c, :d])
assert res == true

res = member? :f, [:a, :b, :c, :d]
res = member?(:f, [:a, :b, :c, :d])
assert res == false
end

end
18 changes: 9 additions & 9 deletions test/chapter_03_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule LittleAlchemist.Chapter03Test do
rember(:mint, [:lamb, :chops, :and, :mint, :jelly])

[:lamb, :chops, :and, :flavored, :mint, :jelly]
rember(:mint, [:lamb, :chops, :and, :mint, :flavored, :mint, :jelly])
rember(:mint, [:lamb, :chops, :and, :mint, :flavored, :mint, :jelly])

[:bacon, :lettuce, :and, :tomato] =
rember(:toast, [:bacon, :lettuce, :and, :tomato])
Expand All @@ -18,10 +18,12 @@ defmodule LittleAlchemist.Chapter03Test do

test "firsts" do
[:apple, :plum, :grape, :bean] =
firsts([[:apple, :peach, :pumpkin],
[:plum, :pear, :cherry],
[:grape, :raisin, :pea],
[:bean, :carrot, :eggplant]])
firsts([
[:apple, :peach, :pumpkin],
[:plum, :pear, :cherry],
[:grape, :raisin, :pea],
[:bean, :carrot, :eggplant]
])

[:a, :c, :e] =
firsts([[:a, :b], [:c, :d], [:e, :f]])
Expand Down Expand Up @@ -52,13 +54,12 @@ defmodule LittleAlchemist.Chapter03Test do
subst(:topping, :fudge, [:ice, :cream, :with, :fudge, :for, :desert])

[:this, :foo, :is, :for, :you]
subst(:foo, :bar, [:this, :bar, :is, :for, :you])
subst(:foo, :bar, [:this, :bar, :is, :for, :you])
end

test "subst2" do
[:vanilla, :ice, :cream, :with, :chocolate, :topping] =
subst2(:vanilla, :chocolate, :banana,
[:banana, :ice, :cream, :with, :chocolate, :topping])
subst2(:vanilla, :chocolate, :banana, [:banana, :ice, :cream, :with, :chocolate, :topping])
end

test "multirember" do
Expand All @@ -78,5 +79,4 @@ defmodule LittleAlchemist.Chapter03Test do
[:my, :other, :foo, :is, :a, :foo] =
multisubst(:foo, :bar, [:my, :other, :bar, :is, :a, :bar])
end

end
1 change: 0 additions & 1 deletion test/chapter_04_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,4 @@ defmodule LittleAlchemist.Chapter04Test do
assert one?(1)
refute one?(9)
end

end
Loading

0 comments on commit a8a5e4b

Please sign in to comment.