wslでelixir その116

# 概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題、やってみた。

# 練習問題
Livebookで論理回路の真理値表を作成せよ。

# 方針
- 組み合わせ回路の真理値表
- インタープリター方式
- xorの場合
make 3
in 0 1
out 2 2
xor 0 1 2
出力
0 1 2
0 0 0
0 1 1
1 0 1
1 1 0
- kino.js使う。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/bf832c49-9ef1-7c86-bffd-b290a25e9eef.png)

# セットアップ

```
Mix.install([
  {:kino, "~> 0.10.0"}
])

```


# サンプルコード

```
defmodule KinoOhi.Table do
  use Kino.JS
  def new(html) do
    Kino.JS.new(__MODULE__, html)
  end

  asset "main.js" do
    """
    var c0;

    class C {
    constructor(n) {
    this.h = "";
    this.n = n;
    this.s = [];
    var t = new Array(n);
    for (var i = 0; i < n; i++)
    {
    t[i] = 0;
    }
    this.s.push(t);
    return this;
    }
    init(a, b) {
    b = b + 1;
    for (var i = a; i < b; i++)
    {
    var t = [];
    for (var j = 0; j < this.s.length; j++)
    {
      var u = this.s[j];
      u[i] = 0;
      t.push(u.slice());
      u[i] = 1;
      t.push(u);
    }
    this.s = t;
    }
    return this;
    }
    xor(a, b, c) {
    for (var j = 0; j < this.s.length; j++)
    {
     this.s[j][c] = this.s[j][a] ^ this.s[j][b];
    }
    return this;
    }
    nand(a, b, c) {
    for (var j = 0; j < this.s.length; j++)
    {
      var s = this.s[j][a] & this.s[j][b];
      if (s == 0)
      {
        this.s[j][c] = 1;
      }
      else
      {
        this.s[j][c] = 0;
      }
    }
    return this;
    }
    nor(a, b, c) {
    for (var j = 0; j < this.s.length; j++)
    {
      var s = this.s[j][a] | this.s[j][b];
      if (s == 0)
      {
        this.s[j][c] = 1;
      }
      else
      {
        this.s[j][c] = 0;
      }
    }
    return this;
    }
    not(a, b) {
    for (var j = 0; j < this.s.length; j++)
    {
      var s = !this.s[j][a];
      if (s == true)
      {
        this.s[j][b] = 1;
      }
      else
      {
        this.s[j][b] = 0;
      }
    }
    return this;
    }
    and(a, b, c) {
    for (var j = 0; j < this.s.length; j++)
    {
     this.s[j][c] = this.s[j][a] & this.s[j][b];
    }
    return this;
    }
    or(a, b, c) {
    for (var j = 0; j < this.s.length; j++)
    {
     this.s[j][c] = this.s[j][a] | this.s[j][b];
    }
    return this;
    }
    get() {
    return this.s;
    }
    table() {
    this.h += '<table>';
    for (var row = 0; row < this.s.length + 1; row++)
    {
    	this.h +='<tr>';
    	for (var col = 0; col < this.n + 1; col++)
    	{
    		if (col === 0 && row === 0)
    		{
    			this.h += '<th>&nbsp;</th>';
     	}
    		else if (col === 0 && row !== 0)
     	{
      	this.h += '<th>' + row + '</th>';
      }
     	else if (row === 0)
     	{
      	this.h += '<th>' + (col - 1) + '</th>'; 
      	}
     	else
     	{
      	var v = this.s[row - 1][col- 1];
       this.h += '<td>' + v + '</td>';
      }
     }
     this.h += '</tr>';
    }
    this.h += '</table>';
    document.getElementById('out').innerHTML = this.h;
    }
    }
    function run(str) {
    var codes = str.split("\\n");  
    var len = codes.length;
    var i;
    for (i = 0; i < len; i++)
    {
    var code = codes[i].split(" ");
    switch (code[0])
    {
    case "":
    break;
    case "out":
    break;
    case "wire":
    break;
    case "make":
      var a = parseInt(code[1]);
      c0 = new C(a);
    break;
    case "in":
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      c0.init(a, b);
    break;
    case "nand":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.nand(a, b, c);
    break;
    case "xor":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.xor(a, b, c);
    break;
    case "and":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.and(a, b, c);
    break;
    case "or":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.or(a, b, c);
    break;
    case "nor":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      c0.nor(a, b, c);
    break;
    case "not":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      c0.not(a, b);
    break;
    default:
      alert(code[0]);
    break;
    }
    }
    c0.table();
    }
    export function init(ctx, html) {
      ctx.importCSS("main.css");
    	const out0 = document.createElement("div");
    out0.id = "out";
    out0.style.width = "100%";
    out0.style.height = "300px";
    ctx.root.appendChild(out0);
      run(html);
    }
    """
  end

  asset "main.css" do
    """
    table {
      border-collapse: collapse;
    }
    td,th {
      width: 30px;
      border: 1px solid #000;
    }
    td {
      text-align: center;
    }
    th {
      background-color: #ccc;
    }
    """
  end
end

KinoOhi.Table.new("""
make 10
in 0 2
wire 3 8
out 9 9
not 2 3
and 0 1 4
and 1 2 5
not 0 6
and 3 4 7
and 5 6 8
or 7 8 9
""")
```


以上。