Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/long-fans-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tsplus/stdlib": patch
---

Finish renaming of Option => Maybe
3 changes: 2 additions & 1 deletion packages/stdlib/_examples/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
console.log(ImmutableArray.from(List(0, 1, 2)) == ImmutableArray(0, 1, 2))
console.log([0, 1, 2].immutable == ImmutableArray(0, 1, 2))
console.log(
Maybe(0)(
pipe(
Maybe(0),
Maybe.$.map((n) => n + 1),
Maybe.$.map((n) => `res: ${n}`)
) == Maybe("res: 1")
Expand Down
2 changes: 1 addition & 1 deletion packages/stdlib/_src/collections/ImmutableArray/dsl/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const adapter: {
} = (_: () => any) =>
new GenLazyHKT(() => {
const x = _()
if (Maybe.isOption(x)) {
if (Maybe.isMaybe(x)) {
return x._tag === "None" ? ImmutableArray.empty() : ImmutableArray(x.value)
}
return x
Expand Down
2 changes: 1 addition & 1 deletion packages/stdlib/_src/collections/SortedMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * from "@tsplus/stdlib/collections/SortedMap/from"
export * from "@tsplus/stdlib/collections/SortedMap/get"
export * from "@tsplus/stdlib/collections/SortedMap/getOrd"
export * from "@tsplus/stdlib/collections/SortedMap/has"
export * from "@tsplus/stdlib/collections/SortedMap/headOption"
export * from "@tsplus/stdlib/collections/SortedMap/headMaybe"
export * from "@tsplus/stdlib/collections/SortedMap/isEmpty"
export * from "@tsplus/stdlib/collections/SortedMap/isNonEmpty"
export * from "@tsplus/stdlib/collections/SortedMap/keys"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { concreteSortedMap } from "@tsplus/stdlib/collections/SortedMap/_internal/SortedMapInternal"

/**
* @tsplus getter SortedMap headOption
* @tsplus getter SortedMap headMaybe
*/
export function headOption<K, V>(self: SortedMap<K, V>): Maybe<Tuple<[K, V]>> {
export function headMaybe<K, V>(self: SortedMap<K, V>): Maybe<Tuple<[K, V]>> {
concreteSortedMap(self)
return self.tree.first
}
6 changes: 3 additions & 3 deletions packages/stdlib/_src/collections/weak/IterableWeakMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ export const IterableWeakMap: IterableWeakMapOps = {}

/**
* @tsplus index IterableWeakMap
* @tsplus fluent IterableWeakMap getOption
* @tsplus fluent IterableWeakMap getMaybe
*/
export function getOption_<K extends object, V>(
export function getMaybe_<K extends object, V>(
self: IterableWeakMap<K, V>,
key: K
): Maybe<V> {
return self.has(key) ? Maybe.some(self.get(key)!) : Maybe.none
}

export const getOption = Pipeable(getOption_)
export const getMaybe = Pipeable(getMaybe_)

/**
* @tsplus static IterableWeakMapOps make
Expand Down
6 changes: 0 additions & 6 deletions packages/stdlib/_src/data/Duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,3 @@ export function equals_(self: Duration, that: Duration) {
}

export const equals = Pipeable(equals_)

/**
* @tsplus macro pipe
* @tsplus fluent Duration __call
*/
export const durationPipe: typeof pipe = pipe
4 changes: 2 additions & 2 deletions packages/stdlib/_src/data/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export * from "@tsplus/stdlib/data/Either/ap"
export * from "@tsplus/stdlib/data/Either/catchAll"
export * from "@tsplus/stdlib/data/Either/compact"
export * from "@tsplus/stdlib/data/Either/compactOption"
export * from "@tsplus/stdlib/data/Either/compactMaybe"
export * from "@tsplus/stdlib/data/Either/definition"
export * from "@tsplus/stdlib/data/Either/dsl"
export * from "@tsplus/stdlib/data/Either/duplicate"
Expand All @@ -14,8 +14,8 @@ export * from "@tsplus/stdlib/data/Either/flatten"
export * from "@tsplus/stdlib/data/Either/fold"
export * from "@tsplus/stdlib/data/Either/foldMap"
export * from "@tsplus/stdlib/data/Either/forEachF"
export * from "@tsplus/stdlib/data/Either/fromMaybe"
export * from "@tsplus/stdlib/data/Either/fromNullable"
export * from "@tsplus/stdlib/data/Either/fromOption"
export * from "@tsplus/stdlib/data/Either/fromPredicate"
export * from "@tsplus/stdlib/data/Either/getAssociative"
export * from "@tsplus/stdlib/data/Either/getCompact"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* Compact an `Either<E, Maybe<A>>` to an `Either<E, A>` given an
* `AssociativeIdentity<E>`.
*
* @tsplus fluent Either compactOption
* @tsplus fluent Either compactMaybe
*/
export function compactOption_<E, A>(self: Either<E, Maybe<A>>, M: AssociativeIdentity<E>): Either<E, A> {
export function compactMaybe_<E, A>(self: Either<E, Maybe<A>>, M: AssociativeIdentity<E>): Either<E, A> {
return self.isLeft() ? self : self.right._tag === "None" ? Either.left(M.identity) : Either.right(self.right.value)
}

/**
* Compact an `Either<E, Maybe<A>>` to an `Either<E, A>` given an
* `AssociativeIdentity<E>`.
*
* @tsplus static Either/Aspects compactOption
* @tsplus static Either/Aspects compactMaybe
*/
export const compactOption = Pipeable(compactOption_)
export const compactMaybe = Pipeable(compactMaybe_)
6 changes: 0 additions & 6 deletions packages/stdlib/_src/data/Either/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,3 @@ export function widenA<A1>() {
<E, A>(self: Either<E, A>): Either<E, A | A1> => self
)
}

/**
* @tsplus macro pipe
* @tsplus fluent Either __call
*/
export const eitherPipe: typeof pipe = pipe
2 changes: 1 addition & 1 deletion packages/stdlib/_src/data/Either/dsl/getCompactF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @tsplus static Either/Ops getCompactF
*/
export function getCompactF<E>(M: AssociativeIdentity<E>): Wither<Either.FixedLeftHKT<E>> {
const compact = Either.$.compactOption(M)
const compact = Either.$.compactMaybe(M)
return HKT.instance(
<G extends HKT>(G: Applicative<G>) =>
<GR, GE, A, B>(f: (a: A) => HKT.Kind<G, GR, GE, Maybe<B>>) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Construct `Either<E, A>` from `Maybe<A>` constructing `E` with `onNone`.
*
* @tsplus static Either/Ops fromOption
* @tsplus static Either/Ops fromMaybe
*/
export function fromOption<A, E>(maybe: Maybe<A>, onNone: LazyArg<E>): Either<E, A> {
export function fromMaybe<A, E>(maybe: Maybe<A>, onNone: LazyArg<E>): Either<E, A> {
return maybe.isNone() ? Either.left(onNone()) : Either.right(maybe.value)
}
2 changes: 1 addition & 1 deletion packages/stdlib/_src/data/Either/getCompact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @tsplus static Either/Ops getCompact
*/
export function getCompact<E>(M: AssociativeIdentity<E>) {
const compact = Either.$.compactOption(M)
const compact = Either.$.compactMaybe(M)
return HKT.instance<Compact<Either.FixedLeftHKT<E>>>({
compact
})
Expand Down
32 changes: 13 additions & 19 deletions packages/stdlib/_src/data/Maybe/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
*/
export type Maybe<A> = None | Some<A>

export interface OptionF extends HKT {
export interface MaybeF extends HKT {
readonly type: Maybe<this["A"]>
}

export declare namespace Maybe {
export type HKT = MaybeF
}

/**
* @tsplus type Maybe/Ops
*/
export interface OptionOps {
$: OptionAspects
export interface MaybeOps {
$: MaybeAspects
}
export const Maybe: OptionOps = {
export const Maybe: MaybeOps = {
$: {}
}

/**
* @tsplus type Maybe/Aspects
*/
export interface OptionAspects {}

export declare namespace Maybe {
export type HKT = OptionF
}
export interface MaybeAspects {}

const _noneHash = Hash.string("Maybe/None")
const _someHash = Hash.string("Maybe/Some")
Expand Down Expand Up @@ -67,13 +67,13 @@ export class Some<A> implements Equals {
* @tsplus unify Maybe/Some
* @tsplus unify Maybe/None
*/
export function unifyOption<X extends Maybe<any>>(
export function unifyMaybe<X extends Maybe<any>>(
self: X
): Maybe<[X] extends [Maybe<infer A>] ? A : never> {
return self
}

export type ArrayOfOptions<Ts extends Maybe<any>[]> = {
export type ArrayOfMaybies<Ts extends Maybe<any>[]> = {
[k in keyof Ts]: Ts[k] extends Maybe<infer A> ? A : never
}[number]

Expand Down Expand Up @@ -121,19 +121,13 @@ export function isSome<A>(fa: Maybe<A>): fa is Some<A> {
}

/**
* @tsplus static Maybe/Ops isOption
* @tsplus static Maybe/Ops isMaybe
*/
export function isOption(u: unknown): u is Maybe<unknown> {
export function isMaybe(u: unknown): u is Maybe<unknown> {
return (
typeof u === "object" &&
u != null &&
"_tag" in u &&
(u["_tag"] === "Some" || u["_tag"] === "None")
)
}

/**
* @tsplus macro pipe
* @tsplus fluent Maybe __call
*/
export const optionPipe: typeof pipe = pipe
4 changes: 2 additions & 2 deletions packages/stdlib/_src/data/Maybe/getRefinement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
*
* @tsplus static Maybe/Ops getRefinement
*/
export function getRefinement<A, B extends A>(getOption: (a: A) => Maybe<B>): Refinement<A, B> {
return (a: A): a is B => getOption(a).isSome()
export function getRefinement<A, B extends A>(getMaybe: (a: A) => Maybe<B>): Refinement<A, B> {
return (a: A): a is B => getMaybe(a).isSome()
}
6 changes: 0 additions & 6 deletions packages/stdlib/_src/data/Tuple/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,3 @@ export function unifyTuple<X extends Tuple<any>>(
export function isTuple(self: unknown): self is Tuple<unknown[]> {
return typeof self === "object" && self != null && TupleSym in self
}

/**
* @tsplus macro pipe
* @tsplus fluent Tuple __call
*/
export const tuplePipe: typeof pipe = pipe
16 changes: 6 additions & 10 deletions packages/stdlib/_src/runtime/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,19 +359,15 @@ export function deriveArray<A extends Array<any>>(

type EitherStructural<E, A> = { _tag: "Left"; left: E } | { _tag: "Right"; right: A }

function deriveEitherInternal<E, A>(
/** @tsplus implicit local */ left: Decoder<E>,
/** @tsplus implicit local */ right: Decoder<A>
): Decoder<EitherStructural<E, A>> {
function deriveEitherInternal<E, A>(left: Decoder<E>, right: Decoder<A>): Decoder<EitherStructural<E, A>> {
return Derive()
}

/**
* @tsplus derive Decoder[Either]<_> 10
*/
export function deriveEither<A extends Either<any, any>>(
...[left, right]: [A] extends [Either<infer _E, infer _A>] ? [left: Decoder<_E>, right: Decoder<_A>]
: never
...[left, right]: [A] extends [Either<infer _E, infer _A>] ? [left: Decoder<_E>, right: Decoder<_A>] : never
): Decoder<A> {
const structural = deriveEitherInternal(left, right)
return Decoder((u) =>
Expand All @@ -381,20 +377,20 @@ export function deriveEither<A extends Either<any, any>>(

type OptionStructural<A> = { _tag: "None" } | { _tag: "Some"; value: A }

function deriveOptionInternal<A>(
/** @tsplus implicit local */ value: Decoder<A>
function deriveMaybeInternal<A>(
value: Decoder<A>
): Decoder<OptionStructural<A>> {
return Derive()
}

/**
* @tsplus derive Decoder[Maybe]<_> 10
*/
export function deriveOption<A extends Maybe<any>>(
export function deriveMaybe<A extends Maybe<any>>(
...[value]: [A] extends [Maybe<infer _A>] ? [value: Decoder<_A>]
: never
): Decoder<A> {
const structural = deriveOptionInternal(value)
const structural = deriveMaybeInternal(value)
return Decoder((u) =>
structural.decodeResult(u).map((e) => e._tag === "Some" ? Maybe.some(e.value) as A : Maybe.none as A)
)
Expand Down
6 changes: 3 additions & 3 deletions packages/stdlib/_src/service/Env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function methodGet<R, S>(this: Env<R>, tag: Tag<S>): S {
return this.unsafeMap.get(tag)! as S
}

function methodGetOption<R, S>(this: Env<R>, tag: Tag<S>): Maybe<S> {
function methodGetMaybe<R, S>(this: Env<R>, tag: Tag<S>): Maybe<S> {
return this.unsafeMap.has(tag) ? Maybe.some(this.unsafeMap.get(tag)! as S) : Maybe.none
}

Expand Down Expand Up @@ -72,7 +72,7 @@ function createEnv<R>(unsafeMap: Env<R>["unsafeMap"]) {
add: methodAdd,
get: methodGet,
unsafeGet: methodGet,
getOption: methodGetOption,
getMaybe: methodGetMaybe,
merge: methodMerge,
prune: pruneMethod,
unsafeMap
Expand All @@ -90,7 +90,7 @@ export interface Env<R> {
add<R, S, H extends S = S>(this: Env<R>, tag: Tag<S>, service: H): Env<R | S>
get<R, T extends Tags<R>>(this: Env<R>, tag: T): T extends Tag<infer S> ? S : never
unsafeGet<R, S>(this: Env<R>, tag: Tag<S>): S
getOption<R, S>(this: Env<R>, tag: Tag<S>): Maybe<S>
getMaybe<R, S>(this: Env<R>, tag: Tag<S>): Maybe<S>
merge<R, R1>(this: Env<R>, that: Env<R1>): Env<R | R1>
prune<R, S extends Tags<R>[]>(this: Env<R>, ...tags: S): Env<{ [k in keyof S]: Tag.TagType<S[k]> }[number]>
}
10 changes: 5 additions & 5 deletions packages/stdlib/_test/Either.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ describe.concurrent("Either", () => {
})

it("compactOption", () => {
assert.isTrue(Either.left("hello").compactOption(AssociativeIdentity.string) == Either.left("hello"))
assert.isTrue(Either.right(Maybe.none).compactOption(AssociativeIdentity.string) == Either.left(""))
assert.isTrue(Either.right(Maybe.some(0)).compactOption(AssociativeIdentity.string) == Either.right(0))
assert.isTrue(Either.left("hello").compactMaybe(AssociativeIdentity.string) == Either.left("hello"))
assert.isTrue(Either.right(Maybe.none).compactMaybe(AssociativeIdentity.string) == Either.left(""))
assert.isTrue(Either.right(Maybe.some(0)).compactMaybe(AssociativeIdentity.string) == Either.right(0))
})

it("do", () => {
Expand Down Expand Up @@ -162,8 +162,8 @@ describe.concurrent("Either", () => {
})

it("fromOption", () => {
assert.isTrue(Either.fromOption(Maybe.some(0), () => 1) == Either.right(0))
assert.isTrue(Either.fromOption(Maybe.none, () => 1) == Either.left(1))
assert.isTrue(Either.fromMaybe(Maybe.some(0), () => 1) == Either.right(0))
assert.isTrue(Either.fromMaybe(Maybe.none, () => 1) == Either.left(1))
})

it("fromPredicate", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/stdlib/_test/Maybe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ describe.concurrent("Maybe", () => {
})

it("aspects", () => {
const result = Maybe(0)(
const result = pipe(
Maybe(0),
Maybe.$.map((n) => n + 1),
Maybe.$.map((n) => n + 1),
Maybe.$.flatMap((n) => Maybe(`ok: ${n}`))
)

assert.isTrue(
result == Maybe("ok: 2")
)
Expand Down
14 changes: 7 additions & 7 deletions packages/stdlib/_test/Service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ describe.concurrent("Environment", () => {
const env = Service.Env(A, { a: 0 }).add(B, { b: 1 })

assert.deepEqual(env.get(A), { a: 0 })
assert.deepEqual(env.getOption(B).value, { b: 1 })
assert.isTrue(env.getOption(C).isNone())
assert.deepEqual(env.getMaybe(B).value, { b: 1 })
assert.isTrue(env.getMaybe(C).isNone())
assert.throw(() => env.unsafeGet(C))
})

it("prunes services in env and merges", () => {
const env = Service.Env(A, { a: 0 }).merge(Service.Env(B, { b: 1 }).add(C, { c: 2 }))
const pruned = env.prune(A, B)
assert.deepEqual(pruned.get(A), { a: 0 })
assert.deepEqual(pruned.getOption(B).value, { b: 1 })
assert.isTrue(pruned.getOption(C).isNone())
assert.deepEqual(pruned.getMaybe(B).value, { b: 1 })
assert.isTrue(pruned.getMaybe(C).isNone())
assert.throw(() => pruned.unsafeGet(C))
assert.deepEqual(env.get(C), { c: 2 })
})
Expand All @@ -44,9 +44,9 @@ describe.concurrent("Environment", () => {

const result = patch.patch(oldEnv)

assert.isTrue(result.getOption(A).isSome())
assert.isTrue(result.getOption(B).isSome())
assert.isTrue(result.getOption(C).isNone())
assert.isTrue(result.getMaybe(A).isSome())
assert.isTrue(result.getMaybe(B).isSome())
assert.isTrue(result.getMaybe(C).isNone())
assert.strictEqual(result.get(B).b, 3)
})

Expand Down
Loading