-
Notifications
You must be signed in to change notification settings - Fork 0
/
onelinetest
86 lines (50 loc) · 1.22 KB
/
onelinetest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# convert a=2+2 and print a+a .... in one line of code
# Boston Python Meetup 11-17-2015 audience exercise. 2.7.x only.
# Demo: http//onelinepy.herokuapp.com
import math
#First the control
a=2+2
print(a+a)
# Labmas add a lot of flexibility
print (lambda a:a+a)(2+2)
# print [a + a for a in [2+ 2][0]
# Determine how to allow function def in blocks of clode:
# def f(x):
return x*10
# print f(3)
# print (lambda f: f(3))(lambda x: x*10)
# What about operations that don't assign to a variable?
do_something()
print 42
Since output value of do_something isn't used, funnel it to unused variable _.' \
print (lambda _: 42)(do_something())
# or
print (do_something(), 42)[1])
# put it all toggether
# if/else
if True:
x = 5
else:
x = 10
print x * 100
#...create conditional expression (B if A else C plus continuation passing
def continuation(x):
print x*100
if True:
x=5
continuation(x)
If False:
x=10
continuation(x)
# answer is a big nested lambda
# While loop
x = 5
def whilte_loop(x):
if x<20:
x=x+4
while_loop(x)
else:
print x
while_loop(x)
# Imports are not as hard as they seem ... __import__ takes care of that
rnd = __import__('random')