fp-tsの一覧ページがあまりにも重くなったので、分割
参照元
一覧
よく参照されているもの一覧
よく参照されているものは最初に理解したほうがよい?
ファイル名 | import先 | ソースの中身 | declare module がある | importでの参照される数 |
---|---|---|---|---|
HKT | なし | interface, type のみ | 全て | 39 |
function |
|
interface, function, const のみ | なし | 35 |
HKTの内容確認 (Functorも含む)
参考
import { describe, it } from 'vitest'
import { type Functor1 } from 'fp-ts/lib/Functor'
import type { Kind, URIS } from 'fp-ts/lib/HKT'
import { pipe } from 'fp-ts/lib/function'
const URI = 'Test2'
type URI = typeof URI
declare module 'fp-ts/HKT' {
interface URItoKind<A> {
readonly Test2: Test2<A>
}
}
const TestFunctor: Functor1<URI> = {
URI,
map: (ma, f) => ({ Test2: f(ma.Test2) })
}
// function liftFunctor1<F extends URIS>(
// Fnctor: Functor1<F>
// ): <A, B>(f: (a: A) => B) => (fa: Kind<F, A>) => Kind<F, B> {
// return (f) => (fa) => Fnctor.map(fa, f)
// }
const liftFunctor1: <F extends URIS>(
Fnctor: Functor1<F>
) => <A, B>(f: (a: A) => B) => (fa: Kind<F, A>) => Kind<F, B> = (Fnctor) => (f) => (fa) =>
Fnctor.map(fa, f)
type Test2<XXX> = {
Test2: XXX
}
type A = number
type B = string
type F<XXX> = Test2<XXX>
describe('fp-ts Tutorial', () => {
it('HKT', () => {
// object
const a: A = 123
const fa: F<A> = {
Test2: a
}
// a function
const a_a_function_double: (a: A) => A = (a) => a * 2
const a_b_function_double: (a: A) => B = (a) => String(a_a_function_double(a))
// test
console.log(
`a => a * 2 :: `,
fa,
` => `,
pipe(fa, liftFunctor1(TestFunctor)(a_a_function_double))
)
console.log(
`a => String( a * 2 ) :: `,
fa,
` => `,
pipe(fa, liftFunctor1(TestFunctor)(a_b_function_double))
)
})
})
実行結果
a => a * 2 :: { Test2: 123 } => { Test2: 246 }
a => String( a * 2 ) :: { Test2: 123 } => { Test2: '246' }
functionの内容確認
参考
import { describe, it } from 'vitest'
import * as f from 'fp-ts/function'
import type { Semigroup } from 'fp-ts/lib/Semigroup'
import type { BooleanAlgebra } from 'fp-ts/lib/BooleanAlgebra'
import { pipe, type FunctionN } from 'fp-ts/function'
import type { Monoid } from 'fp-ts/lib/Monoid'
import type { Semiring } from 'fp-ts/lib/Semiring'
import type { Ring } from 'fp-ts/lib/Ring'
type Test2<XXX> = {
Test2: XXX
}
type _a = number
type _b = string
type A = Test2<_a>
type B = Test2<_b>
const _join_a = (a1: _a, a2: _a) => a1 | a2
const _not_a = (a: _a) => -a
const _meet_a = (a1: _a, a2: _a) => a1 & a2
const _add_a = (a1: _a, a2: _a) => a1 + a2
const _subtract_a = (a1: _a, a2: _a) => a1 - a2
const _multiply_a = (a1: _a, a2: _a) => a1 * a2
const _zero_a = 0
const _one_a = 1
const _search_text = (b1: _b, b2: _b): [number, _b] => {
const sizeDict: Record<number, [number, _b][]> = {}
for (let i = 0; i < b2.length; i++) {
for (let j = i + 1; j < b2.length + 1; j++) {
const target = b2.slice(i, j)
const search_result = b1.lastIndexOf(target)
if (search_result !== -1) {
const length = target.length
const data: [number, _b] = [search_result, target]
if (sizeDict[length]) {
sizeDict[length].push(data)
} else {
sizeDict[length] = [data]
}
}
}
}
const keys = Object.keys(sizeDict)
if (keys.length > 0) {
let max = 0
for (const key of keys) {
const numberKey = Number(key)
if (numberKey > max) {
max = numberKey
}
}
return sizeDict[max][0]
} else {
return [-1, _zero_b]
}
}
const _join_b = (b1: _b, b2: _b) => {
const search_result = _search_text(b1, b2)
if (search_result[0] !== -1) {
if (search_result[1] === b2) return b1
else {
const pre_text = b1.slice(0, search_result[0])
const after_text = b1.slice(search_result[0] + search_result[1].length)
const b2_search_result = b2.indexOf(search_result[1])
const b2_pre_text = b2.slice(0, b2_search_result)
const b2_after_text = b2.slice(b2_search_result + search_result[1].length)
const fix_b2_pre_text =
b2_pre_text.length > 0 ? _insert_text_left + b2_pre_text + _insert_text_right : b2_pre_text
const fix_b2_after_text =
b2_after_text.length > 0
? _insert_text_left + b2_after_text + _insert_text_right
: b2_after_text
return pre_text + fix_b2_pre_text + search_result[1] + fix_b2_after_text + after_text
}
} else {
return b1 + _join_text + b2
}
}
const _not_b = (b: _b) =>
b.length > 0 && b[0] === b[0].toUpperCase() ? b.toLowerCase() : b.toUpperCase()
const _meet_b = (b1: _b, b2: _b) => _search_text(b1, b2)[1]
const _add_b = (b1: _b, b2: _b) =>
b1.length > 0 && b2.length > 0
? b1 + _add_text + b2
: b1.length > 0
? b1
: b2.length > 0
? b2
: _zero_b
const _subtract_b = (b1: _b, b2: _b) => {
const search_result = _search_text(b1, b2)
if (search_result[0] !== -1) {
if (search_result[1] === b2) return b1.replace(b2, '')
else {
const pre_text = b1.slice(0, search_result[0])
const after_text = b1.slice(search_result[0] + search_result[1].length)
return pre_text + _delete_text + after_text
}
} else {
return b1
}
}
const _multiply_b = (b1: _b, b2: _b) =>
b1.length > 0 && b2.length > 0
? `{${b1}, ${b2}} + {${b2}, ${b1}}`
: b1.length > 0
? b1
: b2.length > 0
? b2
: _zero_b
const _zero_b = ''
const _one_b = '#'
const _insert_text_left = '['
const _insert_text_right = ']'
const _delete_text = '[]'
const _join_text = '_'
const _add_text = ' + '
const TestBooleanAlgebraTest2a: BooleanAlgebra<A> = {
implies: (first, second) => ({ Test2: _join_a(_not_a(first.Test2), second.Test2) }),
not: (a) => ({
Test2: _not_a(a.Test2)
}),
zero: {
Test2: _zero_a
},
join: (first, second) => ({ Test2: _join_a(first.Test2, second.Test2) }),
one: {
Test2: _one_a
},
meet: (first, second) => ({ Test2: _meet_a(first.Test2, second.Test2) })
}
const TestBooleanAlgebraTest2b: BooleanAlgebra<B> = {
implies: (first, second) => ({ Test2: _join_b(_not_b(first.Test2), second.Test2) }),
not: (b) => ({
Test2: _not_b(b.Test2)
}),
zero: {
Test2: _zero_b
},
join: (first, second) => ({ Test2: _join_b(first.Test2, second.Test2) }),
one: {
Test2: _one_b
},
meet: (first, second) => ({ Test2: _meet_b(first.Test2, second.Test2) })
}
const TestSemigroupTest2a: Semigroup<A> = {
concat: (x, y) => ({
Test2:
x !== undefined && y !== undefined
? _join_a(x.Test2, y.Test2)
: x !== undefined
? x.Test2
: y !== undefined
? y.Test2
: _zero_a
})
}
const TestSemigroupTest2b: Semigroup<B> = {
concat: (x, y) => ({
Test2:
x !== undefined && y !== undefined
? _join_b(x.Test2, y.Test2)
: x !== undefined
? x.Test2
: y !== undefined
? y.Test2
: _zero_b
})
}
const TestMonoidTest2a: Monoid<A> = {
concat: TestSemigroupTest2a.concat,
empty: {
Test2: _zero_a
}
}
const TestMonoidTest2b: Monoid<B> = {
concat: TestSemigroupTest2b.concat,
empty: {
Test2: _zero_b
}
}
const TestSemiringTest2a: Semiring<A> = {
add: (x, y) => ({
Test2: _add_a(x.Test2, y.Test2)
}),
zero: { Test2: _zero_a },
mul: (x, y) => ({
Test2: _multiply_a(x.Test2, y.Test2)
}),
one: {
Test2: _one_a
}
}
const TestSemiringTest2b: Semiring<B> = {
add: (x, y) => ({
Test2: _add_b(x.Test2, y.Test2)
}),
zero: { Test2: _zero_b },
mul: (x, y) => ({
Test2: _multiply_b(x.Test2, y.Test2)
}),
one: {
Test2: _one_b
}
}
const TestRingTest2a: Ring<A> = {
add: TestSemiringTest2a.add,
zero: TestSemiringTest2a.zero,
mul: TestSemiringTest2a.mul,
one: TestSemiringTest2a.one,
sub: (x, y) => ({ Test2: _subtract_a(x.Test2, y.Test2) })
}
const TestRingTest2b: Ring<B> = {
add: TestSemiringTest2b.add,
zero: TestSemiringTest2b.zero,
mul: TestSemiringTest2b.mul,
one: TestSemiringTest2b.one,
sub: (x, y) => ({ Test2: _subtract_b(x.Test2, y.Test2) })
}
describe('fp-ts Tutorial', () => {
it('function', () => {
// base
const a0: A = { Test2: 0 }
const a1: A = { Test2: 123 }
const b1: B = { Test2: 'abc' }
// a function
const a_a_function: (a: A) => A = (a) => ({
Test2: a.Test2
})
const a_a_function_double: (a: A) => A = (a) => ({
Test2: a.Test2 * 2
})
const a_b_function: (a: A) => B = (a) => ({
Test2: `${a.Test2}`
})
// aArray function
const aArray_b_function: (...a: A[]) => B = (...a) =>
a_b_function(
a.reduce((accumulator, current) => ({ Test2: accumulator.Test2 + current.Test2 }), a0)
)
const aArray_b_function_2: (a: A[]) => B = (a) => aArray_b_function(...a)
// b function
const b_b_function: (b: B) => B = (b) => ({
Test2: b.Test2
})
const b_b_function_double: (b: B) => B = (b) => ({
Test2: b.Test2 + b.Test2
})
const b_b_function_double_not: (b: B) => B = (b) => ({
Test2: b.Test2.toUpperCase() + b.Test2.toUpperCase()
})
const b_b_function_test: (b: B) => B = () => ({
Test2: 'zzcazz'
})
// FunctionN
const a_b_test_function: FunctionN<[A], B> = a_b_function
const b_b_test_function: FunctionN<[B], B> = b_b_function
// flip test
const a_b_c_test_function: (a: A) => (b: B) => boolean = (a) => (b) => true
const b_a_c_test_function: (b: B) => (a: A) => boolean = f.flip(a_b_c_test_function)
// test
console.log(
'getBooleanAlgebra - implies - [-133 => -246 | 123] - ',
'[-246 => 1... 0000 1010] - ',
'[ 123 => 0... 0111 1011] - ',
'[-133 => 1... 0111 1011] - ',
pipe(
a1,
f
.getBooleanAlgebra(TestBooleanAlgebraTest2a)<A>()
.implies(a_a_function_double, a_a_function)
)
)
console.log(
'getBooleanAlgebra - join - [255 => 246 | 123] - ',
'[246 => 1111 0110] - ',
'[123 => 0111 1011] - ',
'[255 => 1111 1111] - ',
pipe(
a1,
f.getBooleanAlgebra(TestBooleanAlgebraTest2a)<A>().join(a_a_function_double, a_a_function)
)
)
console.log(
'getBooleanAlgebra - meet - [114 => 246 & 123] - ',
'[246 => 1111 0110] - ',
'[123 => 0111 1011] - ',
'[114 => 0111 0010] - ',
pipe(
a1,
f.getBooleanAlgebra(TestBooleanAlgebraTest2a)<A>().meet(a_a_function_double, a_a_function)
)
)
console.log(
'getBooleanAlgebra - not - a1 - ',
pipe(a1, f.getBooleanAlgebra(TestBooleanAlgebraTest2a)<A>().not(a_a_function))
)
console.log(
'getBooleanAlgebra - one - a1 - ',
pipe(a1, f.getBooleanAlgebra(TestBooleanAlgebraTest2a)<A>().one)
)
console.log(
'getBooleanAlgebra - zero - a1 - ',
pipe(a1, f.getBooleanAlgebra(TestBooleanAlgebraTest2a)<A>().zero)
)
console.log('---------')
console.log(
'getBooleanAlgebra - implies - [ "ABCABC_abc" => ABCABC | abc ] - ',
pipe(
b1,
f
.getBooleanAlgebra(TestBooleanAlgebraTest2b)<B>()
.implies(b_b_function_double, b_b_function)
)
)
console.log(
'getBooleanAlgebra - implies - [ "abcabc" => abcabc | abc ] - ',
pipe(
b1,
f
.getBooleanAlgebra(TestBooleanAlgebraTest2b)<B>()
.implies(b_b_function_double_not, b_b_function)
)
)
console.log(
'getBooleanAlgebra - join - ab[zz]ca[zz]bc => abcabc | zzcazz - ',
pipe(
b1,
f
.getBooleanAlgebra(TestBooleanAlgebraTest2b)<B>()
.join(b_b_function_double, b_b_function_test)
)
)
console.log(
'getBooleanAlgebra - meet - [abc => abcabc & abc] - ',
pipe(
b1,
f.getBooleanAlgebra(TestBooleanAlgebraTest2b)<B>().meet(b_b_function_double, b_b_function)
)
)
console.log(
'getBooleanAlgebra - not - b1 - ',
pipe(b1, f.getBooleanAlgebra(TestBooleanAlgebraTest2b)<B>().not(b_b_function))
)
console.log(
'getBooleanAlgebra - one - b1 - ',
pipe(b1, f.getBooleanAlgebra(TestBooleanAlgebraTest2b)<B>().one)
)
console.log(
'getBooleanAlgebra - zero - b1 - ',
pipe(b1, f.getBooleanAlgebra(TestBooleanAlgebraTest2b)<B>().zero)
)
console.log('---------')
console.log(
'getSemigroup - concat - a1 - ',
pipe(a1, f.getSemigroup(TestSemigroupTest2a)<A>().concat(a_a_function_double, a_a_function))
)
console.log(
'getSemigroup - concat - b1 - ',
pipe(b1, f.getSemigroup(TestSemigroupTest2b)<B>().concat(b_b_function_double, b_b_function))
)
console.log('---------')
console.log(
'getMonoid - concat - a1 - ',
pipe(a1, f.getMonoid(TestMonoidTest2a)<A>().concat(a_a_function_double, a_a_function))
)
console.log('getMonoid - empty - a1 - ', pipe(a1, f.getMonoid(TestMonoidTest2a)<A>().empty))
console.log(
'getMonoid - concat - b1 - ',
pipe(b1, f.getMonoid(TestMonoidTest2b)<B>().concat(b_b_function_double, b_b_function))
)
console.log('getMonoid - empty - b1 - ', pipe(b1, f.getMonoid(TestMonoidTest2b)<B>().empty))
console.log('---------')
console.log(
'getSemiring - add - [369 => 246 + 123] - ',
pipe(a1, f.getSemiring<A, A>(TestSemiringTest2a).add(a_a_function_double, a_a_function))
)
console.log(
'getSemiring - mul - [30258 => 246 * 123] - ',
pipe(a1, f.getSemiring<A, A>(TestSemiringTest2a).mul(a_a_function_double, a_a_function))
)
console.log('getSemiring - one - ', pipe(a1, f.getSemiring<A, A>(TestSemiringTest2a).one))
console.log('getSemiring - zero - ', pipe(a1, f.getSemiring<A, A>(TestSemiringTest2a).zero))
console.log('---------')
console.log(
'getSemiring - add - ',
pipe(b1, f.getSemiring<B, B>(TestSemiringTest2b).add(b_b_function_double, b_b_function))
)
console.log(
'getSemiring - mul - ',
pipe(b1, f.getSemiring<B, B>(TestSemiringTest2b).mul(b_b_function_double, b_b_function))
)
console.log('getSemiring - one - ', pipe(b1, f.getSemiring<B, B>(TestSemiringTest2b).one))
console.log('getSemiring - zero - ', pipe(b1, f.getSemiring<B, B>(TestSemiringTest2b).zero))
console.log('---------')
console.log(
'getRing - sub - [-123 => 123 - 246] - ',
pipe(a1, f.getRing<A, A>(TestRingTest2a).sub(a_a_function, a_a_function_double))
)
console.log(
'getRing - sub - [ab[]bc => abcabc - zzcazz] - ',
pipe(b1, f.getRing<B, B>(TestRingTest2b).sub(b_b_function_double, b_b_function_test))
)
console.log('---------')
console.log('apply - ', pipe(a_b_function, f.apply<A>(a1)))
console.log('---------')
console.log('identity - ', pipe(a1, f.identity))
console.log('---------')
console.log('unsafeCoerce - ', pipe(a1, f.unsafeCoerce<A, B>))
console.log('---------')
console.log('constant - is LazyArg<A> - ', pipe(a1, f.constant)())
console.log('---------')
console.log('constTrue - ', f.constTrue())
console.log('constFalse - ', f.constFalse())
console.log('constNull - ', f.constNull())
console.log('constUndefined - ', f.constUndefined())
console.log('constVoid - ', f.constVoid())
console.log('---------')
console.log('tuple - ', pipe([a1, a1, a1], f.tuple))
console.log('---------')
console.log('increment - 2 - ', pipe(2, f.increment))
console.log('decrement - 2 - ', pipe(2, f.decrement))
console.log('---------')
console.log('absurd - ', f.absurd)
console.log('---------')
console.log(
'tupled - [a1, a1, a1]::a1 - [369 = 123*3] - ',
pipe(aArray_b_function, f.tupled)([a1, a1, a1])
)
console.log(
'untupled - (a1, a1, a1) - [369 = 123*3] - ',
pipe(aArray_b_function_2, f.untupled)(a1, a1, a1)
)
console.log('---------')
console.log('hole - ', f.hole)
console.log('---------')
console.log('SK - (a1, b1) - ', f.SK(a1, b1))
})
})
実行結果
getBooleanAlgebra - implies - [-133 => -246 | 123] - [-246 => 1... 0000 1010] - [ 123 => 0... 0111 1011] - [-133 => 1... 0111 1011] - { Test2: -133 }
getBooleanAlgebra - join - [255 => 246 | 123] - [246 => 1111 0110] - [123 => 0111 1011] - [255 => 1111 1111] - { Test2: 255 }
getBooleanAlgebra - meet - [114 => 246 & 123] - [246 => 1111 0110] - [123 => 0111 1011] - [114 => 0111 0010] - { Test2: 114 }
getBooleanAlgebra - not - a1 - { Test2: -123 }
getBooleanAlgebra - one - a1 - { Test2: 1 }
getBooleanAlgebra - zero - a1 - { Test2: 0 }
---------
getBooleanAlgebra - implies - [ "ABCABC_abc" => ABCABC | abc ] - { Test2: 'ABCABC_abc' }
getBooleanAlgebra - implies - [ "abcabc" => abcabc | abc ] - { Test2: 'abcabc' }
getBooleanAlgebra - join - ab[zz]ca[zz]bc => abcabc | zzcazz - { Test2: 'ab[zz]ca[zz]bc' }
getBooleanAlgebra - meet - [abc => abcabc & abc] - { Test2: 'abc' }
getBooleanAlgebra - not - b1 - { Test2: 'ABC' }
getBooleanAlgebra - one - b1 - { Test2: '#' }
getBooleanAlgebra - zero - b1 - { Test2: '' }
---------
getSemigroup - concat - a1 - { Test2: 255 }
getSemigroup - concat - b1 - { Test2: 'abcabc' }
---------
getMonoid - concat - a1 - { Test2: 255 }
getMonoid - empty - a1 - { Test2: 0 }
getMonoid - concat - b1 - { Test2: 'abcabc' }
getMonoid - empty - b1 - { Test2: '' }
---------
getSemiring - add - [369 => 246 + 123] - { Test2: 369 }
getSemiring - mul - [30258 => 246 * 123] - { Test2: 30258 }
getSemiring - one - { Test2: 1 }
getSemiring - zero - { Test2: 0 }
---------
getSemiring - add - { Test2: 'abcabc + abc' }
getSemiring - mul - { Test2: '{abcabc, abc} + {abc, abcabc}' }
getSemiring - one - { Test2: '#' }
getSemiring - zero - { Test2: '' }
---------
getRing - sub - [-123 => 123 - 246] - { Test2: -123 }
getRing - sub - [ab[]bc => abcabc - zzcazz] - { Test2: 'ab[]bc' }
---------
apply - { Test2: '123' }
---------
identity - { Test2: 123 }
---------
unsafeCoerce - { Test2: 123 }
---------
constant - is LazyArg<A> - { Test2: 123 }
---------
constTrue - true
constFalse - false
constNull - null
constUndefined - undefined
constVoid - undefined
---------
tuple - [ [ { Test2: 123 }, { Test2: 123 }, { Test2: 123 } ] ]
---------
increment - 2 - 3
decrement - 2 - 1
---------
absurd - [Function: absurd]
---------
tupled - [a1, a1, a1]::a1 - [369 = 123*3] - { Test2: '369' }
untupled - (a1, a1, a1) - [369 = 123*3] - { Test2: '369' }
---------
hole - [Function: absurd]
---------
SK - (a1, b1) - { Test2: 'abc' }