mirror of
https://github.com/type-challenges/type-challenges.git
synced 2025-12-08 19:06:13 +00:00
feat: 17
This commit is contained in:
parent
e3fd163169
commit
06f790ca50
File diff suppressed because one or more lines are too long
@ -5,7 +5,7 @@ Implement a generic `Head<T>` that takes an Array `T` and returns it's first ele
|
||||
|
||||
For example
|
||||
|
||||
```
|
||||
```ts
|
||||
type arr1 = ['a', 'b', 'c']
|
||||
type arr2 = [3, 2, 1]
|
||||
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
<!--info-header-start--><h1>Tail of Array <img src="https://img.shields.io/badge/-medium-eaa648" alt="medium"/> <img src="https://img.shields.io/badge/-%23array-999" alt="#array"/> <img src="https://img.shields.io/badge/-%234.0-999" alt="#4.0"/></h1><blockquote><p>by Anthony Fu <a href="https://github.com/antfu" target="_blank">@antfu</a></p></blockquote><p><a href="https://type-challenges.netlify.app/15/play" target="_blank"><img src="https://img.shields.io/badge/-Take%20the%20Challenge-3178c6?logo=typescript" alt="Take the Challenge"/></a> </p><!--info-header-end-->
|
||||
|
||||
> TypeScript 4.0 is Recommended in this challenge
|
||||
> TypeScript 4.0 is recommended in this challenge
|
||||
|
||||
Implement a generic `Tail<T>` that takes an Array `T` and returns it's last element's type.
|
||||
|
||||
For example
|
||||
|
||||
```
|
||||
```ts
|
||||
type arr1 = ['a', 'b', 'c']
|
||||
type arr2 = [3, 2, 1]
|
||||
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
<!--info-header-start--><h1>Pop <img src="https://img.shields.io/badge/-medium-eaa648" alt="medium"/> <img src="https://img.shields.io/badge/-%23array-999" alt="#array"/> <img src="https://img.shields.io/badge/-%234.0-999" alt="#4.0"/></h1><blockquote><p>by Anthony Fu <a href="https://github.com/antfu" target="_blank">@antfu</a></p></blockquote><p><a href="https://type-challenges.netlify.app/16/play" target="_blank"><img src="https://img.shields.io/badge/-Take%20the%20Challenge-3178c6?logo=typescript" alt="Take the Challenge"/></a> </p><!--info-header-end-->
|
||||
|
||||
> TypeScript 4.0 is Recommended in this challenge
|
||||
> TypeScript 4.0 is recommended in this challenge
|
||||
|
||||
Implement a generic `Pop<T>` that takes an Array `T` and returns an Array without it's last element.
|
||||
|
||||
For example
|
||||
|
||||
```
|
||||
```ts
|
||||
type arr1 = ['a', 'b', 'c', 'd']
|
||||
type arr2 = [3, 2, 1]
|
||||
|
||||
|
||||
24
questions/17-hard-currying-1/README.md
Normal file
24
questions/17-hard-currying-1/README.md
Normal file
@ -0,0 +1,24 @@
|
||||
<!--info-header-start--><h1>Currying 1 <img src="https://img.shields.io/badge/-hard-red" alt="hard"/> <img src="https://img.shields.io/badge/-%23array-999" alt="#array"/> <img src="https://img.shields.io/badge/-%234.0-999" alt="#4.0"/></h1><blockquote><p>by Anthony Fu <a href="https://github.com/antfu" target="_blank">@antfu</a></p></blockquote><p><a href="https://type-challenges.netlify.app/17/play" target="_blank"><img src="https://img.shields.io/badge/-Take%20the%20Challenge-3178c6?logo=typescript" alt="Take the Challenge"/></a> </p><!--info-header-end-->
|
||||
|
||||
> TypeScript 4.0 is recommended in this challenge
|
||||
|
||||
[Currying](https://en.wikipedia.org/wiki/Currying) is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument.
|
||||
|
||||
For example:
|
||||
|
||||
```ts
|
||||
const add = (a: number, b: number) => a + b
|
||||
const three = add(1, 2)
|
||||
|
||||
const curriedAdd = Currying(add)
|
||||
const five = curriedAdd(2)(3)
|
||||
```
|
||||
|
||||
The function passed to `Currying` may have multiple arguments, you need to correctly type it.
|
||||
|
||||
In this challenge, the curried function only accept one argument at a time. Once all the argument is assigned, it should return its result.
|
||||
|
||||
|
||||
**Extra**: Similarly, can you implement `Shift`, `Push` and `Unshift` as well?
|
||||
|
||||
<!--info-footer-start--><br><a href="../../README.md" target="_blank"><img src="https://img.shields.io/badge/-Back-grey" alt="Back"/></a> <a href="https://type-challenges.netlify.app/17/solutions" target="_blank"><img src="https://img.shields.io/badge/-Check%20out%20Solutions-de5a77?logo=awesome-lists&logoColor=white" alt="Check out Solutions"/></a> <a href="https://type-challenges.netlify.app/17/answer" target="_blank"><img src="https://img.shields.io/badge/-Share%20your%20Solutions-green" alt="Share your Solutions"/></a> <!--info-footer-end-->
|
||||
13
questions/17-hard-currying-1/info.yml
Normal file
13
questions/17-hard-currying-1/info.yml
Normal file
@ -0,0 +1,13 @@
|
||||
title: Currying 1
|
||||
|
||||
author:
|
||||
name: Anthony Fu
|
||||
email: hi@antfu.me
|
||||
github: antfu
|
||||
|
||||
tags: array, 4.0
|
||||
|
||||
tsconfig:
|
||||
ts: 4.0.0-beta
|
||||
|
||||
related: 14, 16
|
||||
1
questions/17-hard-currying-1/info.zh-CN.yml
Normal file
1
questions/17-hard-currying-1/info.zh-CN.yml
Normal file
@ -0,0 +1 @@
|
||||
title: 科里化 1
|
||||
1
questions/17-hard-currying-1/template.ts
Normal file
1
questions/17-hard-currying-1/template.ts
Normal file
@ -0,0 +1 @@
|
||||
declare function Currying(fn: any): any
|
||||
13
questions/17-hard-currying-1/test-cases.ts
Normal file
13
questions/17-hard-currying-1/test-cases.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Equal, Expect } from '@type-challenges/utils'
|
||||
|
||||
const curried1 = Currying((a: string, b: number, c: boolean) => true)
|
||||
const curried2 = Currying((a: string, b: number, c: boolean, d: boolean, e: boolean, f: string, g: boolean) => true)
|
||||
|
||||
type cases = [
|
||||
Expect<Equal<
|
||||
typeof curried1, (a: string) => (b: number) => (c: boolean) => boolean
|
||||
>>,
|
||||
Expect<Equal<
|
||||
typeof curried2, (a: string) => (b: number) => (c: boolean) => (d: boolean) => (e: boolean) => (f: string) => (g: boolean) => boolean
|
||||
>>,
|
||||
]
|
||||
Loading…
x
Reference in New Issue
Block a user