conductor
Why I'm building conductorGitHub
v1.5.0
v1.5.0
  • Introduction
  • Overview
    • Introduction
    • Core concepts
  • API reference
    • always
    • append
    • apply
    • arity
    • branch
    • capitalize
    • compose
    • concat
    • curry
    • curryN
    • delay
    • dump
    • entries
    • equals
    • equalsBy
    • factory
    • filter
    • findIndex
    • flatten
    • flip
    • forEach
    • get
    • head
    • ifElse
    • identity
    • into
    • isPromise
    • iterate
    • join
    • keys
    • map
    • merge
    • mergeBy
    • next
    • not
    • pluck
    • prepend
    • random
    • reduce
    • replace
    • slice
    • some
    • split
    • take
    • then
    • toLowerCase
    • transduce
    • transformers
      • transformers/filter
      • transformers/map
    • type
    • upsert
    • values
  • Guides
    • example use cases
    • checkGuards
Powered by GitBook
On this page
  • description
  • examples
  • basic example
  • arity preservation
  • deep currying
  • passing more than one argument
  1. API reference

curry

PreviousconcatNextcurryN

Last updated 7 years ago

curry :: Function f => Function g

description

Returns a of the provided function. Currying is a very important concept in functional programming: it enables , which in turn allows you to reuse these partially-applied functions and to avoid code repetition. curry is a pure function and will return a new function, without modifying the original function. The new function will have the same arity as the original one did.

curry does a little more than classic currying: the returned function can actually accept more than one argument. It can accept from one up to as many as the original function did.

examples

basic example

import { curry } from 'conductor'

const multiply = (x, y) => x * y
const times2 = curry(multiply)(2)

times2(5) // 10

arity preservation

import { curry } from 'conductor'

const multiply = (x, y) => x * y
multiply.length // 2
curry(multiply).length // 2

The resulting function has the same arity as the original.

deep currying

import { curry } from 'conductor'

const add = (a, b, c, d) => a + b + c + d
const addCurried = curry(add)
addCurried(1) // Function
addCurried(1)(2) // Function
addCurried(1)(2)(3) // Function
addCurried(1)(2)(3)(4) // 10

passing more than one argument

import { curry } from 'conductor'

const add = (a, b, c) => a + b + c
const add3 = curry(add)(1, 2)
add3(3) // 6
curried version
partial application