Permalink
Browse files

hmm...

  • Loading branch information...
0 parents commit 3da73166214b9c82dfebe33b3042ce532e0fd41d @phadej committed Sep 20, 2012
Showing with 238 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +115 −0 Promises.hs
  3. +122 −0 promises.js
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,115 @@
+-- Example 1
+
+import Control.Applicative
+import Data.Traversable (sequenceA)
+import Data.Char (toUpper)
+import Control.Exception (try, IOException)
+
+-- Helpers
+wrap = Right
+end = either (const $ error "unexpected error") id
+
+-- Example 1
+example1 = print $ end $ do
+ v <- wrap 1
+ if v == 1
+ then return 2
+ else return 3
+
+example1applicative = print $ end $ f `fmap` wrap 1
+ where f 1 = 2
+ f _ = 3
+
+-- Example 2
+example2 = print $ end $ do
+ x <- wrap 4
+ y <- return $ process1 x
+ z <- return $ process2 y
+ return z
+ where process1 x = x + 2
+ process2 x = x * 3
+
+example2applicative = print $ end $ process2 <$> process1 <$> wrap 4
+ where process1 x = x + 2
+ process2 x = x * 3
+
+-- Example 3
+example3 = print $ defaultOnError 5 $ do
+ x <- wrap 3
+ y <- process x
+ return y
+ where process :: b -> Either String c
+ process _ = Left "error"
+ defaultOnError d = either (const d) id
+
+example3applicative = print $ defaultOnError 5 $ process $ wrap 3
+ where process :: Either a b -> Either String c
+ process _ = Left "error"
+ defaultOnError d = either (const d) id
+
+-- Example 4
+example4 = print $ end $ do
+ x <- wrap 1
+ y <- wrap 2
+ return $ x + y
+
+example4applicative = print $ end $ (+) <$> wrap 1 <*> wrap 2
+
+-- Example 5
+example5 = print $ end $ do
+ x <- wrap 1
+ y <- wrap 2
+ z <- wrap 3
+ return $ foldr (+) 4 [x, y, z]
+
+example5b = print $ end $ let
+ mx = wrap 1
+ my = wrap 2
+ mz = wrap 3
+ in do
+ xs <- sequence [mx, my, mz]
+ return $ foldr (+) 4 xs
+
+{-
+-- sequenceA for the lists
+
+f :: Applicative f => [f a] -> f [a]
+f [] = pure []
+f (x:xs) = (:) <$> x <*> f xs
+-}
+
+example5applicative = print $ end $ foldr (+) 4 <$> sequenceA [mx, my, mz]
+ where mx = wrap 1
+ my = wrap 2
+ mz = wrap 3
+
+-- Example 6
+example6 = do
+ d <- readFile "Promises.hs"
+ putStrLn $ f d
+ where f = map toUpper . take 100
+
+t :: IO a -> IO (Either IOException a)
+t = try
+
+example6b = do
+ d <- t $ readFile "Promises.hs"
+ putStrLn $ end $ f <$> d
+ where f = map toUpper . take 100
+
+ -- Running examples
+main :: IO ()
+main = do
+ example1
+ example1applicative
+ example2
+ example2applicative
+ example3
+ example3applicative
+ example4
+ example4applicative
+ example5
+ example5b
+ example5applicative
+ example6
+ example6b
@@ -0,0 +1,122 @@
+/* npm install q */
+"use strict";
+
+(function () {
+ var Q = require("Q");
+ var FS = require("fs");
+ var output = function(tag) {
+ return function(s) {
+ console.log(tag, s);
+ };
+ };
+
+ // Helpers
+ var wrap = function (v) {
+ return Q.fcall(function () {
+ return v;
+ });
+ };
+
+ var readFile = function (filename) {
+ var deferred = Q.defer();
+ FS.readFile(filename, "utf-8", function (error, text) {
+ if (error) {
+ deferred.reject(new Error(error));
+ } else {
+ deferred.resolve(text);
+ }
+ });
+ return deferred.promise;
+ };
+
+ // Examples 1
+ function example1() {
+ wrap(1)
+ .then(function(value) {
+ if (value == 1) {
+ return 2;
+ } else {
+ return 3;
+ }
+ })
+ .then(output("example 1:"))
+ .end();
+ }
+
+ // Example 2
+ function example2() {
+ var process1 = function (x) { return x + 2; };
+ var process2 = function (x) { return x * 3; };
+
+ wrap(4)
+ .then(process1)
+ .then(process2)
+ .then(output("example 2:"))
+ .end();
+ }
+
+ // Example 3
+ function example3() {
+ var process = function (x) { throw "error"; };
+
+ wrap(3)
+ .then(process)
+ .then(function (value) {
+ return value;
+ }, function (error) {
+ return 5;
+ })
+ .then(output("example 3:"))
+ .end();
+ }
+
+ function example4() {
+ var promise1 = wrap(1);
+ var promise2 = wrap(2);
+
+ Q.all([promise1, promise2])
+ .then(function(xy) {
+ return xy[0] + xy[1];
+ })
+ .then(output("example 4:"))
+ .end();
+ }
+
+ function example5() {
+ var promise1 = wrap(1);
+ var promise2 = wrap(2);
+ var promise3 = wrap(3);
+
+ Q.all([promise1, promise2, promise3])
+ .then(function(xs) {
+ return xs.reduce(function(x, y) { return x + y }, 4);
+ })
+ .then(output("example 5:"))
+ .end();
+ }
+
+ function example6() {
+ readFile("promises.js")
+ .then(function(value) {
+ return value.slice(0, 100).toUpperCase();
+ })
+ .then(output("example 6:\n"))
+ .end();
+ }
+
+ function example7() {
+ var promise = wrap(15);
+
+ promise.then(output("example 7:"));
+ promise.then(output("example 7:"));
+ }
+
+ // Running examples
+ example1();
+ example2();
+ example3();
+ example4();
+ example5();
+ example6();
+ example7();
+}());

0 comments on commit 3da7316

Please sign in to comment.