Skip to content

Support multiple values in Function.apply #4705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: next-minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-adults-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Support multiple values in Function.apply
25 changes: 24 additions & 1 deletion packages/effect/dtslint/Function.tst.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flow, identity, Option, pipe } from "effect"
import { flow, Function, identity, Option, pipe } from "effect"
import { describe, it } from "tstyche"

describe("Function", () => {
Expand Down Expand Up @@ -27,4 +27,27 @@ describe("Function", () => {
)
})
})

it("apply", () => {
const apply1 = Function.apply("a")
const apply2 = Function.apply("a", 1)

const countArgs = (...args: Array<unknown>) => args.length
const arg1 = (a: string) => a
const arg2 = (a: string, b: number) => `${a}${b}`
const arg3 = (a: number) => a

const _a1: number = apply1(countArgs)
const _a2: string = apply1(arg1)
// @ts-expect-error: Target signature provides too few arguments. Expected 2 or more, but got 1.
apply1(arg2)
// @ts-expect-error: Type 'string' is not assignable to type 'number'.
apply1(arg3)

const _b1: number = apply2(countArgs)
const _b2: string = apply2(arg1)
const _b3: string = apply2(arg2)
// @ts-expect-error: Type 'string' is not assignable to type 'number'.
apply1(arg3)
})
})
4 changes: 2 additions & 2 deletions packages/effect/src/Function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const dual: {
}
}
/**
* Apply a function to a given value.
* Apply a function to given values.
*
* @example
* ```ts
Expand All @@ -184,7 +184,7 @@ export const dual: {
*
* @since 2.0.0
*/
export const apply = <A>(a: A) => <B>(self: (a: A) => B): B => self(a)
export const apply = <A extends ReadonlyArray<unknown>>(...a: A) => <B>(self: (...a: A) => B): B => self(...a)

/**
* A lazy argument.
Expand Down
5 changes: 4 additions & 1 deletion packages/effect/test/Function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const g = (n: number) => n * 2

describe("Function", () => {
it("apply", () => {
deepStrictEqual(Function.pipe(String.length, Function.apply("a")), 1)
const countArgs = (...args: Array<unknown>) => args.length

deepStrictEqual(Function.pipe(countArgs, Function.apply("a")), 1)
deepStrictEqual(Function.pipe(countArgs, Function.apply("a", "b", "c")), 3)
})

it("compose", () => {
Expand Down