Last active
June 3, 2022 00:20
-
-
Save masakih/2e3fb8d86e56a79638f37a57ad0b7556 to your computer and use it in GitHub Desktop.
teratailのやつ
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
| func setBit(_ x: Int, _ n: Int) -> Int { ... } | |
| func unsetBit(_ x: Int, _ n: Int) -> Int { ... } | |
| func reverseBit(_ x: Int, _ n: Int) -> Int { ... } | |
| func print8Bit(_ x: Int) { ... } | |
| // 標準入力から整数を読み込む | |
| // 整数以外であれば例外 | |
| func readInt() throws -> Int { ... } | |
| do { | |
| let bitFunctions = [setBit, unsetBit, reverseBit] | |
| var digit = 0 | |
| try (0...).lazy | |
| .map { _ in | |
| print("モードを入力(0: セット、 1: アンセット、 2: 反転):") | |
| return try readInt() | |
| } | |
| .prefix { m in (0..2).contains(m) } | |
| .map { m in | |
| print("何ビット目かを入力(0~7):") | |
| return (m, try readInt()) | |
| } | |
| .prefix { (m, n) in (0...7).contains(n) } | |
| .forEach { (m, n) in | |
| digit = bitFunctions[m](digit, n) | |
| print8Bit(digit) | |
| } | |
| } | |
| catch { | |
| // do nothing | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment