"""This module is nonsense!"""
def main():
"""What is this, JavaScript?"""
print(1 + "1")
if __name__ == '__main__':
main()
$ python -mcompileall nonsense.py
Compiling nonsense.py ...
$ echo $?
0
$ pyflakes nonsense.py
$ echo $?
0
$ pylint --enable=all nonsense.py
[…]
Global evaluation
-----------------
Your code has been rated at 10.00/10
-module(nonsense).
-export([start/0]).
start() ->
io:format("~p", [1 + "1"]).
$ erlc -Werror nonsense.erl
compile: warnings being treated as errors
nonsense.erl:5: this expression will fail with a
'badarith' exception
$ echo $?
1
$ dialyzer nonsense.erl
nonsense.erl:4: Function start/0 has no local return
nonsense.erl:5: The call erlang:'+'(1,[49,...])
will never return since it differs in the 2nd
argument from the success typing arguments:
(number(),number())
done in 0m0.52s
done (warnings were emitted)
main = print (1 + "1")
main :: IO ()
main = print (1 + "1")
$ ghc nonsense.hs
[1 of 1] Compiling Main ( nonsense.hs, nonsense.o )
nonsense.hs:1:17:
No instance for (Num [Char]) arising from
a use of ‘+’
In the first argument of ‘print’,
namely ‘(1 + "1")’
In the expression: print (1 + "1")
In an equation for ‘main’: main = print (1 + "1")
No instance for (Num [Char]) arising from a use of '+'
+
, -
, *
, negate
belong to the Num typeclass (handwavingly similar to a Python ABC)"""Breadth-first traversal"""
def breadth_first(starting_node,
edges):
"""Yield all nodes reachable from starting_node"""
visited = []
queue = [starting_node]
while queue:
node = queue[0]; del queue[:1] # queue.pop(0)
if node not in visited:
yield node
visited.append(node)
queue.extend(edges[node])
"""Breadth-first traversal"""
from typing import List, Dict, Iterator
def breadth_first(starting_node: int,
edges: Dict[int, List[int]])
-> Iterator[int]:
"""Yield all nodes reachable from starting_node"""
visited = List[int]()
queue = [starting_node]
while queue:
node = queue[0]; del queue[:1] # queue.pop(0)
if node not in visited:
yield node
visited.append(node)
queue.extend(edges[node])
"""Breadth-first traversal"""
from typing import List, Dict, Set, Iterator
from collections import deque
def breadth_first(starting_node: int,
edges: Dict[int, List[int]])
-> Iterator[int]:
"""Yield all nodes reachable from starting_node"""
visited = Set[int]()
queue = deque([starting_node])
while queue:
node = queue[0]; del queue[:1] # queue.pop(0)
if node not in visited:
yield node
visited.append(node)
queue.extend(edges[node])
breadth_first2.py: In function "breadth_first":
breadth_first2.py, line 11:
deque[int] has no attribute "__delitem__"
breadth_first2.py, line 14:
Set[int] has no attribute "append"
"""Breadth-first traversal"""
from typing import Iterator, Dict, List, Set
from collections import deque
def breadth_first(starting_node: int,
edges: Dict[int, List[int]])
-> Iterator[int]:
"""Yield all nodes reachable from starting_node"""
visited = Set[int]()
queue = deque([starting_node])
while queue:
node = queue.popleft()
if node not in visited:
yield node
visited.add(node)
queue.extend(edges[node])
1 + "1"
error yet, but can catch many other mistakes such as the refactoring exampletyping
module even in vanilla Python 3typing
annotations in documentation toolsclass AST(object):
def eval(self):
raise NotImplementedError
class Constant(AST):
def __init__(self, value):
self.value = value
def eval(self):
return self.value
class Minus(AST):
def __init__(self, node):
self.node = node
def eval(self):
return self.node.eval()
class Add(AST):
def __init__(self, left, right):
self.left = left
self.right = right
def eval(self):
return self.left.eval() + self.right.eval()
class Multiply(AST):
def __init__(self, left, right):
self.left = left
self.right = right
def eval(self):
return self.left.eval() * self.right.eval()
module NumAST ( AST(..), eval ) where
data AST = Constant Int
| Add AST AST
| Minus AST
| Multiply AST AST
deriving (Show, Eq)
eval :: AST -> Int
eval node = case node of
Constant n -> n
Add a b -> eval a + eval b
Minus n -> negate (eval n)
Multiply a b -> eval a * eval b
class Add(AST):
def __init__(self, left, right):
self.left = left
self.right = right
def eval(self):
return self.left.eval() + self.right.eval()
def __eq__(self, other):
return (isinstance(other, Add) and
self.left == other.left and
self.right == other.right)
def __repr__(self):
return 'Add(%r, %r)' % (self.left, self.right)
newtype
)1 + True == 2
Slides |
|
Source |
|
bob@redivi.com |
|