Python Example Code?

As a very late follow-up to that example, here is one approach to reading KM variables (see the reference to a KM variable named filePath at the end.

Untitled

/usr/local/bin/python3 <<PY_END 2>/dev/null
import functools

# foldl :: (a -> b -> a) -> a -> [b] -> a
def foldl(f, a, xs):
    return functools.reduce(f, xs, a);
  
# foldl1 :: (a -> a -> a) -> [a] -> a  
def foldl1(f, xs):
    return functools.reduce(f, xs[1:], xs[0]) if (len(xs) > 0) else None
    
# even :: Int -> Bool
def even(x):
    return 0 == x % 2;

def main():
    print (list(map(lambda x: x * 2, [1,2,3,4,5])));
    print (foldl(lambda x, y: x + y, 700, [1, 2, 3, 4, 5]));
    print (foldl1(lambda x, y: x + y, [1, 2, 3, 4, 5]));
    print (list(filter(even, [1,2,3,4,5])));

    print ("$KMVAR_filePath");
main()
PY_END
1 Like