Declare functions with type in ghci

To declare a function inside GHCi one should use a let word for function name.

To declare the type explicitly it should be specified after declaring a function name, and the function body should go next after a semicolon:

1
let myFunc :: Int -> Int -> Int; myFunc x y = x + y

or on the next line but with identation:

1
2
let myFunc :: Int -> Int -> Int
myFunc = (+)

SSH config example

SSH config example to use different keys:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# bitbucket
Host bitbucket.org
HostName bitbucket.org
IdentitiesOnly yes
IdentityFile ~/.ssh/bitbucket_key
#PreferredAuthentications publickey
PasswordAuthentication no
User git

# github
Host github.com
HostName github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/github_key
#PreferredAuthentications publickey
PasswordAuthentication no
User git

GPG signed commits on git

GPG signature

Git commits could be signed using GPG signature.

Howto is here

Common problems

Outdated default gpg program on Windows

Currently on windows git has its own gpg program, which is outdated
version 1. And it is not possible to set it to remember passphrase.

So after creating the signature the new version of gpg (version 2)
installed by Gpg4win should be set as gpg program for git:

1
git config --global gpg.program 'C:\Program Files (x86)\GnuPG\bin\gpg.exe'
Remember password

For gpg-agent configuration should be set (usually inside ~/.gnupg/gpg-agent.conf):

1
2
default-cache-ttl 3600
max-cache-ttl 100000
Outdated version on linux

To set gnu-agent remember passphrase, first ~/.gnupg/gpg-agent.conf should be created as described earlier,
second, gpg version 1 doesn’t work with gpg-agent version 2. So gpg2 should be set as gpg.program for git configuration:

1
git config --global gpg.program gpg2

tmux keys

Common

^B : command mode
command: set mouse on - enable mouse navigation

Panes

^B % split vertically
^B " split horizontally
^B arrow_keys switch active pane

^B z zoom in and out currently active pane
command: resize-pane -D/U/L/R <number> resize command for current pane (down/up/left/right)

Help

^B ? show help

Windows

^B c create a window (a set of panes)
^B n next window (pane set)
^B p previous window (pane set)

Sessions

^B d detach session (run in background)
tmux attach - restore last session
tmux ls - list currently running sessions (to restore one later)
tmux attach -t- restore specific session by number or name
^B $ rename session
^B s list of sessions inside tmux (to select one interactively)
tmux new -s <session_name> - create a new named session
tmux kill-session -t <session_name_or_number> - kill specific session

Selection

^B [ switch to scroll mode
q - exit scroll mode
^space mark selection start
^W mark selection end and copy selection
^B ] insert copied text

Get linux mint dns settings

To find out DNS settings on a desktop machine running modern Ubuntu flavour operating system (Linux Mint 18.1)
and using NetworkManager, run this command:

1
2
nmcli connection show --active # to list all active connections. Copy an ID of the active connection.
nmcli connection show <ID> | grep DNS # use the ID found out on the previous step

GHCi multiline

When in haskell REPL (ghci), to use multiline definitions:

First way, is to use blocks, like this:

1
2
3
4
Prelude> :{
Prelude| let addTwo :: Int -> Int -> Int
Prelude| addTwo x y = x + y
Prelude| :}

Second way, use multiline mode:

1
2
3
4
5
Prelude> :set +m
Prelude> let addTwo :: Int -> Int -> Int
Prelude| addTwo x y = x + y
Prelude|
Prelude> addTwo 1 3

Also, don’t forget, function definitions must start from let in REPL.

http://stackoverflow.com/questions/8443035/multi-line-commands-in-ghci

Reduce haskell binary

To reduce the binary compiled by haskell compiler one can use some methods.

First, use dynamic linking:

1
ghc -dynamic -o hello hello.hs

Second, remove all trash (e.g., with strip):

1
strip -p --strip-unneeded --remove-section=.comment -o hello-small hello

Third, compress executable binary (e.g., with UPX) (see man upx for more options)

1
upx --brute -o hello-super-small hello-small

EcmaScript module syntax

Export syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// default exports
export default
export default
export default
export default
export default
export default
export default
export default
export default
// inline exports
export var foo = 1;
export var foo = function () {};
export var bar;
export let foo = 2;
export let bar;
export const foo = 3;
export function foo () {}
export class foo {}
// named exports
export {};
export {foo};
export {foo, bar};
export {foo as bar};
export {foo as default};
export {foo as default, bar};
// exports from (re-exports)
export * from “foo”;
export {} from “foo”;
export {foo} from “foo”;
export {foo, bar} from “foo”;
export {foo as bar} from “foo”;
export {foo as default} from “foo”;
export {foo as default, bar} from “foo”;
export {default} from “foo”;
export {default as foo} from “foo”;

Import syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// default imports
import foo from “foo”;
import {default as foo} from “foo”;
// named imports
import {} from “foo”;
import {bar} from “foo”;
import {bar, baz} from “foo”;
import {bar as baz} from “foo”;
import {bar as baz, xyz} from “foo”;
// glob imports
import * as foo from “foo”;
// mixing imports
import foo, {baz as xyz} from “foo”;
import foo, * as bar from “foo”;
// just import
import “foo”;

google-oauth

How to update session using implicit flow

Using the implicit flow for google oauth2 it is impossible to refresh the session, as
refresh tokens are not accessible in implicit flow. One can use a listener for expiring sessions
and run GoogleAuth.signIn() or GoogleUser.signIn() methods to refresh the session, but
it will lead to the new window to appear on the client side (in case the user has already grant access,
the window will flash for a moment, and close by itself.)

But…

…Google Sign-in client lib has an undocumented method for refreshing the session inside
the browser:

1
gapi.auth2.getAuthInstance().currentUser.get().reloadAuthResponse()

UPDATE

As it comes, the google client lib automatically refreshes the session in 5 mins to the end of previous session.