↑ Up |
In the case of a well-known conversational programming language I have been told from various sides that as soon as a programming community is equipped with a terminal for it, a specific phenomenon occurs that even has a wellestablished name: it is called “the one-liners”. It takes one of two different forms: one programmer places a one-line program on the desk of another and either he proudly tells what it does and adds the qustion, “Can you code this in less symbols?” — as if this were of any conceptual relevance! — or he just says, “Guess what it does!” From this observation, we must conclude that this language as a tool is an open invitation for clever tricks; and while exactly this may be the explanation for some of is appeal, viz. to those who like to show how clever they are, I am sorry, but I must regard this as one of the most damning things that can be said about a programming language.
E. W. Dijkstra (1987): The Humble Programmer.
List.map = |a;f| [] if a==[] else [f(a[0])]+a[1..].map(f) List.filter = |a;p| ([] if a==[] else ([a[0]] if p(a[0]) else [])+a[1..].filter(p)) List.reduce = |a;e,f| e if a==[] else f(a[0],a[1..].reduce(e,f)) List.map = |a;f| a.reduce([],|t,x| t+[f(x)]) List.filter = |a;p| a.reduce([],|t,x| t+[x] if p(x) else t)
chain = |a| list(x for t in a for x in t) a = list((1..10).chunks(2)) print(chain(a))
qsort = |a| ([] if a==[] else qsort(a[1..].filter(|x| x<a[0]))+[a[0]]+ qsort(a[1..].filter(|x| x>=a[0])))
# Print all permutations of [0,1,2]. (list(3)^3).filter(|t| (0..2).all(|x| x in t))).each(print)
# Print the first 100 prime numbers. print((1..).filter(|n| (1..n).count(|k| n%k==0)==2).list(100)) # Calculate the prime factorization of 360. isprime = |n| (1..n).count(|k| n%k==0)==2 factor = |n| (list(2..n).filter(|k| n%k==0 and isprime(k)) .map(|p| [p,(1..).until(|k| n%(p^k)!=0).count()])) print(factor(360))
# Convert f(x,y) to f(x)(y). curry = fn|f| n = f.argc(); a = list(n); (2..n).reduce(fn|x| a[n-1] = x; f(*a) end, |g,i| fn|x| a[n-i] = x; g end) end # Convert f(x)(y) to f(x,y). uncurry = |f| |*a| a.reduce(f,|g,x| g(x))
# Variadic, fixes first arguments. first = |f,*a| |*b| f(*(a+b)) # Variadic, fixes last arguments. last = |f,*a| |*b| f(*(b+a))
# Calculate the power set of {0,1,2}. P = |a| a.reduce({{}},|p,x| p|set(p.map(|s| s|{x}))) print(P({0,1,2}))
(0..25).map(|x| (0..39).map(fn|y| c = (x/10-2)+(y/10-2)*1i; "*" if abs(((|z| z^2+c)^10)(0))<2 else "\s" end).join()).each(print)
(1..100).map(|i| [i]+[3,5].map(|d| int(i%d==0))).map(|[i,x,y]| i if x+y==0 else "Fizz"*x+"Buzz"*y).each(print)