sneq Tutorial
sneq is not Python
If you don't know Python, perfect! You can skip this section.
If you have a background in programming Python, at first glance sneq will seem familiar. You may be tempted to import various Pythonisms into sneq, but you must be aware that sneq is not Python.
Trying to code sneq like you would code Python will make you upset because it will crash.
In sneq, the only time you are allowed to use anonymous function arguments is for built-ins. Functions that are not built-in require you to name your arguments.
def foo_bad(bar): # Not okay.
return bar
def foo_good(bar=None): # Good!
return bar
unique([1, 3, 2, 2]) # Good! "unique" is a built-in.
Functions must be pure. In order to access a variable within the scope of another function, it must be passed to that function.
foo = "bar"
def do_a_thing_bad():
return foo # Not okay. "foo" does not exist in the functions scope.
def do_a_thing_good(foo=foo):
return foo # Good! "foo" is passed into the function as "foo".
Both lists and dicts are immutable.
foo = [1, 2, 3]
foo[1] = 5 # Not okay. You are not allowed to mutate "foo".
foo = [1, 5, 3] # Good! "foo" has been redeclared.
foo = { "bar": 12345 }
foo["bar"] = 42 # Not okay. You are not allowed to mutate "foo".
foo = { "bar": 42 } # Good! "foo" has been redeclared.
Getting sublists is a little more complicated. You need to use the utility function sublist.
lst = [1, 2, 3, 4, 5, 6]
lst = lst[1:-2] # Not okay. This is Python syntax.
lst = util.list.sublist(start=1, end=-2) # Good!
Type mismatches will cause a crash. To prevent type mismatches, detect types and using branching logic to handle expected cases.
two = 2
two_str = "2"
foo = two + two_str # Not okay. This will crash.
foo = None
if isinstance(foo_str, 'str'):
foo = two + dec(foo_str) # Good! We cast "foo_str" to the right type.
Unlike Python, numbers are not integers by default. Like in JavaScript, numbers can have decimal places by default. Unlike JavaScript, numbers are are decimals rather than floats. Because of this, you will not encounter floating point errors, but if you want to use integers, you must cast them with int
.
foo = 0.1 + 0.2 # 0.3, not 0.30000000000000004.
foo = 10 # This is a decimal, not an integer.
foo = int(10) # This is an integer and will crash if you add it to a decimal.
Unlike Python, you can not have a statement without an assignment or without returning something.
2 + 2 # Not okay. You need to assign this to something.
foo = 2 + 2 # Good! The value has been assigned.
def foo():
return # Not okay. The function does not return anything.
def foo():
return None # Good! The function returns None.
In Python normally the first startment in boolean logic executes before all the others, but this is not the case in sneq. All statements in conditional logic will be evaluated in sneq before the comparison occurs.
# Not okay! 2 + "2" will be evaluated still and cause a crash.
foo = True or bool(2 + "2")
In your sneq journey you will find many inconsistencies with Python, because sneq is not Python. There are no classes, there is no try: catch: finally:, and other parts of the syntax are dramatically simplified. Rest assured that if you need to do something that could be done in simple Python, it can probably also be done in sneq, potentially with some extra leg work.