summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--de-de/yaml-de.html.markdown2
-rw-r--r--lua.html.markdown169
-rw-r--r--python3.html.markdown21
-rw-r--r--red.html.markdown10
-rw-r--r--solidity.html.markdown1
5 files changed, 107 insertions, 96 deletions
diff --git a/de-de/yaml-de.html.markdown b/de-de/yaml-de.html.markdown
index 25f2edc4..d0e3471a 100644
--- a/de-de/yaml-de.html.markdown
+++ b/de-de/yaml-de.html.markdown
@@ -10,7 +10,7 @@ lang: de-de
YAML ist eine Sprache zur Datenserialisierung, die sofort von Menschenhand geschrieben und gelesen werden kann.
-YAML ist ein Erweiterung von von JSON mit der Erweiterung um syntaktisch wichtige Zeilenumbrüche und Einrückungen, ähnlich wie auch in Python. Anders als in Python allerdings erlaubt YAML keine Tabulator-Zeichen.
+YAML ist ein Erweiterung von JSON mit der Erweiterung um syntaktisch wichtige Zeilenumbrüche und Einrückungen, ähnlich wie auch in Python geschrieben werden können. Anders als in Python allerdings erlaubt YAML keine Tabulator-Zeichen.
```yaml
# Kommentare in YAML schauen so aus.
diff --git a/lua.html.markdown b/lua.html.markdown
index 32174a81..0a7c4f00 100644
--- a/lua.html.markdown
+++ b/lua.html.markdown
@@ -12,13 +12,15 @@ filename: learnlua.lua
Adding two ['s and ]'s makes it a
multi-line comment.
--]]
---------------------------------------------------------------------------------
+
+----------------------------------------------------
-- 1. Variables and flow control.
---------------------------------------------------------------------------------
+----------------------------------------------------
num = 42 -- All numbers are doubles.
--- Don't freak out, 64-bit doubles have 52 bits for storing exact int
--- values; machine precision is not a problem for ints that need < 52 bits.
+-- Don't freak out, 64-bit doubles have 52 bits for
+-- storing exact int values; machine precision is
+-- not a problem for ints that need < 52 bits.
s = 'walternate' -- Immutable strings like Python.
t = "double-quotes are also fine"
@@ -58,15 +60,10 @@ aBoolValue = false
-- Only nil and false are falsy; 0 and '' are true!
if not aBoolValue then print('twas false') end
--- 'or' and 'and' are short-circuited. This is similar to the a?b:c operator
--- in C/js:
+-- 'or' and 'and' are short-circuited.
+-- This is similar to the a?b:c operator in C/js:
ans = aBoolValue and 'yes' or 'no' --> 'no'
--- BEWARE: this only acts as a ternary if the value returned when the condition
--- evaluates to true is not `false` or Nil
-iAmNotFalse = (not aBoolValue) and false or true --> true
-iAmAlsoNotFalse = (not aBoolValue) and true or false --> true
-
karlSum = 0
for i = 1, 100 do -- The range includes both ends.
karlSum = karlSum + i
@@ -84,19 +81,20 @@ repeat
num = num - 1
until num == 0
---------------------------------------------------------------------------------
+
+----------------------------------------------------
-- 2. Functions.
---------------------------------------------------------------------------------
+----------------------------------------------------
function fib(n)
- if n < 2 then return n end
+ if n < 2 then return 1 end
return fib(n - 2) + fib(n - 1)
end
-- Closures and anonymous functions are ok:
function adder(x)
- -- The returned function is created when adder is called, and remembers the
- -- value of x:
+ -- The returned function is created when adder is
+ -- called, and remembers the value of x:
return function (y) return x + y end
end
a1 = adder(9)
@@ -104,9 +102,10 @@ a2 = adder(36)
print(a1(16)) --> 25
print(a2(64)) --> 100
--- Returns, func calls, and assignments all work with lists that may be
--- mismatched in length. Unmatched receivers are nil; unmatched senders are
--- discarded.
+-- Returns, func calls, and assignments all work
+-- with lists that may be mismatched in length.
+-- Unmatched receivers are nil;
+-- unmatched senders are discarded.
x, y, z = 1, 2, 3, 4
-- Now x = 1, y = 2, z = 3, and 4 is thrown away.
@@ -119,15 +118,13 @@ end
x, y = bar('zaphod') --> prints "zaphod nil nil"
-- Now x = 4, y = 8, values 15..42 are discarded.
--- Functions are first-class, may be local/global. These are the same:
+-- Functions are first-class, may be local/global.
+-- These are the same:
function f(x) return x * x end
f = function (x) return x * x end
-- And so are these:
local function g(x) return math.sin(x) end
-local g = function(x) return math.sin(x) end
--- Equivalent to local function g(x)..., except referring to g in the function
--- body won't work as expected.
local g; g = function (x) return math.sin(x) end
-- the 'local g' decl makes g-self-references ok.
@@ -136,16 +133,15 @@ local g; g = function (x) return math.sin(x) end
-- Calls with one string param don't need parens:
print 'hello' -- Works fine.
--- Calls with one table param don't need parens either (more on tables below):
-print {} -- Works fine too.
---------------------------------------------------------------------------------
+----------------------------------------------------
-- 3. Tables.
---------------------------------------------------------------------------------
+----------------------------------------------------
--- Tables = Lua's only compound data structure; they are associative arrays.
--- Similar to php arrays or js objects, they are hash-lookup dicts that can
--- also be used as lists.
+-- Tables = Lua's only compound data structure;
+-- they are associative arrays.
+-- Similar to php arrays or js objects, they are
+-- hash-lookup dicts that can also be used as lists.
-- Using tables as dictionaries / maps:
@@ -161,13 +157,14 @@ t.key2 = nil -- Removes key2 from the table.
u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
print(u[6.28]) -- prints "tau"
--- Key matching is basically by value for numbers and strings, but by identity
--- for tables.
+-- Key matching is basically by value for numbers
+-- and strings, but by identity for tables.
a = u['@!#'] -- Now a = 'qbert'.
b = u[{}] -- We might expect 1729, but it's nil:
--- b = nil since the lookup fails. It fails because the key we used is not the
--- same object as the one used to store the original value. So strings &
--- numbers are more portable keys.
+-- b = nil since the lookup fails. It fails
+-- because the key we used is not the same object
+-- as the one used to store the original value. So
+-- strings & numbers are more portable keys.
-- A one-table-param function call needs no parens:
function h(x) print(x.key1) end
@@ -187,15 +184,16 @@ v = {'value1', 'value2', 1.21, 'gigawatts'}
for i = 1, #v do -- #v is the size of v for lists.
print(v[i]) -- Indices start at 1 !! SO CRAZY!
end
--- A 'list' is not a real type. v is just a table with consecutive integer
--- keys, treated as a list.
+-- A 'list' is not a real type. v is just a table
+-- with consecutive integer keys, treated as a list.
---------------------------------------------------------------------------------
+----------------------------------------------------
-- 3.1 Metatables and metamethods.
---------------------------------------------------------------------------------
+----------------------------------------------------
--- A table can have a metatable that gives the table operator-overloadish
--- behaviour. Later we'll see how metatables support js-prototype behaviour.
+-- A table can have a metatable that gives the table
+-- operator-overloadish behavior. Later we'll see
+-- how metatables support js-prototypey behavior.
f1 = {a = 1, b = 2} -- Represents the fraction a/b.
f2 = {a = 2, b = 3}
@@ -205,7 +203,7 @@ f2 = {a = 2, b = 3}
metafraction = {}
function metafraction.__add(f1, f2)
- local sum = {}
+ sum = {}
sum.b = f1.b * f2.b
sum.a = f1.a * f2.b + f2.a * f1.b
return sum
@@ -216,9 +214,10 @@ setmetatable(f2, metafraction)
s = f1 + f2 -- call __add(f1, f2) on f1's metatable
--- f1, f2 have no key for their metatable, unlike prototypes in js, so you must
--- retrieve it as in getmetatable(f1). The metatable is a normal table with
--- keys that Lua knows about, like __add.
+-- f1, f2 have no key for their metatable, unlike
+-- prototypes in js, so you must retrieve it as in
+-- getmetatable(f1). The metatable is a normal table
+-- with keys that Lua knows about, like __add.
-- But the next line fails since s has no metatable:
-- t = s + s
@@ -230,12 +229,11 @@ myFavs = {food = 'pizza'}
setmetatable(myFavs, {__index = defaultFavs})
eatenBy = myFavs.animal -- works! thanks, metatable
---------------------------------------------------------------------------------
--- Direct table lookups that fail will retry using the metatable's __index
--- value, and this recurses.
+-- Direct table lookups that fail will retry using
+-- the metatable's __index value, and this recurses.
--- An __index value can also be a function(tbl, key) for more customized
--- lookups.
+-- An __index value can also be a function(tbl, key)
+-- for more customized lookups.
-- Values of __index,add, .. are called metamethods.
-- Full list. Here a is a table with the metamethod.
@@ -256,19 +254,19 @@ eatenBy = myFavs.animal -- works! thanks, metatable
-- __newindex(a, b, c) for a.b = c
-- __call(a, ...) for a(...)
---------------------------------------------------------------------------------
+----------------------------------------------------
-- 3.2 Class-like tables and inheritance.
---------------------------------------------------------------------------------
+----------------------------------------------------
--- Classes aren't built in; there are different ways to make them using
--- tables and metatables.
+-- Classes aren't built in; there are different ways
+-- to make them using tables and metatables.
-- Explanation for this example is below it.
Dog = {} -- 1.
function Dog:new() -- 2.
- local newObj = {sound = 'woof'} -- 3.
+ newObj = {sound = 'woof'} -- 3.
self.__index = self -- 4.
return setmetatable(newObj, self) -- 5.
end
@@ -281,59 +279,62 @@ mrDog = Dog:new() -- 7.
mrDog:makeSound() -- 'I say woof' -- 8.
-- 1. Dog acts like a class; it's really a table.
--- 2. "function tablename:fn(...)" is the same as
--- "function tablename.fn(self, ...)", The : just adds a first arg called
--- self. Read 7 & 8 below for how self gets its value.
+-- 2. function tablename:fn(...) is the same as
+-- function tablename.fn(self, ...)
+-- The : just adds a first arg called self.
+-- Read 7 & 8 below for how self gets its value.
-- 3. newObj will be an instance of class Dog.
--- 4. "self" is the class being instantiated. Often self = Dog, but inheritance
--- can change it. newObj gets self's functions when we set both newObj's
--- metatable and self's __index to self.
+-- 4. self = the class being instantiated. Often
+-- self = Dog, but inheritance can change it.
+-- newObj gets self's functions when we set both
+-- newObj's metatable and self's __index to self.
-- 5. Reminder: setmetatable returns its first arg.
--- 6. The : works as in 2, but this time we expect self to be an instance
--- instead of a class.
+-- 6. The : works as in 2, but this time we expect
+-- self to be an instance instead of a class.
-- 7. Same as Dog.new(Dog), so self = Dog in new().
-- 8. Same as mrDog.makeSound(mrDog); self = mrDog.
---------------------------------------------------------------------------------
+----------------------------------------------------
-- Inheritance example:
LoudDog = Dog:new() -- 1.
function LoudDog:makeSound()
- local s = self.sound .. ' ' -- 2.
+ s = self.sound .. ' ' -- 2.
print(s .. s .. s)
end
seymour = LoudDog:new() -- 3.
seymour:makeSound() -- 'woof woof woof' -- 4.
---------------------------------------------------------------------------------
-- 1. LoudDog gets Dog's methods and variables.
-- 2. self has a 'sound' key from new(), see 3.
--- 3. Same as "LoudDog.new(LoudDog)", and converted to "Dog.new(LoudDog)" as
--- LoudDog has no 'new' key, but does have "__index = Dog" on its metatable.
--- Result: seymour's metatable is LoudDog, and "LoudDog.__index = Dog". So
--- seymour.key will equal seymour.key, LoudDog.key, Dog.key, whichever
+-- 3. Same as LoudDog.new(LoudDog), and converted to
+-- Dog.new(LoudDog) as LoudDog has no 'new' key,
+-- but does have __index = Dog on its metatable.
+-- Result: seymour's metatable is LoudDog, and
+-- LoudDog.__index = LoudDog. So seymour.key will
+-- = seymour.key, LoudDog.key, Dog.key, whichever
-- table is the first with the given key.
--- 4. The 'makeSound' key is found in LoudDog; this is the same as
--- "LoudDog.makeSound(seymour)".
+-- 4. The 'makeSound' key is found in LoudDog; this
+-- is the same as LoudDog.makeSound(seymour).
-- If needed, a subclass's new() is like the base's:
function LoudDog:new()
- local newObj = {}
+ newObj = {}
-- set up newObj
self.__index = self
return setmetatable(newObj, self)
end
---------------------------------------------------------------------------------
+----------------------------------------------------
-- 4. Modules.
---------------------------------------------------------------------------------
+----------------------------------------------------
---[[ I'm commenting out this section so the rest of this script remains
--- runnable.
+--[[ I'm commenting out this section so the rest of
+-- this script remains runnable.
```
```lua
@@ -359,8 +360,8 @@ local mod = require('mod') -- Run the file mod.lua.
local mod = (function ()
<contents of mod.lua>
end)()
--- It's like mod.lua is a function body, so that locals inside mod.lua are
--- invisible outside it.
+-- It's like mod.lua is a function body, so that
+-- locals inside mod.lua are invisible outside it.
-- This works because mod here = M in mod.lua:
mod.sayHello() -- Says hello to Hrunkner.
@@ -368,19 +369,19 @@ mod.sayHello() -- Says hello to Hrunkner.
-- This is wrong; sayMyName only exists in mod.lua:
mod.sayMyName() -- error
--- require's return values are cached so a file is run at most once, even when
--- require'd many times.
+-- require's return values are cached so a file is
+-- run at most once, even when require'd many times.
-- Suppose mod2.lua contains "print('Hi!')".
local a = require('mod2') -- Prints Hi!
local b = require('mod2') -- Doesn't print; a=b.
-- dofile is like require without caching:
-dofile('mod2') --> Hi!
-dofile('mod2') --> Hi! (runs again, unlike require)
+dofile('mod2.lua') --> Hi!
+dofile('mod2.lua') --> Hi! (runs it again)
-- loadfile loads a lua file but doesn't run it yet.
-f = loadfile('mod2') -- Calling f() runs mod2.lua.
+f = loadfile('mod2.lua') -- Call f() to run it.
-- loadstring is loadfile for strings.
g = loadstring('print(343)') -- Returns a function.
diff --git a/python3.html.markdown b/python3.html.markdown
index ff527716..4d5bb3ae 100644
--- a/python3.html.markdown
+++ b/python3.html.markdown
@@ -72,15 +72,24 @@ not False # => True
True and False # => False
False or True # => True
-# Note using Bool operators with ints
-# False is 0 and True is 1
+# True and False are actually 1 and 0 but with different keywords
+True + True # => 2
+True * 8 # => 8
+False - 5 # => -5
+
+# Comparison operators look at the numerical value of True and False
+0 == False # => True
+1 == True # => True
+2 == True # => False
+-5 != False # => True
+
+# Using boolean logical operators on ints casts them to booleans for evaluation, but their non-cast value is returned
# Don't mix up with bool(ints) and bitwise and/or (&,|)
+bool(0) # => False
+bool(4) # => True
+bool(-6) # => True
0 and 2 # => 0
-5 or 0 # => -5
-0 == False # => True
-2 == True # => False
-1 == True # => True
--5 != False != True #=> True
# Equality is ==
1 == 1 # => True
diff --git a/red.html.markdown b/red.html.markdown
index 73baf462..74538bd7 100644
--- a/red.html.markdown
+++ b/red.html.markdown
@@ -114,12 +114,12 @@ i2 * i1 ; result 2
i1 / i2 ; result 0 (0.5, but truncated towards 0)
; Comparison operators are probably familiar, and unlike in other languages
-; you only need a single '=' sign for comparison.
+; you only need a single '=' sign for comparison. Inequality is '<>' like in Pascal.
; There is a boolean like type in Red. It has values true and false, but also
; the values on/off or yes/no can be used
3 = 2 ; result false
-3 != 2 ; result true
+3 <> 2 ; result true
3 > 2 ; result true
3 < 2 ; result false
2 <= 2 ; result true
@@ -129,8 +129,8 @@ i1 / i2 ; result 0 (0.5, but truncated towards 0)
; Control Structures
;
; if
-; Evaluate a block of code if a given condition is true. IF does not return
-; any value, so cannot be used in an expression.
+; Evaluate a block of code if a given condition is true. IF returns
+; the resulting value of the block or 'none' if the condition was false.
if a < 0 [print "a is negative"]
; either
@@ -165,7 +165,7 @@ print ["a is " msg lf]
; until
; Loop over a block of code until the condition at end of block, is met.
-; UNTIL does not return any value, so it cannot be used in an expression.
+; UNTIL always returns the 'true' value from the final evaluation of the last expression.
c: 5
until [
prin "o"
diff --git a/solidity.html.markdown b/solidity.html.markdown
index acf750f7..4ff770eb 100644
--- a/solidity.html.markdown
+++ b/solidity.html.markdown
@@ -831,6 +831,7 @@ someContractAddress.callcode('function_name');
## Additional resources
- [Solidity Docs](https://solidity.readthedocs.org/en/latest/)
- [Smart Contract Best Practices](https://github.com/ConsenSys/smart-contract-best-practices)
+- [Superblocks Lab - Browser based IDE for Solidity](https://lab.superblocks.com/)
- [EthFiddle - The JsFiddle for Solidity](https://ethfiddle.com/)
- [Browser-based Solidity Editor](https://remix.ethereum.org/)
- [Gitter Solidity Chat room](https://gitter.im/ethereum/solidity)