-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3b073db
Showing
22 changed files
with
263 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/_build | ||
/cover | ||
/deps | ||
erl_crash.dump | ||
*.ez |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Shane Logsdon | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Control | ||
|
||
An exploratory look into functors, applicatives, and monads for Elixir. See [the accompanying blog post](http://www.slogsdon.com/functors-applicatives-and-monads-in-elixir/) for more details. | ||
|
||
> More code, documentation, and tests are in progress. | ||
## Installation | ||
|
||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed as: | ||
|
||
1. Add control to your list of dependencies in `mix.exs`: | ||
|
||
def deps do | ||
[{:control, "~> 0.0.1"}] | ||
end | ||
|
||
2. Ensure control is started before your application: | ||
|
||
def application do | ||
[applications: [:control]] | ||
end | ||
|
||
## License | ||
|
||
Control is released under the MIT License. | ||
|
||
See [LICENSE](https://github.com/slogsdon/elixir-control/blob/master/LICENSE) for details. |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
use Mix.Config | ||
|
||
# This configuration is loaded before any dependency and is restricted | ||
# to this project. If another project depends on this project, this | ||
# file won't be loaded nor affect the parent project. For this reason, | ||
# if you want to provide default values for your application for | ||
# 3rd-party users, it should be done in your "mix.exs" file. | ||
|
||
# You can configure for your application as: | ||
# | ||
# config :control, key: :value | ||
# | ||
# And access this configuration in your application as: | ||
# | ||
# Application.get_env(:control, :key) | ||
# | ||
# Or configure a 3rd-party app: | ||
# | ||
# config :logger, level: :info | ||
# | ||
|
||
# It is also possible to import configuration files, relative to this | ||
# directory. For example, you can emulate configuration per environment | ||
# by uncommenting the line below and defining dev.exs, test.exs and such. | ||
# Configuration from the imported file will override the ones defined | ||
# here (which is why it is important to import them last). | ||
# | ||
# import_config "#{Mix.env}.exs" |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Control do | ||
@moduledoc false | ||
end |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defprotocol Control.Applicative do | ||
@moduledoc """ | ||
""" | ||
|
||
@doc """ | ||
""" | ||
@spec apply(t, Control.Functor.t) :: t | ||
def apply(fun, f) | ||
end |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
defimpl Control.Applicative, for: Data.Maybe do | ||
def apply(%{nothing: true} = f, _), do: f | ||
def apply(%{just: fun}, f) do | ||
f |> Control.Functor.fmap(fun) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defprotocol Control.Functor do | ||
@moduledoc """ | ||
Functors are things that can be mapped over, like lists, | ||
`Maybe`s, trees, and such. | ||
""" | ||
|
||
@doc """ | ||
""" | ||
@spec fmap(t, (term -> term)) :: t | ||
def fmap(functor, fun) | ||
end |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defimpl Control.Functor, for: Data.Either do | ||
def fmap(%{left: l} = f, _) when l != nil, do: f | ||
def fmap(%{right: r}, fun) do | ||
fun | ||
|> apply([r]) | ||
|> Data.Either.right | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defimpl Control.Functor, for: List do | ||
defdelegate fmap(list, fun), to: Enum, as: :map | ||
end |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defimpl Control.Functor, for: Data.Maybe do | ||
def fmap(%{nothing: true} = f, _), do: f | ||
def fmap(%{just: v}, fun) do | ||
fun | ||
|> apply([v]) | ||
|> Data.Maybe.just | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defprotocol Control.Monad do | ||
@moduledoc """ | ||
""" | ||
|
||
@doc """ | ||
""" | ||
@spec bind(t, (term -> t)) :: t | ||
def bind(m, fun) | ||
|
||
def left ~>> right | ||
end |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defimpl Control.Monad, for: Data.Maybe do | ||
def bind(%{nothing: true} = f, _), do: f | ||
def bind(%{just: v}, fun) do | ||
fun |> apply([v]) | ||
end | ||
|
||
def left ~>> right do | ||
left |> bind(right) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defprotocol Control.Monoid do | ||
@moduledoc """ | ||
""" | ||
|
||
@doc """ | ||
""" | ||
@spec mempty(t) :: t | ||
def mempty(a) | ||
|
||
@doc """ | ||
""" | ||
@spec mappend(t, t) :: t | ||
def mappend(a, b) | ||
end |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
defimpl Control.Monoid, for: List do | ||
def mempty(_), do: [] | ||
defdelegate mappend(a, b), to: Kernel, as: :++ | ||
end |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Control.SpecialForms do | ||
@doc """ | ||
defmodule Test do | ||
require Control.SpecialForms, as: M | ||
def test do | ||
Data.Maybe.just(5) | ||
|> M.'>>='(fn x -> Data.Maybe.just(x + 5) end) | ||
end | ||
end | ||
Test.test | ||
# %Data.Maybe{just: 10, nothing: false} | ||
""" | ||
defmacro unquote(:'>>=')(left, right) do | ||
quote do | ||
unquote(left) | ||
|> Control.Monad.bind(unquote(right)) | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Data.Either do | ||
@type t :: %__MODULE__{ | ||
left: term, | ||
right: term | ||
} | ||
defstruct left: nil, | ||
right: nil | ||
|
||
def left(v), do: __MODULE__ |> struct(left: v) | ||
def right(v), do: __MODULE__ |> struct(right: v) | ||
end |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Data.Maybe do | ||
@type t :: %__MODULE__{ | ||
just: term, | ||
nothing: boolean | ||
} | ||
defstruct just: nil, | ||
nothing: false | ||
|
||
def just(v), do: __MODULE__ |> struct(just: v) | ||
def nothing, do: __MODULE__ |> struct(nothing: true) | ||
end |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Control.Mixfile do | ||
use Mix.Project | ||
|
||
def project do | ||
[app: :control, | ||
version: "0.0.1", | ||
elixir: "~> 1.1", | ||
build_embedded: Mix.env == :prod, | ||
start_permanent: Mix.env == :prod, | ||
deps: deps] | ||
end | ||
|
||
# Configuration for the OTP application | ||
# | ||
# Type "mix help compile.app" for more information | ||
def application do | ||
[applications: [:logger]] | ||
end | ||
|
||
# Dependencies can be Hex packages: | ||
# | ||
# {:mydep, "~> 0.3.0"} | ||
# | ||
# Or git/path repositories: | ||
# | ||
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} | ||
# | ||
# Type "mix help deps" for more examples and options | ||
defp deps do | ||
[] | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Data.Maybe | ||
alias Control.Applicative, as: A | ||
import Control.Monad | ||
|
||
(&(&1 + 2)) | ||
|> just | ||
|> A.apply(just(5)) | ||
|> bind(&(&1 * 2 |> just)) | ||
|> IO.inspect |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule ControlTest do | ||
use ExUnit.Case | ||
doctest Control | ||
|
||
test "the truth" do | ||
assert 1 + 1 == 2 | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
ExUnit.start() |