-
Notifications
You must be signed in to change notification settings - Fork 43
Intro handson
Ilya Sher edited this page Aug 31, 2018
·
9 revisions
( This document is a work in progress )
This document includes talk points and workshop tasks for the "Introduction to NGS hands-on workshop".
- Covers the modern ops domain-specific niche.
-
bash
is not modern -
python
,ruby
,go
are not ops domain-specific
-
TODO: installation, verification
NGS has several mechanisms for integration with the environment. Running and exiting NGS are the prominent ones.
- Switches:
-e
,-p
,-pl
,-pj
,-pjl
ngs -e 'echo("Hello!")'
ngs -p '"Hello!"'
ngs -pl '[1,"aha",2]'
ngs -pl '0..5'
ngs -pj '["a", "b", "c"] * 2'
ngs -pjl '["a", "b", "c"] * 2'
- Exit codes - all objects are converted to exit codes using
ExitCode
.- Of a process
ngs -e '$(true)'; echo $?
ngs -e '$(false)'; echo $?
ngs -e '$(bash -c "exit 10")'; echo $?
ngs -e '$(ok: bash -c "exit 10")'; echo $?
- Of an NGS object
ngs -e true; echo $?
ngs -e false; echo $?
ngs -e 10; echo $?
ngs -e '"abc" ~ /^a/'; echo $?
ngs -e '"abc" ~ /^x/'; echo $?
- Of a process
-
die()
ngs -e 'die("mymessage")'; echo $?
ngs -e 'die("mymessage", 10)'; echo $?
-
exit()
ngs -e 'exit()'; echo $?
ngs -e 'exit(10)'; echo $?
ngs -e 'exit("mymessage")'; echo $?
ngs -e 'exit("mymessage", 10)'; echo $?
NGS has two syntaxes: commands syntax and expressions syntax.
The reasoning behind two syntaxes: common ops tasks include running external programs, including
- The two syntaxes and switching between them
- Commands syntax (in a file)
- This is the top level syntax in a file, similar to other shells
ls
ls >ls.txt
- Switching to expressions syntax
- Code block:
{ "aaa".echo() }
- RHS of assignment:
=
,+=
,-=
,*=
,/=
,.=
- Function definition:
F f(x) x*10
- Function call:
echo(...)
(but notfunc.method(...)
yet) - Namespace declaration:
ns { ... }
- Control structures:
if
,while
,switch
,for
echo ${1+2}
- Code block:
- Expressions syntax
- This is the syntax in:
- (A) command line switches
- (B) after switch from commands syntax via one of the syntax constructs above
- All the things that worked in commands syntax work, except for running external commands
- Things that are exclusive to expressions syntax
1 + 2
[1,2,3].each(echo)
- Switching to commands syntax
ngs -e '$(ls)'
ngs -e '$(top_level:: ls)'
ngs -p '$(ls)'
-
ngs -pi '$(ls)'
(-pi
is explained in next section) ngs -pi '$(ls | wc -l)'
- This is the syntax in:
- Commands syntax (in a file)
-
-pi
switchngs -pi Str
ngs -pi Str.constructors
ngs -pi ExitCode
-
inspect()
-
ngs -e '["a", "b", "c" ].inspect().echo()'
-
data.method()
andmethod(data)
syntax
-
-
ngs -e '["a", "b", "c" ].inspect().each(echo)'
-
each()
- one of the basic components of functional programming
-
-
- What is functional programming?
- Why functional programming is good for data manipulation?
- Function definition
- Named:
ngs -p 'F f(x) x*2; f(10)'
- Anonymous:
ngs -p 'f = F(x) x*2; f(10)'
ngs -p '(F(x) x*2)(10)'
- Named:
- Basic higher order functions:
each
,map
,filter
,reject
,Pred
ngs -e '[1,2,3].each(echo)'
The workshop will be primarily focused on ops-specific aspects of NGS
- Comments
# HackerNews API documentation: https://github.com/HackerNews/API
- Assignment
BASE_API_URL = 'https://hacker-news.firebaseio.com/v0'
- Get items (stories) list
-
newstories = ``curl -s "${BASE_API_URL}/newstories.json"``
- Quick points about sibling commands
$(run syntax)
`run-and-capture syntax`
- Exit codes when NGS is running external programs and related exceptions
- Quick points about sibling commands
- Limit items number:
newstories = newstories.limit(10)
-
newstories .= limit(10)
(same asx += 1
)
-
- Get information about each item and display it
for i in reverse(newstories) {
item = ``curl -s "${BASE_API_URL}/item/${i}.json"``
# Sometimes "item" is null
not(item) continues
echo("-" * 80)
echo("${Time(item.time)} [${item.by}] [${item.score}] ${item.title}")
for k in %[url text] {
# Display item's .url / .text if exist
if k in item {
echo(item[k])
}
}
}
- Accessing environment variables using
ENV
ENV.HOME / '.hn_state.json'
-
try
try fetch(STATE_FILE)
- Mention
try EXPR catch(e:Exception) HANDLER
fetch(filename:Str)
store(filename:Str, data:Any)
filter()
while CONDITION EXPR
$(sleep ...)
-
main()
and arguments matching-
main()
is not required - Command line arguments are matched automatically
- Parametrize
interval
- Add
once
option
-
NGS official website is at https://ngs-lang.org/