← nimrepo

table, sugar, nimism, shell,typedtraits

6a06fc2988c4c38ba1178e250a051168eb45c4d6 · ovidiucs · Feb 3, 2019 20:38

+170 · 6 files changed

  • commandlineparams.nim +21 −0
  • nimisms.nim +12 −0
  • shellsource.nim +21 −0
  • sugars.nim +42 −0
  • typedtraitsnim.nim +24 −0
  • workingwithtables.nim +50 −0
diff --git a/commandlineparams.nim b/commandlineparams.nim
new file mode 100644
index 0000000..83b038f
--- /dev/null
+++ b/commandlineparams.nim
@@ -0,0 +1,21 @@
+# Copyright 2019.
+import terminal
+import os,strformat
+
+styledEcho fgRed,
+  "* paramCount - returrns the number of cmmand line paramters",
+  "* commandLineParams - returns the command line paramters",
+  "+ import os"
+
+let
+  numParams = paramCount()
+  params = commandLineParams()
+
+echo "Number of command line params: ", numParams
+echo "Command line params: ", params
+echo numParams.len
+
+for n in 0 ..< numParams:
+  echo fmt"Param {n+1} = {params[n]}"
+  echo fmt"{$type(numParams)}"
+
diff --git a/nimisms.nim b/nimisms.nim
new file mode 100644
index 0000000..dd9373a
--- /dev/null
+++ b/nimisms.nim
@@ -0,0 +1,12 @@
+# Copyright 2019.
+#var a {.noInit.}: array [0 .. 1023, char] // not working
+var a {.noInit.}: array[0 .. 1023, char]
+
+# not ok
+#const size = 6
+#type A = range[0 ..< size]
+
+#nim_src_bshJJ4.nim(5, 10) Error: range types need to be constructed with '..', '..<' is not supported
+
+const size = 6
+type A = range[0 .. (size-1)]
diff --git a/shellsource.nim b/shellsource.nim
new file mode 100644
index 0000000..33a7a48
--- /dev/null
+++ b/shellsource.nim
@@ -0,0 +1,21 @@
+# Copyright 2019.
+import os
+import terminal
+
+
+styledEcho fgRed,
+  "Use putEnv to set environment variable\n",
+  "Use getEnv to get environment variable"
+
+echo """
+let
+  envVar = "NIM_TEMP"
+  envVarVal = "foo"
+putEnv(envVar, envVarVal)
+echo getEnv(envVar)
+"""
+let
+  envVar = "NIM_TEMP"
+  envVarVal = "foo"
+putEnv(envVar, envVarVal)
+echo getEnv(envVar)
diff --git a/sugars.nim b/sugars.nim
new file mode 100644
index 0000000..cafc77b
--- /dev/null
+++ b/sugars.nim
@@ -0,0 +1,42 @@
+# Copyright 2019.
+import sugar
+import sequtils
+import strformat
+import bignum
+
+proc int2int(fn: proc(inp: int): int, x: int): int=
+  fn(x)
+echo int2Int(x => x + 1,2)
+
+
+let
+  a = [1,2,3,4,5]
+  b = a.map(x => 2*x)
+
+echo b
+
+let
+  str = "nim lang"
+  cSeq = lc[c | (c <- str), char]
+echo fmt"str of type{$str.type} ={str}"
+echo fmt"cSeq of type {cSeq.type} (seq[SEQ_TYPE]) = {cSeq}"
+
+const upper = 61
+var bn: array[upper, Rat]
+
+proc bernoulli(n:int): Rat =
+  var A: seq[Rat]
+
+  for i in 0 .. n:
+    A.add(newRat())
+
+  for i in 0 .. A.high:
+    discard A[i].set(1,i+1)
+    for j in countDown(i, 1):
+      A[j-1] = j*(A[j-1]-A[j])
+  return A[0]
+
+for i in 0 .. bn.high:
+  bn[i] = bernoulli(i)
+  if bn[i].toFloat != 0.0:
+    echo fmt"B({i:2}) = {bn[i]:>55}"
diff --git a/typedtraitsnim.nim b/typedtraitsnim.nim
new file mode 100644
index 0000000..0d28581
--- /dev/null
+++ b/typedtraitsnim.nim
@@ -0,0 +1,24 @@
+# Copyright 2019.
+import typetraits
+import terminal
+
+styledEcho fgRed,
+  "mainly defines the name proc which",
+  "is used to get the string name of any type.",
+  "It also defines $ aliased to name."
+
+
+var x = 5
+var y = "foo"
+
+# using regular proc call style
+echo name(x.type), " x = ", x
+echo name(y.type), " y = ", y
+
+#Using UFCS
+echo x.type.name, " x = ", x
+echo y.type.name, " y = ", y
+
+# Using $
+echo $x.type, " x = ", x
+echo $y.type, " y = ", y
diff --git a/workingwithtables.nim b/workingwithtables.nim
new file mode 100644
index 0000000..f1b0f21
--- /dev/null
+++ b/workingwithtables.nim
@@ -0,0 +1,50 @@
+# Copyright 2019.
+import tables
+
+var a = {"hi":1, "there":2}.toTable
+echo a["hi"], " ", a.len
+echo "a is of type ", $type(a)
+assert a.hasKey("hi")
+
+for key, value in a:
+  echo key, " ", value
+
+
+var
+  hash = initTable[string, int]() # empty hash table
+  hash2 = {"key1": 1, "key2": 2}.toTable # hash table with two keys
+  hash3 = [("key1", 1), ("key2", 2)].toTable # hash table from tuple array
+  hash4 = @[("key1", 1), ("key2", 2)].toTable # hash table from tuple seq
+  value = hash2["key1"]
+
+
+hash["spam"] = 1
+hash["eggs"] = 2
+hash.add("foo", 3)
+
+echo "hash has ", hash.len, "elements"
+echo "hash has key foo? ", hash.hasKey("foo")
+echo "hash has key bar? ", hash.hasKey("bar")
+
+echo "iterate pairs: " # iterate over (key, value) pair
+for key, value in hash:
+  echo key, ": ", value
+
+echo "iterate keys: " #iterate over keys
+for key in hash.keys:
+  echo key
+
+echo "iterate values: " # iterate over values
+for key in hash.values:
+  echo key
+
+# getting values of specific keys
+echo """hash["spam"] = """, hash["spam"]
+
+# if you try to get a value for an unset key, you will get
+# keyerro exception
+
+try:
+  echo """hash["key_not_yet_set"] = """, hash["key_not_yet_set"]
+except KeyError:
+  echo """hash["key_not_yet_set"] is not yet set"""