Skip to content

Instantly share code, notes, and snippets.

@omas-public
Last active July 31, 2023 09:53
Show Gist options
  • Select an option

  • Save omas-public/81e3db9821f1ffac2ff3e93322066e41 to your computer and use it in GitHub Desktop.

Select an option

Save omas-public/81e3db9821f1ffac2ff3e93322066e41 to your computer and use it in GitHub Desktop.
const identity = v => v
const join = (sep = '\n') => array => array.join(sep)
const split = (sep = ' ') => string => string.split(sep)
const seq = (size, start) => [...Array(size)].map((_, i) => i + start)
const inputs = (file = '/dev/stdin') => {
const stream = require('fs').readFileSync(file, 'utf-8').trim()
const toArray = sep => fn => iter => Array.from(split(sep)(iter), fn)
return {
readCols: (fn = identity) => toArray(' ')(fn)(stream),
readRows: (fn = identity) => toArray('\n')(fn)(stream),
readStream: (fn = identity) => fn(stream),
readMatrix: (fn = identity) => toArray('\n')(identity)(stream)
.map(toArray(' ')(fn))
}
}
const outputs = v => {
const _print = fn => v => console.log(fn(v))
return {
toLine: () => _print(identity)(v),
toCols: () => _print(join(' '))(v),
toRows: () => _print(join('\n'))(v),
toJSON: () => _print(JSON.stringify)(v),
toMatrix: () => _print(join('\n'))(v.map(join(' ')))
}
}
const product = (...iters) => iters.reduce(
(a, b) => a.flatMap(d => b.map(e => [d, e].flat())))
// 即時実行関数(main)
(() => {
// define function
const fn = (N, M) => {
// ここに処理を書く
return [seq(N, 1), seq(M, 1)]
}
// declare varriable
const [N, M] = inputs().readCols(Number)
// processing
const result = fn(N, M)
// display
outputs(result).toJSON()
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment