IT練習ノート

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

Windows10へのGitのインストール

インストールのログ

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\User> iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
PowerShell requires an execution policy of 'RemoteSigned' to run Scoop.
To make this change please run:
'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'
PS C:\Users\User> Set-ExecutionPolicy RemoteSigned -scope CurrentUser

実行ポリシーの変更
実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about_Execution_Policies
のヘルプ トピック (https://go.microsoft.com/fwlink/?LinkID=135170)
で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか?
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)  [?] ヘルプ (既定値は "N"): Y
PS C:\Users\User> iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
Initializing...
Downloading...
Extracting...
Creating shim...
Adding ~\scoop\shims to your path.
Scoop was installed successfully!
Type 'scoop help' for instructions.
PS C:\Users\User> scoop install git
Installing '7zip' (18.05) [64bit]
7z1805-x64.msi (1.7 MB) [=====================================================================================] 100%
Checking hash of 7z1805-x64.msi... ok.
Extracting... done.
Linking ~\scoop\apps\7zip\current => ~\scoop\apps\7zip\18.05
Creating shim for '7z'.
Creating shortcut for 7-Zip (7zFM.exe)
'7zip' (18.05) was installed successfully!
Installing 'git' (2.17.0.windows.1) [64bit]
PortableGit-2.17.0-64-bit.7z.exe (36.6 MB) [==================================================================] 100%
Checking hash of PortableGit-2.17.0-64-bit.7z.exe... ok.
Extracting... done.
Linking ~\scoop\apps\git\current => ~\scoop\apps\git\2.17.0.windows.1
Creating shim for 'git'.
Creating shim for 'gitk'.
Creating shim for 'git-gui'.
Creating shim for 'tig'.
Creating shim for 'git-bash'.
Creating shortcut for Git Bash (git-bash.exe)
Running post-install script...
'git' (2.17.0.windows.1) was installed successfully!
Notes
-----
To get Git to recognise OpenSSH, you will need to run

scoop install openssh
[environment]::setenvironmentvariable('GIT_SSH', (resolve-path (scoop which ssh)), 'USER')

and then restart powershell.
PS C:\Users\User> scoop install openssh
Updating Scoop...
Scoop was updated successfully!
Installing 'openssh' (5.4p1-1) [64bit]
openssh-5.4p1-1-msys-1.0.13-bin.tar.lzma (274.7 KB) [=====================================================================================================================] 100%
openssh-5.4p1-1-msys-1.0.13-lic.tar.lzma (4.8 KB) [=======================================================================================================================] 100%
msysCORE-1.0.13-2-msys-1.0.13-bin.tar.lzma (610.1 KB) [===================================================================================================================] 100%
zlib-1.2.3-2-msys-1.0.13-dll.tar.lzma (36.7 KB) [=========================================================================================================================] 100%
libminires-1.02_1-2-msys-1.0.13-dll.tar.lzma (11.4 KB) [==================================================================================================================] 100%
libopenssl-1.0.0-1-msys-1.0.13-dll-100.tar.lzma (654.9 KB) [==============================================================================================================] 100%
Checking hash of openssh-5.4p1-1-msys-1.0.13-bin.tar.lzma... ok.
Extracting... done.
Checking hash of openssh-5.4p1-1-msys-1.0.13-lic.tar.lzma... ok.
Extracting... done.
Checking hash of msysCORE-1.0.13-2-msys-1.0.13-bin.tar.lzma... ok.
Extracting... done.
Checking hash of zlib-1.2.3-2-msys-1.0.13-dll.tar.lzma... ok.
Extracting... done.
Checking hash of libminires-1.02_1-2-msys-1.0.13-dll.tar.lzma... ok.
Extracting... done.
Checking hash of libopenssl-1.0.0-1-msys-1.0.13-dll-100.tar.lzma... ok.
Extracting... done.
Linking ~\scoop\apps\openssh\current => ~\scoop\apps\openssh\5.4p1-1
Creating shim for 'scp'.
Creating shim for 'sftp'.
Creating shim for 'slogin'.
Creating shim for 'ssh-add'.
Creating shim for 'ssh-agent'.
Creating shim for 'ssh-keygen'.
Creating shim for 'ssh-keyscan'.
Creating shim for 'ssh'.
'openssh' (5.4p1-1) was installed successfully!
PS C:\Users\User> scoop install git
WARN  'git' (2.17.0.windows.1) is already installed.
Use 'scoop update git' to install a new version.
PS C:\Users\User> [environment]::setenvironmentvariable('GIT_SSH', (resolve-path (scoop which ssh)), 'USER')
PS C:\Users\User>

CPS

CPSによる階乗計算

> :{
| fac 0 k = k 1
| fac n k = fac (n-1) $ \res -> k $ (*) n res
| :}
> fac 3 id
6
> fac 3 (+1) -- (1 * 2 * 3) + 1
7

手計算してみる。

  fac 3 k
= fac 2 $ \res -> k $ (*) 3 res
= fac 1 $ \res1 -> (\res -> k $ (*) 3 res) $ (*) 2 res1 
= fac 0 $ \res1 -> (\res -> k $ (*) 3 res) $ (*) 2 res1 
= (\res1 -> (\res -> k $ (*) 3 res) $ (*) 2 res1) 1
= (\res -> k $ (*) 3 res) $ (*) 2 1)
= \res -> k $ (*) 3 res $ 2
= k $ (*) 3 2) 
= k $ 6 

stackでテストスイートを個別に実行する

name: hoge

tests:
  foo-test:
    main:                Spec.hs
    source-dirs:         test
  bar-doctest:
    main: test/doctests.hs

と2つのテストスイートfoo-testbar-doctestを定義する。

このときstack testでテストが実行されるが、両方とも実行される。

片方だけ実行したい場合は

> stack test hoge:foo-test
> stack test hoge:bar-doctest

パッケージが1つだけであれば省略できる。

> stack test :foo-test
> stack test :bar-doctest

Applicative Mabyeで条件分岐

なんらかのデータを取得する処理をして、あればそれを使い、なければ、別途データを取得する

というロジックは、よくあるのではないでしょうか。

例えば、このような場合です。

import qualified Data.HashMap.Lazy as HML

m = HML.fromList [("k1", "**"), ("k2", "##")]

foo x1 x2 = 
  case HML.lookup x1 m of
    Just a1  -> Just a1
    Nothing ->  
      case HML.lookup x2 m of
        Just a2 -> Just a2
        Nothing -> Nothing

キーを二つ用意して、最初のキーで情報が見つかれば、それを使い、見つからなければ、二番目のキーを使います。

lambda > foo "k1" "k2"
Just "**"
lambda > foo "k1_" "k2"
Just "##"
lambda > foo "k1_" "k2_"
Nothing

caseが2つあって冗長です。

Maybeモナドa >>= bとすると、aJustのときに、bに移ります。が、やりたことはこの逆で、Nothingだったらbに移るということです。このような時はApplicativeを使うと良さそうです。

foo'' x1 x2 = HML.lookup x1 m <|> HML.lookup x2 m
lambda > foo'' "k1" "k2"
Just "**"
lambda > foo'' "k1_" "k2"
Just "##"
lambda > foo'' "k1_" "k2_"
Nothing

Contravariant

Data.Functor.Contravariant

covariant     functor ~~> produce something
contravariant functor ~~> consume something
(>$) = contramap . const

const     :: a -> b -> a
contramap :: Contravariant f => (a -> b) -> f b -> f a

contrampa . const = x ->  y -> x 
                         (a -> b) -> f b -> f a
                  = x -> f b -> f a  ( y = a , x = b )
                  = b -> f b -> f a
lambda > import Data.Functor.Contravariant
lambda > data Person = Person {balance :: Int}
lambda > let personBankBalance p = balance p
lambda > let negative = Predicate (\x -> x < 0)
lambda >
lambda > overdrawn = contramap personBankBalance negative ::  Predicate Person
lambda >
lambda > getPredicate overdrawn (Person 5)
False
lambda > getPredicate overdrawn (Person (-10))
True
lambda >
lambda > overdrawn' = (>$) (-15) negative
lambda > getPredicate overdrawn' (Person 5)
True
lambda > getPredicate overdrawn' (Person (-10))
True