Last active
January 18, 2024 17:46
-
-
Save jkuester/8bbdec8c279dcf12fb61f611d8db7652 to your computer and use it in GitHub Desktop.
Comparison of oct-numbers without/with effect-ts
This file contains 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
const CHAR_DICT: Record<string, string> = { | |
' _ | ||_| ': '0', | |
' | | ': '1', | |
' _ _||_ ': '2', | |
' _ _| _| ': '3', | |
' |_| | ': '4', | |
' _ |_ _| ': '5', | |
' _ |_ |_| ': '6', | |
' _ | | ': '7', | |
' _ |_||_| ': '8', | |
' _ |_| _| ': '9', | |
}; | |
const getLines = (input: string): string[] => input.split('\n'); | |
const getShapes = (row: string[]) => { | |
const grids: string[][] = []; | |
for (let i = 0; i < row[0].length; i += 3) { | |
const grid = row.map((line) => line.slice(i, i + 3)); | |
grids.push(grid); | |
} | |
return grids.map((grid) => grid.join('')); | |
}; | |
const getRows = (input: string[]) => { | |
const rows: string[][] = []; | |
for (let i = 0; i < input.length; i += 4) { | |
const grid = input.slice(i, i + 4); | |
rows.push(grid); | |
} | |
return rows; | |
}; | |
const getChar = (shape: string): string => CHAR_DICT[shape] || '?'; | |
export function convert(input: string): string { | |
const lines = getLines(input); | |
return getRows(lines) | |
.map(getShapes) | |
.map(shapes => shapes.map(getChar)) | |
.map(chars => chars.join('')) | |
.join(','); | |
}; |
This file contains 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
import { pipe } from "effect"; | |
import * as S from "effect/String" | |
import * as ReadonlyArray from "effect/ReadonlyArray" | |
import * as ReadonlyRecord from "effect/ReadonlyRecord" | |
import * as Function from "effect/Function" | |
import * as Option from "effect/Option" | |
type Matrix<T> = Array<Array<T>>; | |
const CHAR_DICT: Record<string, string> = { | |
' _ | ||_| ': '0', | |
' | | ': '1', | |
' _ _||_ ': '2', | |
' _ _| _| ': '3', | |
' |_| | ': '4', | |
' _ |_ _| ': '5', | |
' _ |_ |_| ': '6', | |
' _ | | ': '7', | |
' _ |_||_| ': '8', | |
' _ |_| _| ': '9', | |
}; | |
const getLines = (input: string): string[] => pipe( | |
input, | |
S.split('\n') | |
); | |
const getMatrixColumn = <T>(matrix: Matrix<T>) => (index: number) => pipe( | |
matrix, | |
ReadonlyArray.map(r => r[index]) | |
); | |
const rotateMatrix = <T>(matrix: Matrix<T>) => pipe( | |
matrix, | |
ReadonlyArray.get(0), | |
Option.getOrElse(() => []), | |
ReadonlyArray.length, | |
length => ReadonlyArray.makeBy(length, Function.identity), | |
ReadonlyArray.map(getMatrixColumn(matrix)) | |
); | |
const chunkMatrixRows = (chunkSize: number) => <T>(matrix: Matrix<T>) => pipe( | |
matrix, | |
ReadonlyArray.map(s => pipe( | |
s, | |
ReadonlyArray.chunksOf(chunkSize) | |
)) | |
); | |
const flattenStringMatrix = (matrix: Matrix<string>) => pipe( | |
matrix, | |
ReadonlyArray.flatten, | |
ReadonlyArray.join('') | |
); | |
const getShapes = (row: string[]): string[] => pipe( | |
row, | |
ReadonlyArray.map(ReadonlyArray.fromIterable), | |
chunkMatrixRows(3), | |
rotateMatrix, | |
ReadonlyArray.map(flattenStringMatrix) | |
); | |
const getRows = (input: string[]) => pipe( | |
input, | |
ReadonlyArray.chunksOf(4) | |
); | |
const getChar = (shape: string) => pipe( | |
CHAR_DICT, | |
ReadonlyRecord.get(shape), | |
Option.getOrElse(() => '?') | |
); | |
const getRowChars = (row: string[]) => pipe( | |
row, | |
getShapes, | |
ReadonlyArray.map(getChar), | |
ReadonlyArray.join('') | |
); | |
export const convert = (input: string): string => pipe( | |
input, | |
getLines, | |
getRows, | |
ReadonlyArray.map(getRowChars), | |
ReadonlyArray.join(',') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment