Skip to content

Commit

Permalink
rename Cons -> ConsList
Browse files Browse the repository at this point in the history
  • Loading branch information
mccraigmccraig committed Dec 10, 2023
1 parent 9a6bf08 commit fb822bb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
32 changes: 16 additions & 16 deletions src/cons_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export function isNone(v: any): v is None {
// it's probably a cons, but we're only checking
// one level with this type
// deno-lint-ignore no-explicit-any
export type NRCons<T> = None | readonly [T, None | readonly any[]]
export type NRConsList<T> = None | readonly [T, None | readonly any[]]

// a Cons type with limited conditional recursion
// T is the base type for the values
export type Cons<T, C> =
export type ConsList<T, C> =
C extends None
? None
: C extends readonly [infer H extends T, infer R]
? readonly [H, Cons<T, R>]
? readonly [H, ConsList<T, R>]
// don't recurse when there is no match,
// it murders the compiler
: never
Expand All @@ -32,13 +32,13 @@ export type Cons<T, C> =
export function cons<T>() {
return function <V extends T, const C>(
v: V,
c: C extends Cons<T, C> ? C : Cons<T, C>) {
c: C extends ConsList<T, C> ? C : ConsList<T, C>) {
return [v, c] as const
}
}

export type First<T, C> =
C extends Cons<T, C>
C extends ConsList<T, C>
? C extends None
? None
: C extends readonly [infer H extends T, infer _R]
Expand All @@ -48,7 +48,7 @@ export type First<T, C> =

// get the first element from a cons list
export function first<T>() {
return function <const C>(c: C extends Cons<T, C> ? C : Cons<T, C>)
return function <const C>(c: C extends ConsList<T, C> ? C : ConsList<T, C>)
: First<T, C> {
if (isNone(c)) {
return None as First<T, C>
Expand All @@ -59,7 +59,7 @@ export function first<T>() {
}

export type Rest<T, C> =
C extends Cons<T, C>
C extends ConsList<T, C>
? C extends None
? None
: C extends readonly [infer _H extends T, infer R]
Expand All @@ -69,7 +69,7 @@ export type Rest<T, C> =

// get the rest of a cons list
export function rest<T>() {
return function <const C>(c: C extends Cons<T, C> ? C : Cons<T, C>)
return function <const C>(c: C extends ConsList<T, C> ? C : ConsList<T, C>)
: Rest<T, C> {
if (isNone(c)) {
return c as Rest<T, C>
Expand All @@ -89,9 +89,9 @@ export type Last<T, C> =
: never

export function last<T>() {
return function <const C>(c: C extends Cons<T, C> ? C : Cons<T, C>) {
return function <const C>(c: C extends ConsList<T, C> ? C : ConsList<T, C>) {

let result: Cons<T, None> = None
let result: ConsList<T, None> = None
let cursor = c
while (!isNone(cursor)) {
// deno-lint-ignore no-explicit-any
Expand All @@ -105,7 +105,7 @@ export function last<T>() {
}

export type Reverse<T, C, Acc = None> =
C extends Cons<T, C>
C extends ConsList<T, C>
? C extends None
? Acc
: C extends readonly [infer F extends T, infer R]
Expand All @@ -114,9 +114,9 @@ export type Reverse<T, C, Acc = None> =
: never

export function reverse<T>() {
return function <const C>(c: C extends Cons<T, C> ? C : Cons<T, C>) {
return function <const C>(c: C extends ConsList<T, C> ? C : ConsList<T, C>) {

let result: Cons<T, None> = None
let result: ConsList<T, None> = None
let cursor = c
while (!isNone(cursor)) {
// deno-lint-ignore no-explicit-any
Expand All @@ -139,7 +139,7 @@ export type Append<T, C, V extends T> =
// NB: builds an entirely new cons list
export function append<T>() {
return function <const C, V extends T>(
c: C extends Cons<T, C> ? C : Cons<T, C>,
c: C extends ConsList<T, C> ? C : ConsList<T, C>,
v: V) {

// deno-lint-ignore no-explicit-any
Expand All @@ -159,7 +159,7 @@ export type ToTuple<T, C, Acc extends readonly T[] = []> =
export function toTuple<T>() {
return function
<const C>
(c: C extends Cons<T, C> ? C : Cons<T, C>) {
(c: C extends ConsList<T, C> ? C : ConsList<T, C>) {

const result = []
let cursor = c
Expand All @@ -175,7 +175,7 @@ export function toTuple<T>() {
export type FromTuple<T,
// deno-lint-ignore no-explicit-any
Tuple extends readonly [...any[]],
Acc extends NRCons<T> = None> =
Acc extends NRConsList<T> = None> =

Tuple extends readonly []
? Acc
Expand Down
28 changes: 14 additions & 14 deletions src/object_chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {

// an effectful function of Input which build an Object
export type ObjectChainProgram<Input extends ChainTagged,
Steps extends cons.NRCons<UPObjectStepSpec>> =
Steps extends cons.NRConsList<UPObjectStepSpec>> =
(i: Input) => Effect.Effect<
ObjectStepsTupleReqsU<Steps>,
ObjectStepsErrorsU<Steps>,
Expand All @@ -23,18 +23,18 @@ export type ObjectChainProgram<Input extends ChainTagged,

// a type for a service which can run an ObjectChain
export type ObjectChainService<Input extends ChainTagged,
Steps extends cons.NRCons<UPObjectStepSpec>> = {
Steps extends cons.NRConsList<UPObjectStepSpec>> = {
readonly buildObject: ObjectChainProgram<Input, Steps>
}

export type ObjectChainServiceContextTag<Input extends ChainTagged,
Steps extends cons.NRCons<UPObjectStepSpec>> =
Steps extends cons.NRConsList<UPObjectStepSpec>> =
Context.Tag<ChainTag<Input>, ObjectChainService<Input, Steps>>

// get a Context.Tag for an ObjectChainService
export function objectChainServiceContextTag
<Input extends ChainTagged,
Steps extends cons.NRCons<UPObjectStepSpec>>
Steps extends cons.NRConsList<UPObjectStepSpec>>
() {
return Context.Tag<ChainTag<Input>, ObjectChainService<Input, Steps>>()
}
Expand All @@ -43,7 +43,7 @@ export function objectChainServiceContextTag
// Object. it can be built in a single step with objectChain, or iteratively
// with addSteps
export type ObjectChain<Input extends ChainTagged,
Steps extends cons.NRCons<UPObjectStepSpec>,
Steps extends cons.NRConsList<UPObjectStepSpec>,
// this param is only here so that a chain value's IntelliSense
// shows the chain's return type
_Return = ObjectChainStepsReturn<Steps,Input>> = {
Expand All @@ -64,7 +64,7 @@ export type UPObjectChain = {
// deno-lint-ignore no-explicit-any
readonly tag: any
readonly tagStr: string
readonly steps: cons.NRCons<UPObjectStepSpec>
readonly steps: cons.NRConsList<UPObjectStepSpec>
// deno-lint-ignore no-explicit-any
readonly program: (i: any) => Effect.Effect<any, any, any>
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -103,7 +103,7 @@ export type UPObjectChainProgramValue<T extends UPObjectChain> =

// build an ObjectChain from Steps
export function objectChain<Input extends ChainTagged>() {
return function <const Steps extends cons.NRCons<UPObjectStepSpec>>
return function <const Steps extends cons.NRConsList<UPObjectStepSpec>>
(tag: ChainTag<Input>,

steps: ObjectChainSteps<Steps, Input> extends Steps
Expand All @@ -127,7 +127,7 @@ export function objectChain<Input extends ChainTagged>() {
// add an FxStep to an ObjectChain, returning a new ObjectChain
export function addFxStep
<Input extends ChainTagged,
const Steps extends cons.NRCons<UPObjectStepSpec>,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
NewStep extends UPFxObjectStepSpec>

(chain: ObjectChain<Input, Steps>,
Expand All @@ -147,7 +147,7 @@ export function addFxStep
// make an FxStep at the end of an ObjectChain, returning a new ObjectChain
export function makeFxStep
<Input extends ChainTagged,
const Steps extends cons.NRCons<UPObjectStepSpec>,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
K extends string,
A extends ObjectChainStepsReturn<Steps, Input>,
D1 extends D2,
Expand All @@ -169,7 +169,7 @@ export function makeFxStep
// add a PureStep to an ObjectChain, returning a new ObjectChain
export function addPureStep
<Input extends ChainTagged,
const Steps extends cons.NRCons<UPObjectStepSpec>,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
NewStep extends UPPureObjectStepSpec>

(chain: ObjectChain<Input, Steps>,
Expand All @@ -189,7 +189,7 @@ export function addPureStep
// make a PureStep at the end of an ObjectChain, returning a new ObjectChain
export function makePureStep
<Input extends ChainTagged,
const Steps extends cons.NRCons<UPObjectStepSpec>,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
K extends string,
A extends ObjectChainStepsReturn<Steps, Input>,
V>(chain: ObjectChain<Input, Steps>,
Expand All @@ -216,7 +216,7 @@ export function makePureStep
export function objectChainServiceImpl

<Input extends ChainTagged,
const Steps extends cons.NRCons<UPObjectStepSpec>>
const Steps extends cons.NRConsList<UPObjectStepSpec>>

(chain: ObjectChain<Input, Steps>) {

Expand All @@ -233,7 +233,7 @@ export function objectChainServiceImpl
// to an Effect
export function provideObjectChainServiceImpl
<Input extends ChainTagged,
const Steps extends cons.NRCons<UPObjectStepSpec>,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
InR, InE, InV>

(effect: Effect.Effect<InR, InE, InV>,
Expand All @@ -250,7 +250,7 @@ export function provideObjectChainServiceImpl
// calls it's buildObject function
export function objectChainFxFn
<Input extends ChainTagged,
const Steps extends cons.NRCons<UPObjectStepSpec>>
const Steps extends cons.NRConsList<UPObjectStepSpec>>

(chain: ObjectChain<Input, Steps>) {

Expand Down
30 changes: 15 additions & 15 deletions src/object_chain_steps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Effect } from "effect"
import { FxFn, UPFxFn } from "./fx_fn.ts"
import { None, NRCons, Last, ToTuple, toTuple} from "./cons_list.ts"
import { None, NRConsList, Last, ToTuple, toTuple} from "./cons_list.ts"

// inspiration:
// https://dev.to/ecyrbe/how-to-use-advanced-typescript-to-define-a-pipe-function-381h
Expand Down Expand Up @@ -163,7 +163,7 @@ export type ExpandTuple<Tuple extends readonly [...any[]]> = {
} & { length: Tuple['length'] }

// convert a cons list of UPObjectStepSpecs type to a Tuple type
export type ObjectStepsTuple<Steps extends NRCons<UPObjectStepSpec>> =
export type ObjectStepsTuple<Steps extends NRConsList<UPObjectStepSpec>> =
ToTuple<UPObjectStepSpec, Steps>

// get a union of all the Requirements from a list of steps...
Expand All @@ -174,12 +174,12 @@ export type ObjectStepReqs<T extends UPObjectStepSpec> =
? R
: never
export type ObjectStepsReqsU<
List extends NRCons<UPObjectStepSpec>,
List extends NRConsList<UPObjectStepSpec>,
Acc = never> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRCons<UPObjectStepSpec>]
infer R extends NRConsList<UPObjectStepSpec>]
? ObjectStepsReqsU<R, Acc | ObjectStepReqs<F>>
: never
type ObjectStepsTupleReqsUImpl<
Expand All @@ -192,7 +192,7 @@ type ObjectStepsTupleReqsUImpl<
// setting up the recursion Service in object_chain.ts ...converting
// the list to a tuple before extracting the Requirements union seems
// to avoid that
export type ObjectStepsTupleReqsU<Steps extends NRCons<UPObjectStepSpec>> =
export type ObjectStepsTupleReqsU<Steps extends NRConsList<UPObjectStepSpec>> =
ObjectStepsTupleReqsUImpl<ObjectStepsTuple<Steps>>

// get a union of all the Errors from a list of steps
Expand All @@ -202,12 +202,12 @@ export type ObjectStepErrors<T extends UPObjectStepSpec> =
? E
: never
export type ObjectStepsErrorsU<
List extends NRCons<UPObjectStepSpec>,
List extends NRConsList<UPObjectStepSpec>,
Acc = never> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRCons<UPObjectStepSpec>]
infer R extends NRConsList<UPObjectStepSpec>]
? ObjectStepsErrorsU<R, Acc | ObjectStepErrors<F>>
: never

Expand All @@ -218,13 +218,13 @@ export type ObjectStepInput<T extends UPObjectStepSpec> =
? A
: never
export type ObjectStepsInputTuple<
List extends NRCons<UPObjectStepSpec>,
List extends NRConsList<UPObjectStepSpec>,
// deno-lint-ignore no-explicit-any
Acc extends readonly any[] = []> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRCons<UPObjectStepSpec>]
infer R extends NRConsList<UPObjectStepSpec>]
? ObjectStepsInputTuple<R, readonly [...Acc, ObjectStepInput<F>]>
: never

Expand All @@ -235,13 +235,13 @@ export type ObjectStepValue<T extends UPObjectStepSpec> =
? V
: never
export type ObjectStepsValueTuple<
List extends NRCons<UPObjectStepSpec>,
List extends NRConsList<UPObjectStepSpec>,
// deno-lint-ignore no-explicit-any
Acc extends readonly any[] = []> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRCons<UPObjectStepSpec>]
infer R extends NRConsList<UPObjectStepSpec>]
? ObjectStepsValueTuple<R, readonly [...Acc, ObjectStepValue<F>]>
: never

Expand All @@ -261,7 +261,7 @@ export type ObjectStepsValueTuple<
// 2. it's safe to use never in the else branches. they will not be hit

export type ObjectChainSteps<
Specs extends NRCons<UPObjectStepSpec>,
Specs extends NRConsList<UPObjectStepSpec>,
ObjAcc> =

// case: no more specs
Expand All @@ -270,7 +270,7 @@ export type ObjectChainSteps<

// case: there are more specs - add a property to ObjAcc and recurse
: Specs extends readonly [infer First,
infer Rest extends NRCons<UPObjectStepSpec>]
infer Rest extends NRConsList<UPObjectStepSpec>]
? First extends UCFxObjectStepSpec<
infer FK, infer _FA, infer _FD1, infer FD2,
infer FR, infer FE, infer FV>
Expand All @@ -285,7 +285,7 @@ export type ObjectChainSteps<

// get the final Object result type from a list of ObjectStepSpecs
export type ObjectChainStepsReturn<
Specs extends NRCons<UPObjectStepSpec>,
Specs extends NRConsList<UPObjectStepSpec>,
ObjAcc> =
Specs extends None
? ObjAcc // empty specs returns the input
Expand All @@ -304,7 +304,7 @@ export type ObjectChainStepsReturn<
// other type params
export function objectChainStepsProg<Obj>() {

return function <const Specs extends NRCons<UPObjectStepSpec>>
return function <const Specs extends NRConsList<UPObjectStepSpec>>
(objectStepSpecs:
ObjectChainSteps<Specs, Obj> extends Specs
? Specs
Expand Down

0 comments on commit fb822bb

Please sign in to comment.