Skip to content

Instantly share code, notes, and snippets.

@callmekohei
Last active February 9, 2018 13:30
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save callmekohei/70f93fe66fbd23f22ed281797e9ceb27 to your computer and use it in GitHub Desktop.
F#, Python and Vim script tiny memo

F#, Python and Vim script tiny memo

operator

F# python vim script MQL4
assign <- = = =
equal = == == ==
not equal <> != != !=
same instance object.referenceEqual is is
and && and && &&
or || or || ||
not not not ! !

The ternary operator

F#

stdout.WriteLine(if true then "foo" else "bar")
// foo

Python

print('foo' if True else 'bar')
# foo

Vim script

echo( v:true ? 'foo' : 'bar')
" foo

MQL4

void OnStart()
{
    printf( true ? "foo" : "bar");
}
// foo

Commnet

F#

// line comment

(*
    block comment
*)

Python

# line commnet

# block comment is nothing

Vim script

" line comment

" block comment is nothing

MQL4

// line commnet

Strings

FSharp

// string concatenation
let foobar = "foo" + "bar"
stdout.WriteLine(foobar) // foobar

// verbatim strings
let verbatimXml = @"<book title=""foo bar"">"
let tripleXml = """<book title="foo bar">"""
stdout.WriteLine(verbatimXml) // <book title="foo bar">
stdout.WriteLine(tripleXml)   // <book title="foo bar">

// strings indent
let poem =
    "foo\n\
     bar\n\
     baz"

stdout.WriteLine(poem)
// foo
// bar
// baz

Python

# string concatenation
foobar = 'foo' + 'bar'
print(foobar) # foobar

# TODO
# verbatim strings
verbatimXml = r'<book title="foo bar">'
tripleXml   = """<book title="foo bar">"""
print(verbatimXml) # <book title="foo bar">
print(tripleXml)   # <book title="foo bar">

# strings indent
poem ="foo\n\
bar\n\
baz"
print(poem)
# foo
# bar
# baz

Vim script

" string concatenation
let foobar = 'foo' . 'bar'
echo(foobar)
"foobar

" TODO
" verbatim strings
let verbatimXml = "<book title=""foo bar"">"
let tripleXml = '<book title="foo bar">'
echo(verbatimXml)
" <book title=
echo (tripleXml)
" <book title="foo bar">

"TODO
" strings indent
let poem = 'foo\n
            \bar\n
            \baz'
echo(poem)
" foo\nbar\nbaz

MQL4

void OnStart()
{
    // string concatenation
    string foobar = "foo" + "bar";
    printf( foobar );
    // foobar

    // verbatim strings
    // ???

    // strings indent
    // ???
}

Variables

F#

let mutable foo = 123

Python

foo = 123

vim script

let foo = 123

MQL4

int foo = 123;

Constant

F#

let foo = 123

Python

# nothing

Vim script

let foo = 123 | lockvar foo

MQL4

#define FOOBAR "foobar"

Confirm type

F#

"foo".GetType()
|> stdout.WriteLine
// System.String

Python

print(type('foo'))
# <class 'str'>

Vim script

echo(type('abc'))
" 1

" :help type
" --------------------------------------------
" Number:       0  v:t_number
" String:       1  v:t_string
" Funcref:      2  v:t_func
" List:         3  v:t_list
" Dictionary:   4  v:t_dict
" Float:        5  v:t_float
" Boolean:      6  v:t_bool (v:false and v:true)
" None          7  v:t_none (v:null and v:none)
" Job           8  v:t_job
" Channel       9  v:t_channel

MQL4

Condition ( if )

F#(short-circuit evaluation)

if true || false then
    stdout.WriteLine("aaa")
else
    stdout.WriteLine("bbb")

// aaa

Python(short-circuit evaluation)

if True or False:
    print('aaa')
else:
    print('bbb')

# aaa

Vim script(short-circuit evaluation)

if v:true || v:false
    echo("aaa")
else
    echo("bbb")
endif

" aaa

MQL4(short-circuit evaluation)

void OnStart()
{
    if ( true || false )
        printf("aaa");
    else
        printf("bbb");
}
// aaa

Condition ( switch )

F#

let foo = 123

match foo with
| 123 -> stdout.WriteLine("aaa")
| _   -> stdout.WriteLine("bbb")

// aaa

Python

# nothing

Vim script

" nothing

MQL4

void OnStart()
{
    int foo = 123;
    switch( foo )
    {
        case 123 : printf("aaa"); break;
        default  : printf("bbb"); break;
    }
}
// aaa

Loop ( for )

F#

for n in 1..5 do
    stdout.WriteLine(n)

// 1
// 2
// 3
// 4
// 5

Python

for n in range(1,6):
    print(n)

# 1
# 2
# 3
# 4
# 5

Vim script

for n in range(1,5)
    echo(n)
endfor

" 1
" 2
" 3
" 4
" 5

MQL4

void OnStart()
{
    for(int n = 1; n <= 5 ; n++)
        printf( DoubleToStr(n) );
}
// 1.00000000
// 2.00000000
// 3.00000000
// 4.00000000
// 5.00000000

Function

F#

let foo a b =
    a + b

stdout.WriteLine(foo 1 2)

// 3

Python

def foo(a,b):
    return a + b

print(foo(1,2))

# 3

Vim script

function! s:foo(a,b)
    return a:a + a:b
endfunction

echo(s:foo(1,2))
" 3


" :h variables ---> 3. Internal variable
" -----------------------------------------------------------------
" buffer-variable     b:  Local to the current buffer.
" window-variable     w:  Local to the current window.
" tabpage-variable    t:  Local to the current tab page.
" global-variable     g:  Global.
" local-variable      l:  Local to a function.
" script-variable     s:  Local to a |:source|'ed Vim script.
" function-argument   a:  Function argument (only inside a function).
" vim-variable        v:  Global, predefined by Vim.

MQL4

int foo (int a , int b)
{
    return ( a + b );
}

void OnStart()
{
    printf ( DoubleToStr(foo(1,2)));
}
// 3

Class

F#

type Foo(a,b) =
    
    // initial constructor
    let x = a + 100
    let y = b + 100
    
    // add method
    member this.Bar(z) =
        x + y + z

(Foo(1,2)).Bar(3)
|> stdout.WriteLine

// 206

Python

class Foo:

    # initial constructor
    def __init__(self,a,b):
        self.x = a + 100
        self.y = b + 100

    # add method
    def bar(self,c):
        return self.x + self.y + c

print(Foo(1,2).bar(3))

# 206

Vim script

function! s:foo(a,b)

    " initial constructor
    let self =  {}
    let self.x = a:a + 100
    let self.y = a:b + 100

    " add method
    function! self.bar(z)
        return self.x + self.y + a:z
    endfunction

    return self
endfunction

echo(s:foo(1,2).bar(3))

" 206

MQL4

#property strict

class Foo
{
    private:
        int x, y;

    public:

        // initial constructor
        Foo(int a , int b);

        // add method
        int bar(int c)
        {
            return(x + y + c);
        }
};

Foo::Foo(int a ,int b)
{
    x = a + 100;
    y = b + 100;
}


void OnStart()
{
    Foo foo(1,2);
    printf( DoubleToStr( foo.bar(3)) );
}
// 206.00000000

Exception handling

Fsharp

try
    stdout.WriteLine(1/0)
with e -> stdout.WriteLine(e.Message)

// Attempted to divide by zero.

Python

try:
    print(1/0)
except Exception as e:
    print(e)

# division by zero

Vim script

" TODO
try
    echo(1/0)
catch
    echo(v:exception)
endtry

" 9223372036854775807

MQL4

// nothing...

File handling

FSharp

open System.IO

let filePath = "./callmekohei.txt"

do
    // open file
    // 'use' will call Dispose method of IDisposable interface, and Dispose of StreamReader = reader.Close()
    use stream = new StreamReader(filePath)

    // read file
    while not stream.EndOfStream do
        stream.ReadLine() |> stdout.WriteLine

    // implicitly closed at end-of-scope by use


// call
// me
// kohei

Python

filePath = ('./callmekohei.txt')

# open file
f = open(filePath)

# read file
line = f.readline()

while line:
    print (line)
    line = f.readline()

# close file
f.close


# call
#
# me
#
# kohei

Vim script

let filePath = "./callmekohei.txt"

" open file , read file and close file
let lst = readfile(filePath)

for s in lst
    echo(s)
endfor

" call
" me
" kohei

MQL4

void OnStart()
{
    // For security reasons, the file is opened in the folder of the client terminal in the subfolder MQL4\files
    // e.g : C:\Program Files\OANDA - MetaTrader\MQL4\Files\callmekohei.txt
    int filehandle = FileOpen("callmekohei.txt", FILE_TXT);
    string s = FileReadString(filehandle);
    printf(s);
    FileClose(filehandle);
}
@baronfel
Copy link

F#: to close the stream reader, either do

open System.IO

let filePath = "./callmekohei.txt"

// open file
let stream = new StreamReader(filePath)

// read file
while not stream.EndOfStream do
    stream.ReadLine() |> stdout.WriteLine

// explicit close/dispose
stream.Close()

or

open System.IO

let filePath = "./callmekohei.txt"

// open file
// 'use' will call Dispose method of IDisposable interface, and Dispose of StreamReader = reader.Close()
use stream = new StreamReader(filePath)

// read file
while not stream.EndOfStream do
    stream.ReadLine() |> stdout.WriteLine

// implicitly closed at end-of-scope by use ^

@callmekohei
Copy link
Author

callmekohei commented Jan 15, 2018

@baronfel

Thank you!

I fix it!

@AnthonyLloyd
Copy link

AnthonyLloyd commented Jan 16, 2018

For F# you can do printfn instead of stdout.WriteLine.

@callmekohei
Copy link
Author

@AnthonyLloyd

Oh!
Thank you (^_^)/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment