Skip to content

Commit

Permalink
Sketch out more modules/protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
expede committed Oct 10, 2015
1 parent f0e9be7 commit f881086
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
12 changes: 11 additions & 1 deletion lib/witchcraft/arrow.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
defmodule Witchcraft.Arrow do
defprotocol Witchcraft.Arrow do
@type arrow :: {any, any, any}
@type split :: {any, any}

@spec arr((any -> any)) :: arrow
def arr(b->c)

@spec first(arrow) :: arrow
def first(arrow)

@spec second(arrow) :: arrow
def second(arrow)
end
15 changes: 15 additions & 0 deletions lib/witchcraft/arrow/extra.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Witchcraft.Arrow.Extra do
alias Witchcraft.Category, as: C
alias Witchcraft.Arrow, as: A

@spec product(arrow, arrow) :: arrow
def product(f, g) do
C.compose_left_to_right(A.first(f), A.second(g))
end

@spec fanout(arrow, arrow) :: arrow
def fanout(f, g) do
A.arr &({&1, &1})
|> C.compose_left_to_right(product(f,g))
end
end
4 changes: 4 additions & 0 deletions lib/witchcraft/category.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defprotocol Witchcraft.Category do
def id
def compose
end
11 changes: 11 additions & 0 deletions lib/witchcraft/category/extra.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Witchcraft.Category.Extra do
alias Witchcraft.Category, as: C

def compose_left_to_right(a->b, b->c) do
&(&1 |> C.compose(a->b) |> C.compose(b->c))
end

def compose_right_to_left(b->c, a->b) do
&(&1 |> C.compose(a->b) |> C.compose(b->c))
end
end
10 changes: 9 additions & 1 deletion lib/witchcraft/monad.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
defmodule Witchcraft.Monad do
defprotocol Witchcraft.Monad do

@spec return(any) :: any
def return(bare)

@spec bind(any, (any -> any)) :: any
def bind(wrapped_element, wrapping_func)

@spec then(any, any) :: any
def then(wrapped_element, wrapped_element)
end

0 comments on commit f881086

Please sign in to comment.