9
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

環境作成

Dockerを使って環境を作ります
インストールする内容

  • Python3
  • code-server
    • VSCodeをWebで動作させます
  • VSCodeの拡張機能
    • Python
Dockerfile
FROM codercom/code-server:latest

USER root
RUN apt update -y && apt install python3 -y 

USER coder

RUN code-server --install-extension ms-python.python \
    && code-server --install-extension MS-CEINTL.vscode-language-pack-ja
docker-compose.yml
services:
  code-server:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: code-server
    ports:
      - "8080:8080"
    volumes:
      - "./config:/home/coder/.config"
      - "./project:/home/coder/project"
    environment:
      - PASSWORD=1111
      - TZ=Asia/Tokyo

Docker実行

$ docker-compose up -d

http://127.0.0.1:8080/にアクセス
パスワードを聞かれるのでdocker-compose.ymlPASSWORDで設定した文字を入力してください

ログインできると下記の画面になります
image.png

あとはVSCodeと使い方は同じです

Elixirのパイプの再現はできるか?

結論 標準の機能では無理のようです
ライブラリーがあるようですが、逸脱するので今回は使いません

お題:スカイライン GT-Rを取得せよ

  • 車の型式
  • Rが付くのもを取得
  • 35を除外
  • 並び替え
  • SKYLINE GT-R {型式}に変換
Elixir
~w"A70 R33 A80 A90 R35 R32 R34 AW11 S13 S14 S15"
|> Enum.filter(fn x -> String.contains?(x, "R") end)
|> Enum.reject(fn x -> String.contains?(x, "35") end)
|> Enum.sort()
|> Enum.map(fn x -> "SKYLINE GT-R #{x}" end)

# 結果
["SKYLINE GT-R R32", "SKYLINE GT-R R33", "SKYLINE GT-R R34"]

Python
cars = "A70 R33 A80 A90 R35 R32 R34 AW11 S13 S14 S15".split()

result = sorted([
    f"SKYLINE GT-R {x}" 
    for x in cars 
    if "R" in x and "35" not in x
])

print(result)

# 結果
['SKYLINE GT-R R32', 'SKYLINE GT-R R33', 'SKYLINE GT-R R34']

Pythonではリストの内包表記で表現できます

Elixirの関数パターンマッチの再現はできるか?

結論 関数の定義で直接のパターンマッチは無理
だが、matchを使えばできる

お題: 二つの入力値でOK、NGの判定をする

  • 1, 2 OK
  • 2, 3 OK
  • 上記以外 NG
Elixir
defmodule Ymn2 do
  def next(1, 2), do: "OK"
  def next(2, 3), do: "OK"
  def next(_, _), do: "NG"
end

Ymn2.next(1, 2)
|> IO.inspect()

Ymn2.next(2, 3)
|> IO.inspect()

Ymn2.next(2, 1)
|> IO.inspect()

# 結果
"OK"
"OK"
"NG"
Python
def next(a, b):
    match (a, b):
            case (1, 2):
                return "OK"
            case (2, 3):
                return "OK"
            case _:
                return "NG"

print(next(1, 2))
print(next(2, 3))
print(next(2, 1))

# 結果
OK
OK
NG

match

感想

  • 言語ごと文化があるようなので無理にElixirに合わせない方が良い
  • 可能な限り同じ動作ならPythonの文化に合わせてみました
9
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?