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: | Line 1,340: | ||
=={{header|Python}}== |
=={{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) |
<lang python>from functools import (reduce) |
||
Line 1,345: | Line 1,351: | ||
def main(): |
def main(): |
||
print ( |
print ( |
||
ap([tail, init, compose(init)(tail)])( |
|||
["knights"] |
|||
) |
|||
) |
) |
||
Line 1,361: | Line 1,369: | ||
def init(xs): |
def init(xs): |
||
return xs[:-1] |
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, [] |
|||
) |
|||