-
Notifications
You must be signed in to change notification settings - Fork 2
feat: List scalars #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import test from 'ava'; | ||
|
||
import { Int64 } from './int64.js'; | ||
import { List } from './list.js'; | ||
|
||
test('list', (t) => { | ||
const l = new List(Int64); | ||
t.deepEqual(l, new List(Int64)); | ||
|
||
l.value = [new Int64(17), new Int64(19), new Int64(null)]; | ||
t.is(l.length, 3); | ||
}); | ||
|
||
test('list equality', (t) => { | ||
const l1 = new List(Int64); | ||
l1.value = [new Int64(17), new Int64(19), new Int64(null)]; | ||
|
||
const l2 = new List(Int64); | ||
l2.value = [new Int64(17), new Int64(19), new Int64(null)]; | ||
|
||
t.deepEqual(l1, l2); | ||
}); | ||
|
||
test('list inequality', (t) => { | ||
const l1 = new List(Int64); | ||
l1.value = new Int64(4); | ||
|
||
const l2 = new List(Int64); | ||
l2.value = new Int64(7); | ||
|
||
t.notDeepEqual(l1, l2); | ||
}); | ||
|
||
test('list equality when invalid', (t) => { | ||
const l1 = new List(Int64); | ||
l1.value = new Int64(null); | ||
|
||
const l2 = new List(Int64); | ||
l2.value = new Int64(null); | ||
|
||
t.deepEqual(l1, l2); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { DataType, List as ArrowList } from '@apache-arrow/esnext-esm'; | ||
|
||
import { Scalar } from './scalar.js'; | ||
import { isInvalid, NULL_VALUE } from './util.js'; | ||
|
||
type TVector<T extends Scalar<unknown>> = T[]; | ||
|
||
export class List<T extends Scalar<unknown>> implements Scalar<TVector<T>> { | ||
private _type: new (value?: unknown) => T; | ||
private _valid = false; | ||
private _value: TVector<T> = []; | ||
|
||
constructor(scalarType: new (value?: unknown) => T, initialValue?: unknown) { | ||
this._type = scalarType; | ||
if (!isInvalid(initialValue)) this.value = initialValue; | ||
} | ||
|
||
get dataType(): DataType { | ||
return new ArrowList(this._type.prototype.dataType); | ||
} | ||
|
||
set value(inputValue: unknown) { | ||
if (isInvalid(inputValue)) { | ||
this._valid = false; | ||
this._value = []; | ||
return; | ||
} | ||
|
||
const inputArray = Array.isArray(inputValue) ? inputValue : [inputValue]; | ||
const temporaryVector: TVector<T> = []; | ||
|
||
if (inputArray.length > 0) { | ||
const firstItemScalar = new this._type(); | ||
firstItemScalar.value = inputArray[0]; | ||
const firstItemType = Object.getPrototypeOf(firstItemScalar).constructor; | ||
|
||
for (const item of inputArray) { | ||
const scalar = new this._type(); | ||
scalar.value = item; | ||
|
||
if (Object.getPrototypeOf(scalar).constructor !== firstItemType) { | ||
throw new Error( | ||
`Type mismatch: All items should be of the same type as the first item. Expected type ${ | ||
firstItemType.name | ||
}, but got ${Object.getPrototypeOf(scalar).constructor.name}`, | ||
); | ||
} | ||
|
||
temporaryVector.push(scalar); | ||
} | ||
|
||
this._value = temporaryVector; | ||
this._valid = true; // List becomes valid if we successfully process values | ||
return; | ||
} | ||
|
||
this._valid = true; // An empty list is valid | ||
} | ||
|
||
get valid(): boolean { | ||
return this._valid; | ||
} | ||
|
||
get value(): TVector<T> { | ||
return this._value; | ||
} | ||
|
||
toString(): string { | ||
if (!this._valid) { | ||
return NULL_VALUE; | ||
} | ||
return `[${this._value.map((v) => v.toString()).join(', ')}]`; | ||
} | ||
|
||
get length(): number { | ||
return this._value.length; | ||
} | ||
|
||
// If you need an equality method, you can add an equals method similar to the Python __eq__ | ||
equals(other: List<T>): boolean { | ||
if (!other) return false; | ||
if (this.constructor !== other.constructor) return false; | ||
if (this._valid !== other.valid) return false; | ||
return JSON.stringify(this._value) === JSON.stringify(other.value); // crude equality check for objects, might need refinement. | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TVector because
Vector
is:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explanation: Difference between
TVector
andVector
typesTVector
TVector
.TVector<Int64>
, then it's equivalent toInt64[]
.TVector<Bool>
, then it's equivalent toBool[]
.TVector
.Vector
Vector
is an array ofScalar<unknown>
.Vector
; it's always an array ofScalar<unknown>
.Summary
TVector
provides flexibility on the array item's type, letting you specify it when using the type.Vector
is fixed to always be an array ofScalar<unknown>
.