Substring/Top and tail: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: ( comprehension in lieu of ap (<*>) ))
m (Reverted an inadvertent edit)
Line 1,340:
=={{header|Python}}==
 
<lang python>print "knight"[1:] # strip first character
print "socks"[:-1] # strip last character
print "brooms"[1:-1] # strip both first and last characters</lang>
 
 
Or, composing atomic functional expressions for these slices:
<lang python>from functools import (reduce)
 
Line 1,345 ⟶ 1,351:
def main():
print (
[fap('knights') for f in [tail, init, compose(init)(tail)]])(
["knights"]
)
)
 
Line 1,361 ⟶ 1,369:
def init(xs):
return xs[:-1]
 
 
# ap (<*>) :: [(a -> b)] -> [a] -> [b]
def ap(fs):
return lambda xs: reduce(
lambda a, f: a + reduce(
lambda a, x: a + [f(x)], xs, []
), fs, []
)