-
-
Notifications
You must be signed in to change notification settings - Fork 125
Instead of
function(arg)use
function argor
arg.functionInstead of
function(arg1,arg2)use
arg1.function arg2Instead of
if condition:
instructionuse
if condition:instructionThe same trick works for loops and even procs definitions. You can further avoid the leading spaces by separating instructions by ; instead of newlines.
You can also replace all indentation by parentheses.
for x in..10:
for y in..10:
if x+y>5:echo x+yCan become:
for x in..10:(for y in..10:(if x+y>5:echo x+y))or even shorter:
for x in..10:(for y in..10:
if x+y>5:echo x+y)Use
"item1,item2,item3,item4,item5,item6,item7,item8".split','to create a list of strings. This requires importing the strutils module and saves 2N-16 characters (where N is the number of items and assuming you already import something).
If none of the items contain a whitespaces, use
split"item1 item2 item3 item4 item5 item6 item7 item8"You can also use the split function from re instead for a shorter import, though it requires a regex splitter.
"item1,item2,item3,item4,item5,item6,item7,item8".split re","Use default values for parameters to avoid having to declare their type. For example
proc f(n=0,s="")=...saves 2+4 characters.
Furthermore, you can declare the output type to be auto (or even deprecated any) to save additional bytes.
Multiple variables can be declared using a single var keyword like
var x,y,z=0or
var
x=0
y=1
z=2There are several options:
-
getopt, fromgetopt(available throughprelude). This is usually best, however it can't be used when any arguments start with-. (eg Proximity Grid, sometimes ROT13) -
paramStr, fromos. This can be better on holes that don't requirepreludeand only use each arg once. -
commandLineParams, fromos. Though very long, it can still be useful if want to work with args as an array without looping over them.
Examples:
# single arg reference with getopt
include prelude
for x in getopt():echo x[0]# multiple arg references with getopt
include prelude
for _,x,_ in getopt():echo x,x# paramStr
import os
for i in 1..99:echo i.paramStr# commandLineParams
import os
echo commandLineParams().join "\n"Instead of
=if x>0:A else:Buse
=[B,A][1.min x]Instead of
=if condition:A else:Buse
=[B,A][int condition]Logical and involving easily-negated bools, like
a==5 and b
a>5 and bcan be replaced with < and >:
a!=5<b
a<=5<bFor replacement heavy solutions, use
import re
let r=replaceor multiReplace.
Instead of using tables or case constructs, use two arrays of matching length. Find the index of a key using find and use that to access the value in the other array.
Use map, filter, all (or/and "It" variants) etc. from sequtils module.
Use
include preludeto import modules os, strutils, times, parseutils, hashes, tables, sets, sequtils, parseopt, strformat.
Furthermore, depending on what modules you need, you can find some module that imports what you need and include that instead. Some examples of such modules:
| Module | Imports |
|---|---|
| cgi | strutils, os, strtabs, cookies, uri |
| tables | since, hashes, math, algorithm |
| times | strutils, math, options, since, winlean, time_t |
| math | since, bitops, fenv |
| net | since, nativesockets, os, strutils, times, sets, options, monotimes, ssl_config, winlean, openssl, ssl_certs, winlean |
| json | hashes, tables, strutils, lexbase, streams, macros, parsejson, options, since |
| oids | hashes, times, endians, random, decode_helpers |
| re | pcre, strutils, rtarrays |