IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

Railsのリクエストパラメータの確認方法

logディレクトリ配下にログファイルがある。

(root)$ ls log
delete.me   development.log production.log
(root)$ vim log/development.log 

Started POSTを目印に探す。

例1:チケット作成時

Started POST "/projects/proj_hoge/issue_categories" for 127.0.0.1 at 2016-05-01 12:32:53 +0900
Processing by IssueCategoriesController#create as HTML 
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tLh26w1pZDyrZ+7e3LywL+CMI1EUl3oM93udrssJHUEe7uFhfjLhYBBKL0ENht7pa9Htfk1eaJqlkMYbgdPtGA==", "issue_category"=>{"name"=>"新しいカテゴリ01", "assigned_to_id"=>""}, "commit"=>"作成", "project_id"=>"proj_hoge"}
  ^[[1m^[[35mSQL (8.8ms)^[[0m  UPDATE `tokens` SET `tokens`.`updated_on` = '2016-05-01 21:32:53' WHERE `tokens`.`user_id` = 1 AND `tokens`.`value` = '44c8a7afb44b4045a8dabe89b8b178b857183b1f' AND `tokens`.`action` = 'session' 

例2:カテゴリー作成時

Started POST "/projects/proj_hoge/issues" for 127.0.0.1 at 2016-05-01 12:25:30 +0900
Processing by IssuesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8el3FevbMDoiQr70DxFP4QtfcRIhI9e51V6WR8oPGrJbv+CfmIC1Zplvf2veKyEngAK/PXjqxS+Htc3ygNXq6w==", "form_update_triggered_by"=>"", "issue"=>{"is_private"=>"0", "tracker_id"=>"1", "subject"=>"aaaaa", "description"=>"", "status_id"=>"1", "priority_id"=>"2", "assigned_to_id"=>"", "parent_issue_id"=>"", "start_date"=>"2016-05-01", "due_date"=>"", "estimated_hours"=>"", "done_ratio"=>"0"}, "was_default_status"=>"1", "commit"=>"作成", "project_id"=>"proj_hoge"}

HaskellでOpenGLのサンプルを動かす

どうもGLUTは描画がメインで、GUIの操作はうまくできないらしく、GUI操作までやりたい場合はGLFWを使うようです(この点は理解があやふや)。ともかく動いたのでメモ。

サンプル

f:id:naotoogawa:20170202212747p:plain

ライブラリ

OpenGLのライブラリ(Macではデフォルトで入っている??)

GLFW - An OpenGL library

Haskellバインディング

GitHub - bsl/GLFW-b: Haskell bindings to GLFW

サンプル

GitHub - bsl/GLFW-b-demo: GLFW-b demo

インストール手順

サンドボックスを作成

cabal sandbox

OpenGLのインストール

cabal install OpenGL

GLFW-bのインストール

cabal install GLFW-b 

mtlのインストール

cabal install mtl

srcディレクトリを作り、そこに、サンプルのソースをコピーする。

(root)$ ls src
Gear.hs         Main.hs         

cabalファイルをinit``で作成する。あらかじめソースがあると依存関係をよしなにcabal```ファイルに自動的に記述してくれるようです。

(root)$ cabal init

依存関係の該当部分は下記のようになっていました。(実際に生成された依存関係は1行でした。)

executable Main
  main-is:             Main.hs
  -- other-modules:       
  -- other-extensions:    
  build-depends:       base >=4.9 && <4.10,
                       OpenGL >=3.0 && <3.1,
                       stm >=2.4 && <2.5,
                       mtl >=2.2 && <2.3,
                       transformers >=0.5 && <0.6,
                       pretty >=1.1 && <1.2,
                       GLFW-b >=1.4 && <1.5

ビルドする。

(root)$ cabal build
Package has never been configured. Configuring with default flags. If this
fails, please run configure manually.
Warning: The package list for 'hackage.haskell.org' is 19 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Configuring Main-0.1.0.0...
Building Main-0.1.0.0...
Preprocessing executable 'Main' for Main-0.1.0.0...
[1 of 2] Compiling Gear             ( src/Gear.hs, dist/build/Main/Main-tmp/Gear.o )
[2 of 2] Compiling Main             ( src/Main.hs, dist/build/Main/Main-tmp/Main.o )
Linking dist/build/Main/Main ...

実行する。

(root)$ dist/build/Main/Main 
    ------------------------------------------------------------
    '?': Print these instructions
    'i': Print GLFW information
    
    * Mouse cursor, keyboard cursor keys, and/or joystick
      control rotation.
    * Mouse scroll wheel controls distance from scene.
    ------------------------------------------------------------
window focus: FocusState'Focused
cursor pos: 26 695
cursor pos: 15 682
cursor pos: 11 665

2次曲線と4次曲線の原点での曲率

じっくり学ぶ曲線と曲面―微分幾何学初歩

じっくり学ぶ曲線と曲面―微分幾何学初歩

p.15 に 例1.1.4(放物線の曲率)、例1.1.5(4次曲線の曲率)があります。

  • 放物線の原点での曲率は2|a|
  • 4次曲線の原点での曲率は0

と説明されています。

これはグラフを書いてみるとイメージができると思います。

放物線

f:id:naotoogawa:20170131212449p:plain

https://sandbox.open.wolframcloud.com/app/objects/65966c15-2789-4f0c-bf88-06d0ed98093d#sidebar=compute

4次曲線

4次曲線は原点で直線のように見えます。

f:id:naotoogawa:20170131212456p:plain

https://sandbox.open.wolframcloud.com/app/objects/05e4c0d1-f853-43ae-8a68-b257c150b931#sidebar=compute

Servantでの例外処理

ファイルの読み込み(基本形で、ここではまだ例外処理を入れていない)

Reading a file in Servant

ファイル読み込みに失敗した時の対処 (catch)

*> :t catch
catch :: Exception e => IO a -> (e -> IO a) -> IO a

catchは、catchした時の対処はできるが、戻りの型はIO aなので、aで正常と異常を区別しなくてはいけない。下の例ではIO Stringなので、Stringで正常と異常を区別することになるが、異常の状態をなにか特別な文字列表現にしなくてはいけない。これは、実質的に、例外の情報を、呼び出し側に教えることができない。

Exception handing by catch in Servant

ファイル読み込みに失敗した時の対処 (try)

tryIO (Either a b)なので、例外の情報をLeftで呼び出し元に教えることができる。

Exception handling by try in Servant

パスより読み込むファイルを指定する

Capturing in Servant

明示的に型を指定するVisible Type Application

GHC 8.0からVisible Type Applicationが導入されました。

TypeApplication – GHC

今までは::で型を明示的に指定していました。

Prelude> 
Prelude> let x = return 1
Prelude> :t x
x :: (Num a, Monad m) => m a
Prelude> let x = return 1 :: Maybe Int
Prelude> :t x
x :: Maybe Int
Prelude> let x = return 1 :: Maybe Float
Prelude> :t x
x :: Maybe Float
Prelude> 

TypeApplicationsを使うと@Typeで型を指定することができるようになります。

Prelude> 
Prelude> :set -XTypeApplications
Prelude> let x = return @Maybe 1
Prelude> :t x
x :: Num a => Maybe a
Prelude> 

使い方としては、ポリモーフィックな関数があったとして、それに対して型を限定した使い方をさせたい時に使うのかもしれません。

Prelude> :t map
map :: (a -> b) -> [a] -> [b]
Prelude> :t map @Int @Char
map @Int @Char :: (Int -> Char) -> [Int] -> [Char]

IntとIntegerの違い

IntIntegerとの違い

インスタンスの確認

Prelude > :i Int
data Int = GHC.Types.I# GHC.Prim.Int#    -- Defined in ‘GHC.Types’
instance Bounded Int      -- Defined in ‘GHC.Enum’
instance Enum Int         -- Defined in ‘GHC.Enum’
instance Eq Int           -- Defined in ‘GHC.Classes’
instance Integral Int     -- Defined in ‘GHC.Real’
instance Num Int          -- Defined in ‘GHC.Num’
instance Ord Int          -- Defined in ‘GHC.Classes’
instance Read Int         -- Defined in ‘GHC.Read’
instance Real Int         -- Defined in ‘GHC.Real’
instance Show Int         -- Defined in ‘GHC.Show’
Prelude > :i Integer
data Integer
  = integer-gmp-1.0.0.1:GHC.Integer.Type.S# !GHC.Prim.Int#
  | integer-gmp-1.0.0.1:GHC.Integer.Type.Jp# {-# UNPACK #-}integer-gmp-1.0.0.1:GHC.Integer.Type.BigNat
  | integer-gmp-1.0.0.1:GHC.Integer.Type.Jn# {-# UNPACK #-}integer-gmp-1.0.0.1:GHC.Integer.Type.BigNat
                      -- Defined in ‘integer-gmp-1.0.0.1:GHC.Integer.Type’
instance Enum Integer     -- Defined in ‘GHC.Enum’
instance Eq Integer       -- Defined in ‘integer-gmp-1.0.0.1:GHC.Integer.Type’
instance Integral Integer -- Defined in ‘GHC.Real’
instance Num Integer      -- Defined in ‘GHC.Num’
instance Ord Integer      -- Defined in ‘integer-gmp-1.0.0.1:GHC.Integer.Type’
instance Read Integer     -- Defined in ‘GHC.Read’
instance Real Integer     -- Defined in ‘GHC.Real’
instance Show Integer     -- Defined in ‘GHC.Show’

BoundedクラスのインスタンスになっているのがIntで、一方、IntegerはBoundedクラスのインスタンスではない。

Boundedを確認してみると。Integerには最大値(maxBound)がないことがわかる。

Prelude > maxBound :: Int
9223372036854775807

Prelude > maxBound :: Integer

<interactive>:11:1: error:
    • No instance for (Bounded Integer)
        arising from a use of ‘maxBound’
    • In the expression: maxBound :: Integer
      In an equation for ‘it’: it = maxBound :: Integer

Intの最大値(9223372036854775807)の型を確認する。

Prelude > 9223372036854775807 :: Integer
9223372036854775807

Prelude > 9223372036854775807 :: Int
9223372036854775807

Intの最大値から+1の(9223372036854775807+1)の型を確認する。

Prelude > 9223372036854775808 :: Integer
9223372036854775808

Prelude > 9223372036854775808 :: Int

<interactive>:15:1: warning: [-Woverflowed-literals]
    Literal 9223372036854775808 is out of the Int range -9223372036854775808..9223372036854775807
    If you are trying to write a large negative literal, use NegativeLiterals
-9223372036854775808