-
Notifications
You must be signed in to change notification settings - Fork 4
Cse machine core #52
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
Cse machine core #52
Changes from all commits
Commits
Show all changes
4 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
Binary file not shown.
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,112 @@ | ||
| import * as es from 'estree' | ||
| import { isImportDeclaration, getModuleDeclarationSource } from './utils'; | ||
|
|
||
| /** | ||
| * Python style dictionary | ||
| */ | ||
| export default class Dict<K, V> { | ||
| constructor(private readonly internalMap = new Map<K, V>()) {} | ||
|
|
||
| public get size() { | ||
| return this.internalMap.size | ||
| } | ||
|
|
||
| public [Symbol.iterator]() { | ||
| return this.internalMap[Symbol.iterator]() | ||
| } | ||
|
|
||
| public get(key: K) { | ||
| return this.internalMap.get(key) | ||
| } | ||
|
|
||
| public set(key: K, value: V) { | ||
| return this.internalMap.set(key, value) | ||
| } | ||
|
|
||
| public has(key: K) { | ||
| return this.internalMap.has(key) | ||
| } | ||
|
|
||
| /** | ||
| * Similar to how the python dictionary's setdefault function works: | ||
| * If the key is not present, it is set to the given value, then that value is returned | ||
| * Otherwise, `setdefault` returns the value stored in the dictionary without | ||
| * modifying it | ||
| */ | ||
| public setdefault(key: K, value: V) { | ||
| if (!this.has(key)) { | ||
| this.set(key, value) | ||
| } | ||
|
|
||
| return this.get(key)! | ||
| } | ||
|
|
||
| public update(key: K, defaultVal: V, updater: (oldV: V) => V) { | ||
| const value = this.setdefault(key, defaultVal) | ||
| const newValue = updater(value) | ||
| this.set(key, newValue) | ||
| return newValue | ||
| } | ||
|
|
||
| public entries() { | ||
| return [...this.internalMap.entries()] | ||
| } | ||
|
|
||
| public forEach(func: (key: K, value: V) => void) { | ||
| this.internalMap.forEach((v, k) => func(k, v)) | ||
| } | ||
|
|
||
| /** | ||
| * Similar to `mapAsync`, but for an async mapping function that does not return any value | ||
| */ | ||
| public async forEachAsync(func: (k: K, v: V, index: number) => Promise<void>): Promise<void> { | ||
| await Promise.all(this.map((key, value, i) => func(key, value, i))) | ||
| } | ||
|
|
||
| public map<T>(func: (key: K, value: V, index: number) => T) { | ||
| return this.entries().map(([k, v], i) => func(k, v, i)) | ||
| } | ||
|
|
||
| /** | ||
| * Using a mapping function that returns a promise, transform a map | ||
| * to another map with different keys and values. All calls to the mapping function | ||
| * execute asynchronously | ||
| */ | ||
| public mapAsync<U>(func: (key: K, value: V, index: number) => Promise<U>) { | ||
| return Promise.all(this.map((key, value, i) => func(key, value, i))) | ||
| } | ||
|
|
||
| public flatMap<U>(func: (key: K, value: V, index: number) => U[]) { | ||
| return this.entries().flatMap(([k, v], i) => func(k, v, i)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convenience class for maps that store an array of values | ||
| */ | ||
| export class ArrayMap<K, V> extends Dict<K, V[]> { | ||
| public add(key: K, item: V) { | ||
| this.setdefault(key, []).push(item) | ||
| } | ||
| } | ||
|
|
||
| export function filterImportDeclarations({ | ||
| body | ||
| }: es.Program): [ | ||
| ArrayMap<string, es.ImportDeclaration>, | ||
| Exclude<es.Program['body'][0], es.ImportDeclaration>[] | ||
| ] { | ||
| return body.reduce( | ||
| ([importNodes, otherNodes], node) => { | ||
| if (!isImportDeclaration(node)) return [importNodes, [...otherNodes, node]] | ||
|
|
||
| const moduleName = getModuleDeclarationSource(node) | ||
| importNodes.add(moduleName, node) | ||
| return [importNodes, otherNodes] | ||
| }, | ||
| [new ArrayMap(), []] as [ | ||
| ArrayMap<string, es.ImportDeclaration>, | ||
| Exclude<es.Program['body'][0], es.ImportDeclaration>[] | ||
| ] | ||
| ) | ||
| } | ||
Oops, something went wrong.
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.
Note that Python dictionaries guarantee insertion order since 3.6 in the language documentation.
If I understand correctly, map also maintains insertion order on iteration. So this should be fine I hope.