How to use the ramda.curry function in ramda

To help you get started, we’ve selected a few ramda 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 ivan-kleshnin / cyclejs-examples / 2.1-routing / src / rx.utils.js View on Github external
let R = require("ramda")
let {curry, is, map, split} = require("ramda")
let {Observable} = require("rx")

// scanFn :: s -> (s -> s) -> s
let scanFn = curry((state, updateFn) => {
  if (!is(Function, updateFn) || updateFn.length != 1) {
    throw Error("updateFn must be a function with arity 1, got " + updateFn)
  } else {
    return updateFn(state)
  }
})

//  --> String -> Observable
let pluck = function (path) {
  let lens = R.lensPath(path.split("."))
  return this.map((v) => R.view(lens, v))
}

//  --> String, (a -> (b -> c)) -> Observable
let overState = function (path, fn) {
  let lens = R.lensPath(path.split("."))
github flunc / optics / src / Prism.js View on Github external
const
  R        = require('ramda'),
  RF       = require('ramda-fantasy'),
  Choice   = require('./Internal/Profunctor/Class/Choice'),
  PF       = require('./Internal/Profunctor/Class/Profunctor'),
  Market   = require('./Internal/Profunctor/Market'),
  Tagged   = require('./Internal/Profunctor/Tagged'),

  Either = RF.Either,
  Maybe  = RF.Maybe,
  Tuple  = RF.Tuple,
  Unit   = {};


// prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
const prism = R.curry((to, fro, pab) =>
  PF.dimap(fro, (e => e.value), Choice.right(PF.rmap(to, pab))));

// prism_ :: (a -> s) -> (s -> Maybe a) -> PrismP s a
const prism_ = R.curry((to, fro) =>
  prism(to, s => Maybe.maybe(Either.Left(s), Either.Right, fro(s))));

// review :: Review s t a b -> b -> t
const review = R.curry((p, r) => p(Tagged(r)).unTagged);

// nearly :: a -> (a -> Boolean) -> PrismP a Unit
const nearly = R.curry((x, f) =>
  prism_(() => x, a => f(a) ? Maybe.Just(Unit) : Maybe.Nothing()));

// only :: Eq a => a -> PrismP a Unit
const only = a =>
  prism_(() => a,
github funkia / list / src / ramda.ts View on Github external
// @ts-ignore
import { curry, CurriedFunction2 } from "ramda";
import * as L from "./index";

export { Node, List, list } from "./index";

export const prepend = curry(L.prepend);
export const isList = curry(L.isList);
export const append = curry(L.append);
export const of = curry(L.of);
export const pair = curry(L.pair);
export const empty = curry(L.empty);
export const repeat = curry(L.repeat);
export const times = curry(L.times);
export const length = curry(L.length);
export const first = curry(L.first);
export const head = first;
export const last = curry(L.last);
export const nth = curry(L.nth);
export const map = curry(L.map);
export const forEach = curry(L.forEach);
export const pluck = curry(L.pluck as (<a>(key: string, l: L.List</a><a>) =&gt; A));
export const range = curry(L.range);
export const foldl = curry(L.foldl);
export const reduce = foldl;
export const filter = curry(L.filter);
export const reject = curry(L.reject);
export const partition = curry(L.partition);
export const join = curry(L.join);</a>
github nickslevine / zebras / zebras.js View on Github external
/**
 * Returns a new series with the differences between the values in the order of
 * the input series.
 *
 * @func
 * @memberOf Z
 * @category Analysis
 * @param {Array} arr Series to calculate differences for
 * @return {Array}
 * @example
 *
 * const series = [7, 2, 30, 30, 56, 75]
 * Z.diff(series)
 * // [NaN, -5, 28, 0, 26, 19]
 */
const diff = R.curry(arr => {
  const iRange = R.range(0, arr.length)
  const result = R.map(i => {
    if (i === 0) return NaN
    return arr[i] - arr[i - 1]
  }, iRange)
  return result
})

/**
 * Calculate rolling statistics
 *
 * Calculate statistics over a moving window.
 * Works with Z.min, Z.max, Z.mean, Z.std, Z.sum, Z.prod, or any other function
 * that takes an array as a single argument.
 *
 * @func
github blockchain / My-Wallet-V3 / src / coin-selection.js View on Github external
const { curry, unfold, reduce, last, filter, head, map, isNil, isEmpty, tail, clamp, sort } = require('ramda');
const Coin = require('./coin.js');

const fold = curry((empty, xs) => reduce((acc, x) => acc.concat(x), empty, xs));
const foldCoins = fold(Coin.empty);

const dustThreshold = (feeRate) => (Coin.inputBytes({}) + Coin.outputBytes({})) * feeRate;

const transactionBytes = (inputs, outputs) =>
  Coin.TX_EMPTY_SIZE + inputs.reduce((a, c) => a + Coin.inputBytes(c), 0) + outputs.reduce((a, c) => a + Coin.outputBytes(c), 0);

const effectiveBalance = curry((feePerByte, inputs, outputs = [{}]) =>
  foldCoins(inputs).map(v =>
    clamp(0, Infinity, v - transactionBytes(inputs, outputs) * feePerByte))
);

// findTarget :: [Coin] -> Number -> [Coin] -> String -> Selection
const findTarget = (targets, feePerByte, coins, changeAddress) => {
  let target = foldCoins(targets).value;
  let _findTarget = seed => {
    let acc = seed[0];
    let newCoin = head(seed[2]);
    if (isNil(newCoin) || acc > target + seed[1]) { return false; }
    let partialFee = seed[1] + Coin.inputBytes(newCoin) * feePerByte;
    let restCoins = tail(seed[2]);
    let nextAcc = acc + newCoin.value;
    return acc > target + partialFee ? false : [[nextAcc, partialFee, newCoin], [nextAcc, partialFee, restCoins]];
  };
github Circle-gg / thunder-mail / routes / email.route / controller.js View on Github external
.map((email) =&gt; findOrCreateSubscriber({ email, orgId }))
        )
        .then(tagUnsubscribed(sendAction));
});

/**
 * Tag the subscribers that are unsubscribed.
 *
 * @private
 * @func
 * @since v0.0.0
 * @param {Object} sendAction - the send action object with all the details we need to send the emails
 * @param {Array} subscribers - list of subscribers we query from our database
 * @return {Promise} SendAction
 */
const tagUnsubscribed = R.curry(function tagUnsubscribed(sendAction, subscribers) {
    //we need to filter out subscribers that are opt out
    const unsubscribed = subscribers
        .filter((subscriber) =&gt; !subscriber.subscribed)
        .map((subscriber) =&gt; subscriber.email);

    const taggedDestinations = sendAction.destinations
        .map((destination) =&gt; ({
            ...destination,
            validated: !R.contains(unsubscribed, getRecipients(destination))
        }));

    return Promise.resolve({
        ...sendAction,
        destinations: taggedDestinations
    });
});
github funkia / list / src / ramda.ts View on Github external
export const head = first;
export const last = curry(L.last);
export const nth = curry(L.nth);
export const map = curry(L.map);
export const forEach = curry(L.forEach);
export const pluck = curry(L.pluck as (<a>(key: string, l: L.List</a><a>) =&gt; A));
export const range = curry(L.range);
export const foldl = curry(L.foldl);
export const reduce = foldl;
export const filter = curry(L.filter);
export const reject = curry(L.reject);
export const partition = curry(L.partition);
export const join = curry(L.join);
export const foldr = curry(L.foldr);
export const reduceRight = foldr;
export const ap = curry(L.ap);
export const chain = curry(L.chain);
export const flatten = curry(L.flatten);
export const every = curry(L.every);
export const all = every;
export const some = curry(L.some);
export const any = some;
export const none = curry(L.none);
export const find = curry(L.find);
export const indexOf = curry(L.indexOf);
export const findIndex = curry(L.findIndex);
export const includes = curry(L.includes);
export const contains = includes;
export const equals = curry(L.equals);
export const concat = curry(L.concat);
export const update = curry(L.update);
export const adjust = curry(L.adjust);</a>
github SeanCannon / prettycats / lib / predicates / _private.js View on Github external
'use strict';

const R = require('ramda');

const ofLength = R.curry((proto, comparitor, length, x) => {
  return R.allPass([R.is(proto), R.compose(R[comparitor](R.__, length), R.length)])(x);
});

const modTwoEq = v => R.compose(R.equals(v), R.mathMod(R.__, 2));

module.exports = {
  ofLength,
  modTwoEq
};
github winkerVSbecks / tachyons-measured / src / style-helper.js View on Github external
import R from 'ramda';
import { mqObjToSelectors } from './media-queries';
import { isPresent } from './utils';

const hasMediaQuery = R.compose(R.is(Object), R.nth(1));

const selectorFor = R.curry((prop, val) => `${prop}${val}`);

const selectorWithMQFor = R.ifElse(hasMediaQuery,
  R.apply(mqObjToSelectors),
  R.apply(selectorFor),
);

/**
 * Get multiple tachyon classes
 * using an object where key is
 * selector type and value is value
 */
export const classesFor = R.compose(
  R.map(selectorWithMQFor),
  R.toPairs,
  R.filter(isPresent),
);
github ai-labs-team / casium / src / dispatcher.ts View on Github external
import { always, cond, curry, flip, is, pipe, prop, T } from 'ramda';
import Message, { MessageConstructor } from './message';
import ExecContext from './runtime/exec_context';
import { EffectType, ProcessState } from './subscription';
import { safeStringify } from './util';

export type EffectMap = Map any&gt;;

const unbox = cond([
  [is(Message), pipe(prop('data'), Array.of)],
  [is(ProcessState), Array.of],
  [T, always(null)]
]);

export const handler = curry((effects: EffectMap, msg: Message) =&gt; {
  const key = msg &amp;&amp; msg[EffectType] || (msg.constructor as MessageConstructor);
  return effects.get(key) &amp;&amp; key || Array.from(effects.keys()).find(flip(is)(msg));
});

/**
 * Dispatches command messages.
 *
 * @param {Map} A map pairing command message constructors to effects handlers
 * @param {Function} dispatch A container-bound dispatch function for sending
 *        effect results (where applicable) back to containers
 * @param {Message} msg A command message to dispatch
 * @return {*} returns the result of calling the effect handler
 */
export default curry((effects: EffectMap, execContext: ExecContext, msg: Message) =&gt; {
  const ctor = msg &amp;&amp; msg.constructor, data = unbox(msg), callback = effects.get(handler(effects, msg));