From 06f790ca50887dbac37315dbbd05ad43404f7aa4 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 29 Jul 2020 14:04:44 +0800 Subject: [PATCH] feat: 17 --- README.md | 2 +- README.zh-CN.md | 2 +- questions/14-easy-head/README.md | 2 +- questions/15-medium-tail/README.md | 4 ++-- questions/16-medium-pop/README.md | 4 ++-- questions/17-hard-currying-1/README.md | 24 +++++++++++++++++++++ questions/17-hard-currying-1/info.yml | 13 +++++++++++ questions/17-hard-currying-1/info.zh-CN.yml | 1 + questions/17-hard-currying-1/template.ts | 1 + questions/17-hard-currying-1/test-cases.ts | 13 +++++++++++ 10 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 questions/17-hard-currying-1/README.md create mode 100644 questions/17-hard-currying-1/info.yml create mode 100644 questions/17-hard-currying-1/info.zh-CN.yml create mode 100644 questions/17-hard-currying-1/template.ts create mode 100644 questions/17-hard-currying-1/test-cases.ts diff --git a/README.md b/README.md index 835f88da..0d6fd27d 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ English | 简体中文 > Click the following badges to see detail of the challenges! -
13・Hello World


4・Pick<T, K> 7・Readonly<T> 14・Head of Array


2・Get Return Type 3・Omit<T, K> 8・Readonly 2 9・Deep Readonly 10・Tuple to Union 12・Chainable Options 15・Tail of Array 16・Pop


6・Simple Vue


5・Get Readonly Keys
By Tags
#4.014・Head of Array 15・Tail of Array 16・Pop
#application12・Chainable Options 6・Simple Vue
#array14・Head of Array 15・Tail of Array 16・Pop
#built-in4・Pick<T, K> 7・Readonly<T> 2・Get Return Type 3・Omit<T, K>
#deep9・Deep Readonly
#infer2・Get Return Type 10・Tuple to Union
#object-keys7・Readonly<T> 8・Readonly 2 9・Deep Readonly 5・Get Readonly Keys
#readonly7・Readonly<T> 8・Readonly 2 9・Deep Readonly
#this6・Simple Vue
#tuple10・Tuple to Union
#union4・Pick<T, K> 3・Omit<T, K> 10・Tuple to Union
#utils5・Get Readonly Keys
+
13・Hello World


4・Pick<T, K> 7・Readonly<T> 14・Head of Array


2・Get Return Type 3・Omit<T, K> 8・Readonly 2 9・Deep Readonly 10・Tuple to Union 12・Chainable Options 15・Tail of Array 16・Pop


6・Simple Vue 17・Currying 1


5・Get Readonly Keys
By Tags
#4.014・Head of Array 15・Tail of Array 16・Pop 17・Currying 1
#application12・Chainable Options 6・Simple Vue
#array14・Head of Array 15・Tail of Array 16・Pop 17・Currying 1
#built-in4・Pick<T, K> 7・Readonly<T> 2・Get Return Type 3・Omit<T, K>
#deep9・Deep Readonly
#infer2・Get Return Type 10・Tuple to Union
#object-keys7・Readonly<T> 8・Readonly 2 9・Deep Readonly 5・Get Readonly Keys
#readonly7・Readonly<T> 8・Readonly 2 9・Deep Readonly
#this6・Simple Vue
#tuple10・Tuple to Union
#union4・Pick<T, K> 3・Omit<T, K> 10・Tuple to Union
#utils5・Get Readonly Keys
## Recommended Readings diff --git a/README.zh-CN.md b/README.zh-CN.md index e21f2989..6678292e 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -28,7 +28,7 @@ TypeScript 类型体操姿势合集 > 点击下方徽章查看题目内容 -
13・Hello World


4・实现 Pick<T, K> 7・实现 Readonly<T> 14・获取数组头部


2・获取函数返回类型 3・实现 Omit<T, K> 8・Readonly 2 9・深度 Readonly 10・元组转合集 12・可串联构造器 15・获取数组尾部 16・出堆


6・简单的 Vue 类型


5・获取只读字段
By Tags
#4.014・获取数组头部 15・获取数组尾部 16・出堆
#application12・可串联构造器 6・简单的 Vue 类型
#array14・获取数组头部 15・获取数组尾部 16・出堆
#built-in4・实现 Pick<T, K> 7・实现 Readonly<T> 2・获取函数返回类型 3・实现 Omit<T, K>
#deep9・深度 Readonly
#infer2・获取函数返回类型 10・元组转合集
#object-keys7・实现 Readonly<T> 8・Readonly 2 9・深度 Readonly 5・获取只读字段
#readonly7・实现 Readonly<T> 8・Readonly 2 9・深度 Readonly
#this6・简单的 Vue 类型
#tuple10・元组转合集
#union4・实现 Pick<T, K> 3・实现 Omit<T, K> 10・元组转合集
#utils5・获取只读字段
+
13・Hello World


4・实现 Pick<T, K> 7・实现 Readonly<T> 14・获取数组头部


2・获取函数返回类型 3・实现 Omit<T, K> 8・Readonly 2 9・深度 Readonly 10・元组转合集 12・可串联构造器 15・获取数组尾部 16・出堆


6・简单的 Vue 类型 17・科里化 1


5・获取只读字段
By Tags
#4.014・获取数组头部 15・获取数组尾部 16・出堆 17・科里化 1
#application12・可串联构造器 6・简单的 Vue 类型
#array14・获取数组头部 15・获取数组尾部 16・出堆 17・科里化 1
#built-in4・实现 Pick<T, K> 7・实现 Readonly<T> 2・获取函数返回类型 3・实现 Omit<T, K>
#deep9・深度 Readonly
#infer2・获取函数返回类型 10・元组转合集
#object-keys7・实现 Readonly<T> 8・Readonly 2 9・深度 Readonly 5・获取只读字段
#readonly7・实现 Readonly<T> 8・Readonly 2 9・深度 Readonly
#this6・简单的 Vue 类型
#tuple10・元组转合集
#union4・实现 Pick<T, K> 3・实现 Omit<T, K> 10・元组转合集
#utils5・获取只读字段
## 推荐读物 diff --git a/questions/14-easy-head/README.md b/questions/14-easy-head/README.md index 3167b724..be8b68b4 100644 --- a/questions/14-easy-head/README.md +++ b/questions/14-easy-head/README.md @@ -5,7 +5,7 @@ Implement a generic `Head` that takes an Array `T` and returns it's first ele For example -``` +```ts type arr1 = ['a', 'b', 'c'] type arr2 = [3, 2, 1] diff --git a/questions/15-medium-tail/README.md b/questions/15-medium-tail/README.md index b05cff5a..7097b56a 100644 --- a/questions/15-medium-tail/README.md +++ b/questions/15-medium-tail/README.md @@ -1,12 +1,12 @@

Tail of Array medium #array #4.0

by Anthony Fu @antfu

Take the Challenge

-> TypeScript 4.0 is Recommended in this challenge +> TypeScript 4.0 is recommended in this challenge Implement a generic `Tail` 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] diff --git a/questions/16-medium-pop/README.md b/questions/16-medium-pop/README.md index c574d2c2..df0e074d 100644 --- a/questions/16-medium-pop/README.md +++ b/questions/16-medium-pop/README.md @@ -1,12 +1,12 @@

Pop medium #array #4.0

by Anthony Fu @antfu

Take the Challenge

-> TypeScript 4.0 is Recommended in this challenge +> TypeScript 4.0 is recommended in this challenge Implement a generic `Pop` 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] diff --git a/questions/17-hard-currying-1/README.md b/questions/17-hard-currying-1/README.md new file mode 100644 index 00000000..15248c5d --- /dev/null +++ b/questions/17-hard-currying-1/README.md @@ -0,0 +1,24 @@ +

Currying 1 hard #array #4.0

by Anthony Fu @antfu

Take the Challenge

+ +> 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? + +
Back Check out Solutions Share your Solutions \ No newline at end of file diff --git a/questions/17-hard-currying-1/info.yml b/questions/17-hard-currying-1/info.yml new file mode 100644 index 00000000..b029690a --- /dev/null +++ b/questions/17-hard-currying-1/info.yml @@ -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 \ No newline at end of file diff --git a/questions/17-hard-currying-1/info.zh-CN.yml b/questions/17-hard-currying-1/info.zh-CN.yml new file mode 100644 index 00000000..40e96e1f --- /dev/null +++ b/questions/17-hard-currying-1/info.zh-CN.yml @@ -0,0 +1 @@ +title: 科里化 1 \ No newline at end of file diff --git a/questions/17-hard-currying-1/template.ts b/questions/17-hard-currying-1/template.ts new file mode 100644 index 00000000..be44579b --- /dev/null +++ b/questions/17-hard-currying-1/template.ts @@ -0,0 +1 @@ +declare function Currying(fn: any): any diff --git a/questions/17-hard-currying-1/test-cases.ts b/questions/17-hard-currying-1/test-cases.ts new file mode 100644 index 00000000..89f5abb5 --- /dev/null +++ b/questions/17-hard-currying-1/test-cases.ts @@ -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 (b: number) => (c: boolean) => boolean + >>, + Expect (b: number) => (c: boolean) => (d: boolean) => (e: boolean) => (f: string) => (g: boolean) => boolean + >>, +]