This commit is contained in:
Anthony Fu 2020-07-29 14:04:44 +08:00
parent e3fd163169
commit 06f790ca50
10 changed files with 59 additions and 7 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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]

View File

@ -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]

View File

@ -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]

View 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-->

View 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

View File

@ -0,0 +1 @@
title: 科里化 1

View File

@ -0,0 +1 @@
declare function Currying(fn: any): any

View 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
>>,
]