Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Implements other functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
robotlolita committed Jan 17, 2014
1 parent b98e856 commit 733972e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 86 deletions.
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
69 changes: 62 additions & 7 deletions src/index.ls
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
3 changes: 1 addition & 2 deletions test/specs/index.ls
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,5 +26,4 @@
module.exports = [
# The specification objects go here
# See: http://hifivejs.github.io/hifive/getting-started.html
require './monad-laws'
]
70 changes: 0 additions & 70 deletions test/specs/monad-laws.ls

This file was deleted.

2 changes: 1 addition & 1 deletion test/tap.ls
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 733972e

Please sign in to comment.