lean-ja / lean-ja/lean-by-example

`Array.reshape` 関数を Vector にする

Open
#2,480 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

付録アイデア
Dominant language
Lean
Stars
188
Forks
15
Avg merge
9h 8m
Merged PRs (30d)
6

Description

これを Vector α (m * n) からの関数にしたくなるのが人情であるが、これが意外と大変だったりする。

実際、こういう関数を実装したいときはどうするのが良いのだろうか?現在のベストプラクティスを調べてみたいところ。

/-- 配列を二次元配列に変換する。
サイズが期待と異なる場合は `panic!` を呼ぶ -/
def Array.reshape (m n : Nat) (xs : Array α) : Array (Array α) :=
  if xs.size ≠ m * n then
    panic! s!"{decl_name%}: size mismatch"
  else
    Array.range m |>.map (fun i => xs.extract (n * i) (n * (i + 1)))

#guard
  let actual := Array.reshape (m := 2) (n := 3) #[1, 2, 3, 4, 5, 6]
  let expected := #[#[1, 2, 3], #[4, 5, 6]]
  actual == expected

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository file or test; start with the Array.reshape example and compare the requested Vector signature with the existing Array behavior. Determine the current best practice for handling the size mismatch and confirm that the provided guard example still expresses the expected result.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.