ES6. Using yield
and yield*
.
function* a() {
var i = yield "first thing from a";
var j = yield "second thing from a";
return [i, j];
}
function* b() {
// Run with `node --harmony coro.js` | |
"use strict"; | |
const Promise = require('bluebird'); | |
// Takes a function*, `g`, that yields promises. | |
// Returns a promise that resolves to `g`'s eventual return value. | |
var coro = function(gStar) { | |
const pending = Promise.pending(); |
module Phantom ( | |
ei, eb, plus, iff, -- Smart constructors | |
eval -- Evaluation | |
) where | |
data Expr a = EI Int | |
| EB Bool | |
| Plus (Expr Int) (Expr Int) | |
| If (Expr Bool) (Expr a) (Expr a) | |
deriving Show |
{-# LANGUAGE GADTs #-} | |
module Gadt where | |
import Data.Monoid | |
data Expr a where | |
EI :: Int -> Expr Int | |
EB :: Bool -> Expr Bool | |
Plus :: Expr Int -> Expr Int -> Expr Int |
ES6. Using yield
and yield*
.
function* a() {
var i = yield "first thing from a";
var j = yield "second thing from a";
return [i, j];
}
function* b() {
module Trie where | |
import Prelude hiding (lookup) | |
import Data.Maybe | |
import qualified Data.Map as M | |
data Trie = Trie (M.Map Char Trie) Bool | |
empty :: Trie | |
empty = Trie M.empty False |
module FreeGenerators where | |
import Control.Monad.Trans | |
import Control.Monad.Trans.Free | |
data YieldF i o r = YieldF o (i -> r) | |
instance Functor (YieldF i o) where | |
fmap f (YieldF o k) = YieldF o (f . k) |
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-} | |
{-# LANGUAGE NoMonomorphismRestriction #-} | |
module Generators where | |
import Prelude hiding (take, filter) | |
import Control.Applicative | |
import Control.Monad | |
import Control.Monad.Trans |
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-} | |
{-# LANGUAGE Rank2Types #-} | |
module Tuyau where | |
import Control.Applicative | |
import Control.Monad | |
import Control.Monad.Trans | |
import System.IO (isEOF) |
{-# LANGUAGE DeriveFunctor #-} | |
module Family where | |
import Control.Applicative | |
import Data.Monoid | |
-- Functors | |
-- | |
-- Lift a function (a -> b) to (f a -> f b) |
{-# LANGUAGE ExistentialQuantification #-} | |
import Control.Applicative | |
import Data.Functor.Identity | |
data Ap f a = Pure a | |
| forall x y. LiftA2 (x -> y -> a) (f x) (Ap f y) | |
instance Functor f => Functor (Ap f) where | |
fmap f (Pure a) = Pure (f a) |