Skip to content

Commit

Permalink
trivial Lazy instance for functions (#38)
Browse files Browse the repository at this point in the history
* trivial Lazy instance for functions

* make lazyFn defer actually defer fn call

Add tests for this.

* relax types in tests
  • Loading branch information
matthewleon authored and paf31 committed May 28, 2017
1 parent 33bcce0 commit c393ee5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ install:
- chmod a+x $HOME/purescript
- npm install -g bower
- npm install
- bower install
script:
- bower install --production
- npm run -s build
- bower install
- npm -s test
after_success:
- >-
test $TRAVIS_TAG &&
Expand Down
3 changes: 3 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
],
"dependencies": {
"purescript-prelude": "^3.0.0"
},
"devDependencies": {
"purescript-eff": "^3.1.0"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build -- --censor-lib --strict"
"build": "pulp build -- --censor-lib --strict",
"test": "pulp test"
},
"devDependencies": {
"pulp": "^10.0.4",
Expand Down
5 changes: 4 additions & 1 deletion src/Control/Lazy.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Control.Lazy where

import Data.Unit (Unit)
import Data.Unit (Unit, unit)

-- | The `Lazy` class represents types which allow evaluation of values
-- | to be _deferred_.
Expand All @@ -10,6 +10,9 @@ import Data.Unit (Unit)
class Lazy l where
defer :: (Unit -> l) -> l

instance lazyFn :: Lazy (a -> b) where
defer f = \x -> f unit x

-- | `fix` defines a value as the fixed point of a function.
-- |
-- | The `Lazy` instance allows us to generate the result lazily.
Expand Down
19 changes: 19 additions & 0 deletions test/Test/Control/Lazy.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Test.Control.Lazy (testLazy) where

import Control.Applicative (pure)
import Control.Monad.Eff (Eff)
import Control.Lazy (fix)
import Data.Unit (Unit, unit)

foo :: forall a. a -> Unit
foo _ = unit

foofoo :: forall a b. a -> (b -> Unit)
foofoo _ = foo

foo' :: forall a. a -> Unit
foo' = fix foofoo

-- the idea here is that foo and foo' are the same function
testLazy :: Eff () Unit
testLazy = pure (foo' unit)
9 changes: 9 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Test.Main (main) where

import Control.Monad.Eff (Eff)
import Data.Unit (Unit)

import Test.Control.Lazy (testLazy)

main :: Eff () Unit
main = testLazy

0 comments on commit c393ee5

Please sign in to comment.