How to use the daggy.taggedSum function in daggy

To help you get started, we’ve selected a few daggy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fantasyland / fantasy-eithers / src / either.js View on Github external
'use strict';

const {tagged, taggedSum} = require('daggy');
const {compose, identity} = require('fantasy-combinators');
const {of, chain, concat, map, ap, sequence} = require('fantasy-land');

const Either = taggedSum({
    Left:  ['l'],
    Right: ['r']
});

// Methods
Either.prototype.fold = function(f, g) {
    return this.cata({
        Left: f,
        Right: g
    });
};
Either[of] = Either.Right;
Either.prototype.swap = function() {
    return this.fold(
        (l) => Either.Right(l),
        (r) => Either.Left(r)
github edahlseng / eff-javascript / sources / reader.js View on Github external
/* @flow */

import daggy from "daggy";

import { interpreter, send } from "./eff.js";

const Reader = daggy.taggedSum("Reader", {
	get: [],
});

export const get = () => send(Reader.get);

export const interpretReader = (i: any) =>
	interpreter({
		predicate: x => Reader.is(x),
		handler: readerEffect =>
			readerEffect.cata({
				get: () => continuation => continuation(i),
			}),
	});
github edahlseng / eff-javascript / sources / state.js View on Github external
/* @flow */

import daggy from "daggy";

import { interpreter, send } from "./eff.js";

const State = daggy.taggedSum("State", {
	get: [],
	modify: ["modificationFunction"],
	put: ["newState"],
});

export const get = () => send(State.get);
export const modify = (modificationFunction: any => any) =>
	send(State.modify(modificationFunction));
export const put = (newState: mixed) => send(State.put(newState));

export const interpretState = (startingState: mixed) => {
	let state = startingState;

	return interpreter({
		predicate: x => State.is(x),
		handler: stateEffect =>
github fantasyland / fantasy-frees / examples / interpreter-forth.js View on Github external
'use strict';

const daggy = require('daggy');

const {compose, constant, identity} = require('fantasy-combinators');
const {Free} = require('./../fantasy-frees');
const {Tuple2} = require('fantasy-tuples');
const {Lens}  = require('fantasy-lenses');
const State = require('fantasy-states');

const Forth = daggy.taggedSum({
    Push: ['a', 'next'],
    Add:  ['next'],
    Mul:  ['next'],
    Dup:  ['next'],
    End:  ['next']
});
const unit = daggy.tagged('x');
const Unit = () => unit('');

Forth.prototype.toString = function() {
    const named = (name) => (x) => 'Forth.' + name + '(' + x + ')';
    return this.cata({
        Push: (x, y) =>'Forth.Push(' + x + ', ' + y.toString() + ')',
        Add: named('Add'),
        Mul: named('Mul'),
        Dup: named('Dup'),
github fantasyland / fantasy-frees / examples / interpreter-coproduct.js View on Github external
'use strict';

const daggy = require('daggy');

const {compose, identity} = require('fantasy-combinators');
const {Free} = require('./../fantasy-frees');
const Coproduct = require('fantasy-coproducts');
const IO = require('fantasy-io');


const FPrint = daggy.tagged('s', 'a');
const FRead = daggy.tagged('f');
const unit = daggy.tagged('x');
const Unit = () => unit('');

const Logger = daggy.taggedSum({
    Error: ['x', 'a'],
    Debug: ['x', 'a']
});

FPrint.prototype.map = function(f) {
    return FPrint(this.s, f(this.a));
};

FRead.prototype.map = function(f) {
    return FRead(compose(f)(this.f));
};

Logger.prototype.map = function(f) {
    return this.cata({
        Error: (a, b) => Logger.Error(a, f(b)),
        Debug: (a, b) => Logger.Debug(a, f(b))
github fluture-js / fluture-express / index.js View on Github external
//.
//# Redirect :: (Number, String) -> Response a
//.
//. Indicates a redirection. The first argument will be the response status
//. code, and the second will be the value of the Location header.
//.
//# Empty :: Response a
//.
//. Indicates an empty response. The response status will be set to 204, and
//. no response body or Content-Type header will be sent.
//.
//# Next :: a -> Response a
//.
//. Indicates that this middleware does not form a response. The supplied value
//. will be assigned to `res.locals` and the next middleware will be called.
const Response = daggy.taggedSum ('Response', {
  Stream: ['code', 'mime', 'stream'],
  Json: ['code', 'value'],
  Redirect: ['code', 'url'],
  Empty: [],
  Next: ['locals'],
});

const runAction = (name, action, req, res, next) => {
  const ret = action (req, res.locals);

  if (!isFuture (ret)) {
    throw new TypeError (
      `The "${name}" action did not return a Future, instead saw:\n\n  ${ret}`
    );
  }
github tjercus / react-pattern-matching-daggy / src / types.js View on Github external
import daggy from "daggy";

export const Item = daggy.tagged("Item", ["title"]);

export const List = daggy.taggedSum("List", {
  Empty: [],
  Initial: [],
  Items: [Item],
  NotFound: ["searchMessage"],
  FetchError: []
});

export const LIST_ITEMS = [
  { title: "Butter" },
  { title: "Bread" },
  { title: "Eggs" },
  { title: "Fish" },
  { title: "Cake" }
];
github DrBoolean / freeky / either.js View on Github external
const daggy = require('daggy')
const liftF = require('./free').liftF

const Either = daggy.taggedSum({ Left_: ['x'], Right_: ['x'] })

const Left = x => liftF(Either.Left_(x))
const Right = x => liftF(Either.Right_(x))

Either.of = Right

Either.prototype.fold = function(f, g) {
  return this.cata({ Left_: f, Right_: g })
}

module.exports = {Either, Left, Right}
github DrBoolean / freeky / free.js View on Github external
const daggy = require('daggy')

const Free = daggy.taggedSum({Impure: ['x', 'f'], Pure: ['x']})

Free.of = Free.Pure

const kleisli_comp = (f, g) => x => f(x).chain(g)

Free.prototype.fold = function() {
  return this.x.fold.apply(this.x, arguments)
}


Free.prototype.map = function(f) {
  return this.cata({
    Impure: (x, g) => Free.Impure(x, y => g(y).map(f)),
    Pure: x => Free.Pure(f(x))
  })
}
github fantasyland / fantasy-frees / examples / interpreter-tweet.js View on Github external
'use strict';

const daggy = require('daggy');

const {identity} = require('fantasy-combinators');    
const {Free}    = require('./../fantasy-frees');
const {singleton}     = require('fantasy-helpers');
    
const Cofree   = require('fantasy-cofrees');
const Identity = require('fantasy-identities');
const Option   = require('fantasy-options');

const Tweet   = daggy.tagged('id', 'str');
const User    = daggy.tagged('id', 'name', 'photo');

const Service = daggy.taggedSum({
    GetTweets    : ['id'],
    GetUserName  : ['id'],
    GetUserPhoto : ['id']
});
    
const Request = daggy.taggedSum({
    Fetch : ['x'],
    Pure  : ['x']
});

function pure(x) {
    return Free.liftFC(Request.Pure(x));
}

function fetch(s) {
    return Free.liftFC(Request.Fetch(s));

daggy

Library for creating tagged constructors.

MIT
Latest version published 3 years ago

Package Health Score

54 / 100
Full package analysis

Popular daggy functions