Skip to content

Commit

Permalink
initial commit >>=
Browse files Browse the repository at this point in the history
  • Loading branch information
slogsdon committed Oct 4, 2015
0 parents commit 3b073db
Show file tree
Hide file tree
Showing 22 changed files with 263 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/_build
/cover
/deps
erl_crash.dump
*.ez
21 changes: 21 additions & 0 deletions LICENSE
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.
27 changes: 27 additions & 0 deletions README.md
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.
30 changes: 30 additions & 0 deletions config/config.exs
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"
3 changes: 3 additions & 0 deletions lib/control.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule Control do
@moduledoc false
end
9 changes: 9 additions & 0 deletions lib/control/applicative.ex
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
6 changes: 6 additions & 0 deletions lib/control/applicative/maybe.ex
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
11 changes: 11 additions & 0 deletions lib/control/functor.ex
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
8 changes: 8 additions & 0 deletions lib/control/functor/either.ex
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
3 changes: 3 additions & 0 deletions lib/control/functor/list.ex
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
8 changes: 8 additions & 0 deletions lib/control/functor/maybe.ex
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
11 changes: 11 additions & 0 deletions lib/control/monad.ex
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
10 changes: 10 additions & 0 deletions lib/control/monad/maybe.ex
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
14 changes: 14 additions & 0 deletions lib/control/monoid.ex
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
4 changes: 4 additions & 0 deletions lib/control/monoid/list.ex
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
21 changes: 21 additions & 0 deletions lib/control/special_forms.ex
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
11 changes: 11 additions & 0 deletions lib/data/either.ex
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
11 changes: 11 additions & 0 deletions lib/data/maybe.ex
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
32 changes: 32 additions & 0 deletions mix.exs
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
9 changes: 9 additions & 0 deletions test.exs
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
8 changes: 8 additions & 0 deletions test/control_test.exs
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
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit 3b073db

Please sign in to comment.