Skip to content

Commit

Permalink
Rename identifiers according to issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
tonymorris committed Sep 4, 2013
1 parent dcfd9a6 commit 2f4e602
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 274 deletions.
1 change: 1 addition & 0 deletions .ghci
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
:set -fno-warn-unused-binds
:set -fno-warn-unused-do-bind
:set -fno-warn-type-defaults

4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ contain examples of data structures and Haskell syntax. The next step is to comp

After this, the following progression of modules is recommended:

* `Monad.Fuunctor`
* `Monad.Moonad`
* `Monad.Functor`
* `Monad.Monad`
* `Monad.State`
* `Monad.StateT`
* `Structure.ListZipper`
Expand Down
6 changes: 3 additions & 3 deletions course.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: course
version: 0.0.7
version: 0.0.8
license: BSD3
license-file: etc/LICENCE
author: Tony Morris <[email protected]>
Expand Down Expand Up @@ -51,8 +51,8 @@ library
Intro.Validation
IO.Interactive
Monad.Compose
Monad.Fuunctor
Monad.Moonad
Monad.Functor
Monad.Monad
Monad.State
Monad.StateT
Parser.JsonParser
Expand Down
10 changes: 6 additions & 4 deletions src/Course.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE NoImplicitPrelude #-}

module Course
(
module Algorithm.Anagrams
Expand All @@ -9,8 +11,8 @@ module Course
, module Intro.Validation
, module IO.Interactive
, module Monad.Compose
, module Monad.Fuunctor
, module Monad.Moonad
, module Monad.Functor
, module Monad.Monad
, module Monad.State
, module Monad.StateT
, module Parser.JsonParser
Expand All @@ -34,8 +36,8 @@ import Intro.Optional
import Intro.Validation
import IO.Interactive
import Monad.Compose
import Monad.Fuunctor
import Monad.Moonad
import Monad.Functor
import Monad.Monad
import Monad.State
import Monad.StateT
import Parser.JsonParser
Expand Down
29 changes: 16 additions & 13 deletions src/IO/Interactive.hs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
{-# LANGUAGE NoImplicitPrelude #-}

module IO.Interactive where

import Monad.Fuunctor
import Monad.Moonad
import Prelude(error, flip, const, (.), putStr, putStrLn, getChar, (==), Maybe(..), Bool(..), IO, String)
import Monad.Functor
import Monad.Monad
import Data.Char
import Data.List(find)

-- | Eliminates any value over which a functor is defined.
vooid ::
Fuunctor m =>
Functor m =>
m a
-> m ()
vooid =
fmaap (const ())
fmap (const ())

-- | A version of @bind@ that ignores the result of the effect.
(>-) ::
Moonad m =>
Monad m =>
m a
-> m b
-> m b
Expand All @@ -24,7 +27,7 @@ vooid =

-- | An infix, flipped version of @bind@.
(>>-) ::
Moonad m =>
Monad m =>
m a
-> (a -> m b)
-> m b
Expand All @@ -33,7 +36,7 @@ vooid =

-- | Runs an action until a result of that action satisfies a given predicate.
untilM ::
Moonad m =>
Monad m =>
(a -> m Bool) -- ^ The predicate to satisfy to stop running the action.
-> m a -- ^ The action to run until the predicate satisfies.
-> m a
Expand All @@ -42,7 +45,7 @@ untilM p a =
p r >>- \q ->
if q
then
reeturn r
return r
else
untilM p a

Expand All @@ -55,14 +58,14 @@ echo =
if c == 'q'
then
putStrLn "Bye!" >-
reeturn True
return True
else
reeturn False)
return False)
(putStr "Enter a character: " >-
getChar >>- \c ->
putStrLn "" >-
putStrLn [c] >-
reeturn c))
return c))

data Op =
Op Char String (IO ()) -- keyboard entry, description, program
Expand Down Expand Up @@ -158,9 +161,9 @@ interactive =
if c == 'q'
then
putStrLn "Bye!" >-
reeturn True
return True
else
reeturn False)
return False)
(putStrLn "Select: " >-
traaverse (\(Op c s _) ->
putStr [c] >-
Expand Down
50 changes: 27 additions & 23 deletions src/Monad/Fuunctor.hs → src/Monad/Functor.hs
Original file line number Diff line number Diff line change
@@ -1,78 +1,82 @@
module Monad.Fuunctor where
{-# LANGUAGE NoImplicitPrelude #-}

module Monad.Functor where

import Prelude(error, IO, fmap)
import qualified Prelude as P
import Intro.Id
import Intro.Optional
import Intro.Validation
import Structure.List

class Fuunctor f where
fmaap :: (a -> b) -> f a -> f b
class Functor f where
fmap :: (a -> b) -> f a -> f b

-- Exercise 1
-- Relative Difficulty: 1
--
-- | Maps a function on the Id functor.
--
-- >>> fmaap (+1) (Id 2)
-- >>> fmap (+1) (Id 2)
-- Id 3
instance Fuunctor Id where
fmaap =
instance Functor Id where
fmap =
error "todo"

-- Exercise 2
-- Relative Difficulty: 2
--
-- | Maps a function on the List functor.
--
-- >>> fmaap (+1) Nil
-- >>> fmap (+1) Nil
-- []
--
-- >>> fmaap (+1) (1 :. 2 :. 3 :. Nil)
-- >>> fmap (+1) (1 :. 2 :. 3 :. Nil)
-- [2,3,4]
instance Fuunctor List where
fmaap =
instance Functor List where
fmap =
error "todo"

-- Exercise 3
-- Relative Difficulty: 2
--
-- | Maps a function on the Optional functor.
--
-- >>> fmaap (+1) Empty
-- >>> fmap (+1) Empty
-- Empty
--
-- >>> fmaap (+1) (Full 2)
-- >>> fmap (+1) (Full 2)
-- Full 3
instance Fuunctor Optional where
fmaap =
instance Functor Optional where
fmap =
error "todo"

-- Exercise 4
-- Relative Difficulty: 3
--
-- | Maps a function on the reader ((->) t) functor.
--
-- >>> fmaap (+1) (*2) 8
-- >>> fmap (+1) (*2) 8
-- 17
instance Fuunctor ((->) t) where
fmaap =
instance Functor ((->) t) where
fmap =
error "todo"

-- Exercise 5
-- Relative Difficulty: 2
--
-- | Maps a function on an IO program.
--
-- >>> fmaap reverse (putStr "hi" >> return "abc")
-- >>> fmap reverse (putStr "hi" >> return "abc")
-- hi"cba"
instance Fuunctor IO where
fmaap =
instance Functor IO where
fmap =
error "todo"

-----------------------
-- SUPPORT LIBRARIES --
-----------------------

instance Fuunctor [] where
fmaap =
fmap
instance Functor [] where
fmap =
P.fmap
58 changes: 58 additions & 0 deletions src/Monad/Functor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package Monad

import Intro._
import Structure._

trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]

// Provided as a synonym for `fmap` but with the arguments flipped.
// This helps the type-inferencer.
final def apply[A, B](a: F[A])(f: A => B): F[B] =
fmap(f)(a)
}

object Functor {
// Exercise 1
// Relative Difficulty: 1
implicit val IdFunctor: Functor[Id] =
new Functor[Id] {
def fmap[A, B](f: A => B) =
sys.error("todo")
}

// Exercise 2
// Relative Difficulty: 2
implicit val ListFunctor: Functor[List] =
new Functor[List] {
def fmap[A, B](f: A => B) =
sys.error("todo")
}

// Exercise 3
// Relative Difficulty: 2
implicit val OptionalFunctor: Functor[Optional] =
new Functor[Optional] {
def fmap[A, B](f: A => B) =
sys.error("todo")
}

// Exercise 4
// Relative Difficulty: 3
implicit def Function1Functor[T]: Functor[({type l[a] = T => a})#l] =
new Functor[({type l[a] = T => a})#l] {
def fmap[A, B](f: A => B) =
sys.error("todo")
}

///////////////////////
// SUPPORT LIBRARIES //
///////////////////////

implicit val ScalaListFunctor: Functor[scala.List] =
new Functor[scala.List] {
def fmap[A, B](f: A => B) =
_ map f
}

}
58 changes: 0 additions & 58 deletions src/Monad/Fuunctor.scala

This file was deleted.

Loading

0 comments on commit 2f4e602

Please sign in to comment.