Skip to content
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

17 - Currying 1 #15

Open
antfu opened this issue Jul 29, 2020 · 4 comments
Open

17 - Currying 1 #15

antfu opened this issue Jul 29, 2020 · 4 comments
Labels
17 answer Share answers/solutions to a question

Comments

@antfu
Copy link
Member

antfu commented Jul 29, 2020

✍️ Answers

Name your title starting with the question no you are trying to answer.

For example:
12 - my one-line solution

type Unshift<T> = T extends [infer K, ...infer U] ? U : unknown
type Head<T> = T extends [infer K, ...infer U] ? K : unknown

type Curried<T, R> = T extends Array<any>
  ? T['length'] extends 1
    ? (args: Head<T>) => R
    : (args: Head<T>) => Curried<Unshift<T>, R>
  : never

declare function Currying<T extends unknown[], R>(fn: (...args: T) => R): Curried<T, R>
@antfu antfu added answer Share answers/solutions to a question 17 labels Jul 29, 2020
@suddenlyGiovanni
Copy link

A VariadicCurrying would also be a crazy good challenge, plus would also set up a good example on how to approach such a challenge after the introduction of Variadic Tuple types in TS >= 4.0

@millsp
Copy link

millsp commented Nov 23, 2020

This implementation is variadic, what did you mean @suddenlyGiovanni? Did you mean that it can take rest args?

@Luncher
Copy link

Luncher commented Mar 7, 2021

This implementation can't pass the test case

@action-hong
Copy link

action-hong commented Aug 3, 2021

can not pass the test case

// const curried1: (args: string) => (args: number) => (args: boolean) => boolean
const curried1 = Currying((a: string, b: number, c: boolean) => true)
//const curried2: (args: string) => (args: number) => (args: boolean) => (args: boolean) => (args: boolean) => (args: string) => (args: boolean) => boolean
const curried2 = Currying((a: string, b: number, c: boolean, d: boolean, e: boolean, f: string, g: boolean) => true)

all return boolean instead of true, how to fix it ?

pietrushka added a commit to pietrushka/type-challenges that referenced this issue Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
17 answer Share answers/solutions to a question
Projects
None yet
Development

No branches or pull requests

5 participants