[話者認識] 基礎

話者データを学習し、どの人のデータなのか機械学習で予測する
Libraryにmatplotlib, numpy, scikit-learn, libROSA、
識別にSVM、フーリエ変換、MFCCなどを使う

SVM
L 境界線(直線・曲線)を作り、データがどちら側か判定する
  L 境界線付近をサポートベクトルと呼ぶ

import numpy as np
import matplotlib.pyplot as plt
import random
from sklearn import svm

N = 15
random.seed(11813)
train_X = np.array([[random.randint(0,100), random.randint(0, 100)] for i in range(N)])

for i in range(len(train_X)):
	plt.plot(train_X[i][0], train_X[i][1],'o', color='blue')
	plt.annotate(i, (train_X[i][0], train_X[i][1]), size=20)
plt.savefig('test.jpg',dpi=100)

分類する

train_y = np.array([0 for i in range(N)])
train_y[2] = train_y[3] = train_y[1] = train_y[4] = train_y[5] = train_y[6] = train_y[11] = 1

colors = ['blue','red']
for i in range(len(train_X)):
	plt.plot(train_X[i][0], train_X[i][1],'o', color=colors[train_y[i]])
	plt.annotate(i, (train_X[i][0], train_X[i][1]), size=20)
plt.savefig('test.jpg',dpi=100)
test_X = np.array([[30,60]])

plt.plot(test_X[0][0], test_X[0][1],'x', color="black")
plt.annotate('test', (test_X[0][0], test_X[0][1]), size=20)

plt.savefig('test.jpg',dpi=100)

clf = svm.SVC(gamma=0.0001, C=1)
clf.fit(train_X, train_y)

test_y = clf.predict(test_X)
print(test_y)

なるほど、SVMが何をやってるのかは理解できた。

[TypeScript] コマンドライン引数から緯度・経度から距離を測定したい

$ npm install @turf/distance
$ npm install @turf/helpers

index.ts

import distance from '@turf/distance'
import { point } from '@turf/helpers'

const tokyo = point([139.69198077115493, 35.68978025468156])
const osaka = point([135.51980022509665, 34.6865543538711])
const result = distance(tokyo, osaka, {units: 'kilometers'})
console.log(result)

$ node sample.js
395.1818245563336

これをコマンドライン引数で書くと

import distance from '@turf/distance'
import { point } from '@turf/helpers'

console.log("Node path = " + process.argv[0])
console.log("Script file path = " + process.argv[1])

const startLongitude = Number(process.argv[2])
const startLatitude = Number(process.argv[3])
const destLongitude = Number(process.argv[4])
const destLatitude = Number(process.argv[5])

const start = point([startLongitude, startLatitude])
const dest = point([destLongitude, destLatitude])
const result = distance(start, dest, {units: 'kilometers'})
console.log(result)

これにアロー関数を追加する

const f = (name:string, speed:number): void => {
	let time = result / speed
	console.log("時速" + speed + "km/hの" + name + "だと、" + time + "時間かかります。")
}

f("新幹線", 200)
f("高速バス", 120)

$ node sample.js 139.69198077115493 35.68978025468156 135.51980022509665 34.6865543538711
395.1818245563336
時速200km/hの新幹線だと、1.975909122781668時間かかります。
時速120km/hの高速バスだと、3.29318187130278時間かかります。

process.argvを変数に代入すると冗長になるから直接書いても良さそうだな
高速バスで東京大阪は3.2時間以上かかるから、実際のところ60km/h ってところか。

うむ、書きながら勉強になるな

djangoをherokuにdeployしたい

### Django プロジェクト作成
$ python3 –version
Python 3.8.10
$ pip3 –version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
$ sudo apt install python3-django
$ pip3 install dj-database-url
$ django-admin startproject newsproject
$ cd newsproject
$ python3 manage.py startapp newsapp
templatesフォルダ作成
newsproject/settings.py

ALLOWED_HOSTS = ["192.168.34.10"]

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'newsapp',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR, 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

newsproject/urls.py

from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('newsapp.urls')),
]

newsapp/urls.py

from django.urls import path
from .views import Create, listfunc

urlpatterns = [
	path('', Create.as_view(), name='home'),
	path('list/', listfunc, name='list'),
]

newsapp/views.py

from django.shortcuts import render
from .models import News
from django.views.generic import CreateView
from django.urls import reverse_lazy
import urllib.request
import requests
from bs4 import BeautifulSoup

class Create(CreateView):
	template_name = 'home.html'
	model = News
	fields = ('url'),
	success_url = reverse_lazy('list')

def listfunc(request):
	for post in News.objects.all():
		url = post.url
	list = []
	response = requests.get(url)
	bs = BeautifulSoup(response.text, "html.parser")
	ul_tag = bs.find_all(class_="topicsList_main")
	for tag in ul_tag[0]:
		title = tag.a.getText()
		url2 = tag.a.get("href")
		list.append([title, url2])
	context = {'list': list,}
	return render(request, 'list.html', context)

newsapp/models.py

from django.db import models

class News(models.Model):
	url = models.CharField(max_length=100)

// migration
$ python3 manage.py makemigrations
$ python3 manage.py migrate

templates/base.html

<!doctype html>
<html lang="en">
 <head>
   <!-- Required meta tags -->
   <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
   <!-- Bootstrap CSS -->
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
   <title>Pachi Data</title>
 </head>
 <body>
   {% block header %}
   {% endblock header %}
   {% block content %}
   {% endblock content %}
   <!-- Optional JavaScript -->
   <!-- jQuery first, then Popper.js, then Bootstrap JS -->
   <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
   <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
   <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
 </body>
</html>

templates/home.html
[htmle]
{% extends ‘base.html’ %}

{% block header %}

News

Enter the URL

{% endblock header %}

{% block content %}

{% csrf_token %}

URL:


{% endblock content %}
[/html]

templates/list.html

{% extends 'base.html' %}

{% block header %}
<div class="jumbotron">
   <div class="container">
     <h1 class="display-4">News</h1>
   </div>
 </div>
{% endblock header %}

{% block content %}
<div class="container">
<ul class="list-group list-group-flush">
 <table border="2">
   <tr>
     <th>タイトル</th>
     <th>URL</th>
   </tr>
   {% for post, post2 in list %} 
   <tr>
     <td>{{ post }}</td>
     <td><a href="{{ post2 }}">{{ post2 }}</a></td>
   </tr>
   {% endfor %}
 </table>
</ul>
</div>
{% endblock content %}

$ python3 manage.py runserver 192.168.34.10:8000

### herokuにDeploy
$ echo web: gunicorn newsproject.wsgi –log-file – > Procfile
$ freeze > requirements.txt
$ python3 –version
Python 3.8.10
$ echo python-3.8.10 > runtime.txt
$ echo -e __pycache__\\ndb.sqlite3\\n.DS_Store\\nlocal_settings.py > .gitignore

newsproject/settings.py
L SECRET_KEYを削除する

import dj_database_url

DEBUG = False
// 省略

ALLOWED_HOSTS = ["*"]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', // 追加
]

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': '',
        'HOST': 'host',
        'PORT': '',
    }
}

try:
    from .local_settings import *
except ImportError:
    pass

if not DEBUG:
    SECRET_KEY = os.environ['SECRET_KEY']
    import django_heroku
    django_heroku.settings(locals())

df_from_env = dj_database_url.config(conn_max_age=600, ssl_require=True)
DATABAES['default'].update(db_from_env)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

newsproject/local_settings.py
L SECRET_KEYをコピーする

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = 'django-insecure-bm-*'

DATABASES = {
	'default': {
		'ENGINE' : 'django.db.backends.sqlite3',
		'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
	}
}

DEBUG = True

if not DEBUG:
    SECRET_KEY = os.environ['SECRET_KEY']

$ git init
$ git add -A
$ git commit -m “initial commit”
$ heroku login
$ heroku create
$ heroku config:set SECRET_KEY=’django-insecure-*’
$ git push heroku master

ERROR: Could not find a version that satisfies the requirement cliapp==1.20180812.1 (from versions: 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, 1.0.7, 1.0.8, 1.0.9)
remote: ERROR: No matching distribution found for cliapp==1.20180812.1

git pushできない…
なかなか難儀やな

Getting started Heroku with Ubuntu20.04, python

$ sudo snap install heroku –classic
heroku v7.59.1 from Heroku✓ installed
$ heroku login
// ブラウザにアクセス
$ git clone https://github.com/heroku/python-getting-started.git
$ cd python-getting-started
$ heroku create
$ git push heroku main
$ heroku ps:scale web=1
$ heroku open

ソースファイルを見るとdjangoで作られていますね

procfile
web: gunicorn gettingstarted.wsgi

$ heroku ps
=== web (Free): gunicorn gettingstarted.wsgi (1)
web.1: up 2021/11/28 13:00:59 +0000 (~ 10m ago)

requirements.txt

django
gunicorn
django-heroku

in local
$ pip install -r requirements.txt

うむー まとまり過ぎていて細かな手順がわからん

[TypeScript] コンソール作成

Node.jsでは実行時に渡される引数の情報はprocess.argvにまとめられている
process.argv[0] = nodeコマンドのパス
process.argv[1] = スクリプトファイルのパス
process.argv[2]以降 = コマンド実行時に渡される引数

src/index.ts

console.log("Node path = " + process.argv[0])
console.log("Script file path = " + process.argv[1])

const data: number[] = []
for(var i = 2; i < process.argv.length; i++){
	data.push(Number(process.argv[i]))
}
console.log(data)

for (let item of data){
	const res = primeFactor(item)
	console.log(item + '= ' + res)
}

function primeFactor(a: number): number[] {
	const v: number[ ] = []
	let x = a
	let n = 2
	while(x > n){
	  if(x % n == 0){
	  	x = x / n
	  	v.push(n)
	  } else {
	  	n += n == 2 ? 1 : 2
	  }
	}
	v.push(x)
	return v
}

$ npm run build
$ node dist/main.js 100 1234 9876
Node path = /usr/bin/node
Script file path = /home/vagrant/dev/hands-on/dist/main.js
[ 100, 1234, 9876 ]
100= 2,2,5,5
1234= 2,617
9876= 2,2,3,823

console.log("Node path = " + process.argv[0])
console.log("Script file path = " + process.argv[1])

const data: number[] = []
for(var i = 2; i < process.argv.length; i++){
	data.push(Number(process.argv[i]))
}
console.log('parameters: ' + data)

const f = aggregate()


for (let item of data){
	const res = f(item)
	console.log(res)
}

function aggregate(): (n:number) => [number, number, number, number, number] {
	let total = 0
	let totalp = 0
	let totalt = 0
	return (n:number):[number, number, number, number, number] => {
		total += n
		let tax = Math.floor(n - n / 1.1)
		totalp += n - tax
		totalt += tax
		return [n, tax, total, totalp, totalt]
	}
}

$ node dist/main.js 1200 1870 3058 2765 1875
Node path = /usr/bin/node
Script file path = /home/vagrant/dev/hands-on/dist/main.js
parameters: 1200,1870,3058,2765,1875
[ 1200, 109, 1200, 1091, 109 ]
[ 1870, 170, 3070, 2791, 279 ]
[ 3058, 278, 6128, 5571, 557 ]
[ 2765, 251, 8893, 8085, 808 ]
[ 1875, 170, 10768, 9790, 978 ]

なるほど、ここまでの知識を使って何か作ってみたいですね

[TypeScript] 高度な関数

### 例外処理
try, catch(e), finallyで書く
以下のスクリプトでは

const f = (arr?:any[]): void => {
    let res = 'Array: '
    for(let i of arr){
        res += String(i) + '\t'
    }
    console.log(res)
}

try {
    f(['ok', 'NG'])
    f([10, 20, 30])
    f()
} catch(e){
    console.log(e.message)
}

“arr is not iterable”

### 例外を発生させる関数

const f = (n:number):[number, Error?] => {
    if (n < 0){
        return [n, Error("negative number")]
    }
    let total = 0
    for(let i = 1; i<= n; i++)
        total += i
    return [total]
}

let[res1, err1] = f(100)
if(err1 == undefined)
    console.log(res1)
else console.log(err1)

let[res2, err2] = f(-100)
if(err2 == undefined)
    console.log(res2)
else console.log(err2)

### Errorをthrowする
throw Error

const f = (n:number):number => {
    if (n < 0){
        throw Error("negative!")
    }
    let total = 0
    for(let i = 1; i<= n; i++)
        total += i
    return total
}

let re1 = f(100)
console.log(re1)
let re2 = f(-100)
console.log(re2)

### ジェネリクス
定義の段階では型を指定せず、利用する場合に特定の型を使う
function 関数 ()引数:戻り値

function getRnd<T>(values: T[ ]): T {
    const r = Math.floor(Math.random() * values.length)
    return values[r]
}

const data1 = [0, 2, 4, 5, 6, 10]
const data2 = ['グー','チョキ','パー']
const data3 = [true, false]

for(let i = 0; i < 10; i++){
    const re1 = getRnd(data1)
    const re2 = getRnd(data2)
    const re3 = getRnd(data3)
    const res = re1 + '(' + typeof(re1) + ')\t'
        + re2 + '(' + typeof(re2) + ')\t'
        + re3 + '(' + typeof(re3) + ')'
    console.log(res)
}

### ジェネレーターと遅延評価
呼び出すごとに新しい値を取り出す関数
function* と yield(待ち状態)を作ります
nextで次の数字を呼び出す

function* fibo(n:number){
    let n1 = 0
    let n2 = 1
    for(let i = 0; i <= n; i++){
        yield n1
        let n3 = n1 + n2
        n1 = n2
        n2 = n3
    }
}

const n = 10
let fb = fibo(n)
for(let i = 0; i <= n + 3; i++){
    let ob = fb.next()
    console.log(ob.value)
}

### 非同期処理とpromise
処理を開始すると、待ち続けることなく次の処理に進んでいく
promiseは事後処理を呼び出して必要な作業を行う

function 関数(引数): Promise {
	return new Promise ((関数) => {


		関数()
	})
}
const f = (n:number, d:number): Promise<number> => {
    console.log("start:" + n)
    return new Promise((f) => {
        let total = 0
        for(let i = 1; i <= n; i++)
            total += i
        setTimeout(() => {
            f(total)
        }, d)
    })
}

const cb = (n:number) => {
    console.log("result:" + n)
}

f(10, 300).then(cb)
f(100, 200).then(cb)
f(1000, 100).then(cb)

プロミスを理解しないといけないことはわかった

[TypeScript] 関数2

### 無名関数
– 関数名がないもの

const f = function(name:string):void {
    console.log("hello, " + name)
}

f("taro")
f("hanako")

### アロー関数
(引数): 戻り値 => 実行する処理

const f = (name:string):void => {
    console.log("hello, " + name)
}

console.log(typeof(f)) とすると “function” がreturnとなる

### 内部関数
関数の中でしか使えない関数

const f = (n:number)=> {
    const inF = (n:number):void => {
        console.log("value:" + n)
    }
    let total = 0
    for (let i = 1; i<= n; i++){
        total += i
        inF(total)
    }
}
f(10)

### 引数に関数を使う

const func = (n:number, f:Function):void => {
    let res = f(n)
    console.log("Result:" + res)
}

const double = (n:number) => n * 2
const total = (n:number) => {
    let total = 0
    for (let i = 1; i <= n; i++)
        total += i
    return total
}

const num = 100
func(num, double)
func(num, total)

### 数値とテキストの関数を引数にする
charAtは「●文字目」を得るもの

const func = (n:number, f:(n:number)=>number|string):void => {
    let res = f(n)
    console.log("Result:" + res)
}

const double = (n:number) => n * 2
const word = (n:number):string => {
    const w = ['〇','一','二','三','四','五','六','七','八','九']
    const s = String(n)
    let res:String[] = []
    for(let i = 0; i< s.length; i++){
        let c = s.charAt(i)
        res.push(w[Number(c)])
    }
    return res.join('')
}

const num = 1230
func(num, double)
func(num, word)

### 戻り値に関数を使う

const f = (tax:number):(n:number)=> number => {
    return (n:number)=> n * (1 +tax)
}

const f1 = f(0.1)
const f2 = f(0.8)

const price = 123400
console.log(f1(price))
console.log(f2(price))

### クロージャー
定義された環境を保ち、その中で動く関数をクロージャという

const f = (n:number):() => number => {
    let count:number = 0
    return ():number => {
        count += n
        return count
    }
}

const f1 = f(1)
const f2 = f(2)
const f3 = f(3)

for(let i = 0; i < 10; i++){
    console.log(f1() + '\t' + f2() + '\t' + f3())
}

ちょっと難しくなってきた

[TypeScript] 関数1

function hello(name:string) {
    console.log("Hello " + name + "!")
}

hello("taro")
hello("hanako")

引数に型を指定する

### 変数のスコープ
varは全体で利用可、letは宣言した構文内
– var

function total(max:number){
    var num = 0
    for(var i = 1; i < max; i++){
        num += i
    }
    console.log("total:" + (num + i))
}

total(100)
total(200)
total(300)

– letにするとエラーになる

let num = 0
    for(let i = 1; i < max; i++){
        num += i
    }
    console.log("total:" + (num + i))

Cannot find name ‘i’.

### 戻り値
– 関数の処理実行後に返される値を戻り値という
– 何も戻さない場合はvoidを指定する

function total(max:number):number{
    let num = 0
    for(let i = 1; i < max; i++){
        num += i
    }
    return num
}

function printTotal(n:number):void {
    let res = total(n)
    console.log(n + "までの合計:" + res)
}

printTotal(123)
printTotal(1234)
printTotal(12345)

### 複数の値を戻す
タプルを使って複数の値の型を記述する

function calcTax(price:number):[price:number, tax:number]{
    const p = price / 1.1
    const t = price - p
    return [p, t]  
}

function printTax(price:number): void {
    const [pr, tx] = calcTax(price)
    console.log(price + "の本体価格:" + pr + ", 税額:" + tx)
}

printTax(2750)
printTax(3080)

### 引数に条件型

function printPerson(id:number | string, name:string, age:number):void{
    switch(typeof(id)){
        case 'string':
        console.log('your id is "' + id + '".')
        break
        case 'number':
        console.log("No," + id)
        break
        default:
        console.log('wrong id ...')
    }
    console.log('Name:' + name + '(' + age + ')')
}

printPerson(10, "taro", 39)
printPerson("flower", "hanako", 20)

### オプション引数の関数
オプション引数はnullを許容する

function printPerson(name?:string, age?:number):void{
    const nameval = name ? name : "no-name"
    const ageval = age ? String(age) : '-'
    console.log('Name:' + nameval + '(' + ageval + ')')
}

printPerson("taro", 39)
printPerson("hanako")
printPerson()

– 引数に初期値を入れる方法も一般的

### 可変長引数

const f = (...data:number[]):number => {
    let total = 0
    for (let i of data){
        total += i
    }
    return total
}

console.log(f(1,2,3,4,5))
console.log(f(10,20,30,40,50,60,70))
console.log(f(123,456,67,89))

なるほど、この数時間でプログラミングの理解がかなり進んだ

[TypeScript] 基礎文法2

### 配列
– 配列の作り方は[valu1, value2, …] と new Array()がある

const data = [10, 20, 30]
const total = data[0] + data[1] + data[2]
console.log('total:' + total)

– 変更不可の配列

let data2:readonly number[] = [10, 20, 30]
data2[0] = 100

Index signature in type ‘readonly number[]’ only permits reading.

– 配列の場合は for(let variables of value) を使う

const data = [100, 98, 76, 59, 87]
let total = 0
for (let item of data){
    total += item
}

const av = total / data.length
console.log('total:' + total)
console.log('average:' + av)

– 配列の要素の操作
L 最初に追加
data.unshift(value)
L 最初を削除
data.shift()
L 最後に追加
data.push(value)
L 最後を削除
data.pop()

let data:any = [10, 20, 30, 40, 50]

console.log(data)
for(let i = 0; i < 5; i++){
    data.pop()
    data.unshift('⭐️')
    console.log(data)
}

### タプル
変数の型を指定することで、異なる型も入れることができる

let me:[string, number]
let you:[string, number]

me = ['taro', 39]
you = ['hanako', 'hanako@gmail.com']

### enum
{}に用意した値しか設定できない。それ以外の値は使えない

enum janken {goo, choki, paa}

const you = janken.goo

switch(you) {
    case janken.goo:
    console.log('あいこ')
    break
    case janken.choki:
    console.log('win')
    break
    case janken.paa:
    console.log('lose')
    break    
}

### 型エイリアス
型に別名を入れることでタプルをわかりやすくする

type name = string
type age = number

let me:[name, age]
let you:[age, name]

me = ['taro', 39]
you = [29, 'hanako']

console.log(me)
console.log(you)

typeで型を指定

type name = string
type mail = string
type age = number
type person = [name, mail, age]

const taro:person = ['taro', 'taro@gmail.com', 39]
const hanako:person = ['hanako', 'hanako@gmail.com', 24]
const sachiko:person = ['sachiko', 'sachiko@gmail.com', 17]

const data:person[] = [taro, hanako, sachiko]

for(let item of data){
    console.log(item)
}

リテラル型
L リテラルとは直接ソースコードに書かれる値
L 指定した値しかない型

type hello = "hello"
type by = "bye"
type name = string

const taro:name = "taro"
const msg1:hello = "hello"
console.log(msg1 + ", " + taro)
const hanako:name = "hanako"
const msg2:by = "bye"
console.log(msg2 + ", " + hanako)

conditional types
L “|”を使うことで、enumと同じような使い方ができる

type msg = "hello" | "bye"
type name = string

const taro:name = "taro"
const msg1:msg = "hello"
console.log(msg1 + ", " + taro)
const hanako:name = "hanako"
const msg2:msg = "bye"
console.log(msg2 + ", " + hanako)

### 型チェック
– typeofで型チェックを行う

type id = number | string

const idA:id = "taro"
const idB:id = 123

const tp = idA

switch(typeof(tp)){
    case "number":
    console.log(tp + " is number")
    break
    case "string":
    console.log(tp + " is string")
    break
    default:
    console.log("type is undefined")
}

### ユーティリティ型
性質を付加する特殊な型
Readonly<●●>は総称型と呼ばれる機能

type data = [string, number]
type ReqData = Readonly<data>

const x:data = ["taro", 39]
const y:ReqData = ["hanako", 28]

x[1] = 28
y[1] = 17

Cannot assign to ‘1’ because it is a read-only property.

### シンボル
全ての値がユニークであることを保証される型
variable = Symbol(value)

const a1:string = "ok"
const b1:string = "ok"

console.log(a1 == b1)
console.log(a1 === b1)

const a2:unique symbol = Symbol("ok")
const b2:unique symbol = Symbol("ok")

console.log(a2 == b2)
console.log(a2 === b2)

This condition will always return ‘false’ since the types ‘typeof a2’ and ‘typeof b2’ have no overlap.
This condition will always return ‘false’ since the types ‘unique symbol’ and ‘string’ have no overlap.

### nullかも知れない値
値がないことを許容するために”?”をつける
絶対にnullではない場合、”!”をつける

type data = [name:string, age?:number]

const taro:data = ["taro", 39]
const hanako:data = ["hanako"]
console.log(taro)
console.log(hanako)

大分味が出てきた

[TypeScript] 基礎文法

– number, string, boolean, privateがある
– 定数は最初の値を変更できない、変数は変更できる
– varはプルグラム全体、letは宣言された構文の中のみ

let x:number
x = 123
console.log(x)
x = "ok"
console.log(x)

Type ‘string’ is not assignable to type ‘number’. でエラーになる

– let xはlet x:anyと同じでこれであれば型が異なってもOK
– null, undefined, NaNなどの特殊な値も

### 型変換

let x = 123
console.log(x)
let y = "456"
x = +y // x = Number(y)
console.log(x)

テキストを数値にするには、+かNumber()

### 演算

let price = 12500
let withTax = price * 1.1
let woTax = price / 1.1
console.log("price:" + price)
console.log("税別:" + withTax)
console.log("税込" + woTax)

### 制御構文

const num = 12345
const result = num % 2
if (result == 0){ 
    console.log(num + ":偶数")
} else {
    console.log(num + ":奇数")
}

== は等しい、===は型も等しい

const month = 7

if(month <= 0)
    console.log("不明")
else if(month < 3)
    console.log(month + ":冬")
else if(month < 6)
    console.log(month + ":春")
else if(month < 9)
    console.log(month + ":夏")
else if(month < 12)
    console.log(month + ":秋")
else if(month < 13)
    console.log(month + ":冬")
else
    console.log("不明")


三項演算子

const num = 12345

const result = num % 2 === 0 ? '偶数' : '奇数'
console.log(num + ' is ' + result)

switch

const month = 7

switch(month) {
    case 1: console.log(month + " winter"); break
    case 2: console.log(month + " winter"); break
    case 3: console.log(month + " spring"); break
    case 4: console.log(month + " spring"); break
    case 5: console.log(month + " spring"); break
    case 6: console.log(month + " summer"); break
    case 7: console.log(month + " summer"); break
    case 8: console.log(month + " summer"); break
    case 10: console.log(month + " fall"); break
    case 11: console.log(month + " fall"); break
    case 12: console.log(month + " winter"); break
    default: console.log("不明")
}

do while

const max = 100
let total = 0
let count = 0

while(count <= max) {
    total += count++
}
console.log(max + " sum is " + total)

for

const max = 100
let total = 0

for(let i = 1; i <= max; i++){
    total += i
}
console.log("total is " + total)

for(let var in value) と for(let var of value) がある

うむ、ここはどの言語でもだいたい同じ