From 733972e92546fe32fa26284b0f75f9912f5e2bc5 Mon Sep 17 00:00:00 2001 From: "Quildreen \"Sorella\" Motta" Date: Fri, 17 Jan 2014 01:15:10 -0200 Subject: [PATCH] Implements other functionality. --- LICENCE | 2 +- README.md | 2 +- package.json | 10 +++--- src/index.ls | 69 +++++++++++++++++++++++++++++++++++---- test/specs/index.ls | 3 +- test/specs/monad-laws.ls | 70 ---------------------------------------- test/tap.ls | 2 +- 7 files changed, 72 insertions(+), 86 deletions(-) delete mode 100644 test/specs/monad-laws.ls diff --git a/LICENCE b/LICENCE index 599eb3d..425dcda 100644 --- a/LICENCE +++ b/LICENCE @@ -1,6 +1,6 @@ The MIT License -Copyright (c) 2013 Quildreen Motta +Copyright (c) 2013-2014 Quildreen Motta Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 5a949b2..e494abd 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ platforms by the use of shims. Just include [es5-shim][] :) ## Licence -Copyright (c) 2013 Quildreen Motta. +Copyright (c) 2013-2014 Quildreen Motta. Released under the [MIT licence](https://github.com/folktale/control.async/blob/master/LICENCE). diff --git a/package.json b/package.json index ca0d870..61d8513 100644 --- a/package.json +++ b/package.json @@ -21,15 +21,17 @@ "url": "https://github.com/folktale/control.async/issues" }, "dependencies": { - "monads.future": "~0.1.0" + "flaw": "~0.1.0", + "data.future": "~2.0.0" }, "devDependencies": { - "browserify": "~2.35.4", - "groc": "git://github.com/killdream/groc#patch-livescript", + "browserify": "git://github.com/robotlolita/node-browserify", + "groc": "git://github.com/robotlolita/groc", "LiveScript": "~1.2.0", "hifive-tap": "~0.1.0", "hifive": "~0.1.0", "uglify-js": "~2.4.3", - "laws": "~0.2.0" + "laws": "~0.2.0", + "claire": "~0.4.1" } } diff --git a/src/index.ls b/src/index.ls index 2356e41..063ec75 100644 --- a/src/index.ls +++ b/src/index.ls @@ -1,7 +1,7 @@ # # control.async /** ^ - * Copyright (c) 2013 Quildreen Motta + * Copyright (c) 2013-2014 Quildreen Motta * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files @@ -25,14 +25,69 @@ # Operations for asynchronous control flow. -Future = require 'monads.future' +Future = require 'data.future' +flaw = require 'flaw' + +TimeoutError = (n) -> flaw 'TimeoutError', "Timeoutted after #n milliseconds." # # Function: delay # -# Returns a promise that gets resolved after X seconds +# Returns a promise that gets resolved after X milliseconds # -# + type: Int -> Future(a,Int) -export delay = (n) -> new Future (reject, resolve) -> do - s = new Date - set-timeout (-> resolve (new Date - s)), n * 1000 +# + type: Int -> Future(a, Int) +export delay = (n) -> new Future (reject, resolve) !-> + s = new Date + set-timeout (-> resolve (new Date - s)), n + + +# # Function: timeout +# +# Returns a promise that fails after X milliseconds +# +# + type: Int -> Future(Error, a) +export timeout = (n) -> new Future (reject, resolve) !-> + s = new Date + set-timeout (-> reject (TimeoutError n)), n + + +# # Function: parallel +# +# Resolves all futures in parallel. +# +# + type: [Future(a, b)] -> Future(a, [b]) +export parallel = (xs) -> new Future (reject, resolve) !-> + len = xs.length + result = new Array len + resolved = false + + for x,i in xs => compute x, i + + function compute(x, i) => x.fork do + * (e) -> do + if resolved => return + resolved := true + reject e + * (v) -> do + if resolved => return + result[i] := v + len := len - 1 + if len is 0 => do + resolved := true + resolve result + + +# # Function: nondeterministic-choice +# +# Returns the value of the first resolved or rejected future. +# +# + type: [Future(a, b)] -> Future(a, b) +export nondeterministic-choice = (xs) -> new Future (reject, resolve) !-> + resolved = false + for x,i in xs => x.fork do + * (e) -> transition reject, e + * (v) -> transition resolve, v + + function transition(f, a) => if not resolved + resolved := true + f a diff --git a/test/specs/index.ls b/test/specs/index.ls index 10a2fd5..85e62e8 100644 --- a/test/specs/index.ls +++ b/test/specs/index.ls @@ -1,7 +1,7 @@ # # Entry point for the specifications /** ^ - * Copyright (c) 2013 Quildreen Motta + * Copyright (c) 2013-2014 Quildreen Motta * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files @@ -26,5 +26,4 @@ module.exports = [ # The specification objects go here # See: http://hifivejs.github.io/hifive/getting-started.html - require './monad-laws' ] diff --git a/test/specs/monad-laws.ls b/test/specs/monad-laws.ls deleted file mode 100644 index c6ed683..0000000 --- a/test/specs/monad-laws.ls +++ /dev/null @@ -1,70 +0,0 @@ -# # Specification for the monadic laws - -/** ^ - * Copyright (c) 2013 Quildreen Motta - * - * 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. - */ - -# Before we define the specification, we need to set up the specs -# libraries, and grab a hold of our data structure and the laws we want -# to verify. -spec = (require 'hifive')! -laws = require 'laws' -Async = require '../../src/' - -# And to use the laws, we need to provide a constructor function, that -# given a single argument will return a new data structure containing -# that argument. We also make sure that the constructor for our -# semigroup implementation lifts the value into a non empty list, so we -# can concatenate the values. -make = (a) -> new Async a -make-nel = (a) -> new Async [a] - -# Then we provide the specification for the test runner. As we're using -# Hifive here, it expects that each definition for the specification to -# have a function that will throw exceptions on failures. We use the -# `asTest` property from Claire that returns exactly what Hifive (and -# Mocha & other testing libraries) expect. -module.exports = spec 'Algebraic laws' (o, spec) -> - - spec ': Semigroup' (o) -> - o '1. Associativity' laws.semigroup.associativity(make-nel).as-test! - - spec ': Monoid' (o) -> - o '1. Right identity' laws.monoid.right-identity(make).as-test! - o '2. Left identity' laws.monoid.left-identity(make).as-test! - - spec ': Functor' (o) -> - o '1. Identity' laws.functor.identity(make).as-test! - o '2. Composition' laws.functor.composition(make).as-test! - - spec ': Applicative' (o) -> - o '1. Identity' laws.applicative.identity(make).as-test! - o '2. Composition' laws.applicative.composition(make).as-test! - o '3. Homomorphism' laws.applicative.homomorphism(make).as-test! - o '4. Interchange' laws.applicative.interchange(make).as-test! - - spec ': Chain' (o) -> - o '1. Associativity' laws.chain.associativity(make).as-test! - - spec ': Monad' (o) -> - o '1. Left identity' laws.monad.left-identity(make).as-test! - o '2. Right identity' laws.monad.right-identity(make).as-test! diff --git a/test/tap.ls b/test/tap.ls index 0786387..f02e781 100644 --- a/test/tap.ls +++ b/test/tap.ls @@ -1,7 +1,7 @@ # # A node test runner /** ^ - * Copyright (c) 2013 Quildreen Motta + * Copyright (c) 2013-2014 Quildreen Motta * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files