#!/usr/bin/env -S sh +x -v # run this script and save its ANSI-styled results with the command # # sh +x -v ./tl-examples.sh 2>&1 | cat > tl-examples-result.txt ###################################################################### # run a (pure) python expression as a data-calculator; no input is read tl = '{n: 2**n for n in range(1, 11)}' {"1": 2, "2": 4, "3": 8, "4": 16, "5": 32, "6": 64, "7": 128, "8": 256, "9": 512, "10": 1024} # run a python expression as a data-calculator, showing a multiline JSON tl = 'json2({n: 2**n for n in range(1, 11)})' { "1": 2, "2": 4, "3": 8, "4": 16, "5": 32, "6": 64, "7": 128, "8": 256, "9": 512, "10": 1024 } # show numbers, one per line tl = 'range(5)' 0 1 2 3 4 # show numbers, one per line, end-value included tl = 'fromto(5, 10)' 5 6 7 8 9 10 # show base-10 logarithms for a few numbers seq 10 | tl 'f"log10({int(l):-3}) ~= {log10(float(l)):7.4f}"' log10( 1) ~= 0.0000 log10( 2) ~= 0.3010 log10( 3) ~= 0.4771 log10( 4) ~= 0.6021 log10( 5) ~= 0.6990 log10( 6) ~= 0.7782 log10( 7) ~= 0.8451 log10( 8) ~= 0.9031 log10( 9) ~= 0.9542 log10( 10) ~= 1.0000 # emit an extra empty line every 5 input lines seq 15 | tl '("", l) if i % 5 == 0 and i > 0 else l' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # filter lines; None results skip output for their current line seq 15 | tl 'l if not l.endswith("5") else None' 1 2 3 4 6 7 8 9 10 11 12 13 14 # filter lines concisely, by using boolean results seq 15 | tl 'not l.endswith("5")' 1 2 3 4 6 7 8 9 10 11 12 13 14 # color/style some text echo 'hi there!' | tl 'green(l)' hi there! # color/style some text echo 'hi there!' | tl 'greenback(l)' hi there! # pick 20 items from a list, allowing repetitions tl = 'choices(["abc", "def", "xyz"], k=20)' xyz def def abc abc xyz abc abc xyz abc xyz def xyz def def xyz abc abc def def # count/tally unique lines tl = 'choices(["abc", "def", "xyz"], k=20)' | tl -a 'tally(lines)' {"def": 7, "abc": 8, "xyz": 5} # avoid duplicates tl = 'choices(["abc", "def", "xyz"], k=20)' | tl 'once(l)' abc def xyz # auto-detect all http/https hyperlinks from a webpage, avoid repeating # auto-detected hyperlinks, and finally number unique results tl 'links(l)' https://www.bing.com | tl 'once(l)' | tl 'f"{i+1}\t{l}"' 1 https://www.bing.com/th?id=OHR.ThamesLondon_EN-CA7037142112_tmb.jpg& 2 https://www.bing.com/?form=HPFBBK& 3 https://r.bing.com 4 http://www.w3.org/2000/svg 5 https://r.bing.com/rs/6q/fU/jnc,nj/tlifxqsNyCzxIJnRwtQKuZToQQw.js?or=w 6 https://go.microsoft.com/fwlink/?linkid=2127455 7 https://bing.microsoftapp.net/bing/?adjust=nk9xri2_dzfz5n2 8 https://app.adjust.com/d93ddtl_a75k0gg?deeplink=sapphire%3A%2F%2Fwallpaper 9 https://www.msn.com/play?ocid=cgbinghp 10 https://assets.msn.com/bundles/v1/bingHomepage/latest/widget-initializer.js 11 https://r.bing.com/rb/5S/jnc,nj/nh-cbrH5PR-ER51QfBlGhxzAaq4.js?bu=BbQEuAS6BPoDowQ&or=w 12 https://business.bing.com/api/v3/search/person/photo?caller=IP 13 https://storage.live.com/users/0x 14 https://login.live.com/login.srf?wa=wsignin1.0 # figure out what some funcs do, via extra func `wat` (What Are These?) tl = 'wat(recover, dedup, once, dive, len, cond, log10)' recover Python Library Documentation: function recover in module __main__ recover(*args) -> Any Avoid exceptions using a lambda/callback func, in one of 4 ways recover(zero_args_func) recover(zero_args_func, exception_replacement_value) recover(one_arg_func, arg) recover(one_arg_func, arg, exception_replacement_value) dedup Python Library Documentation: function dedup in module __main__ dedup(v: Iterable) -> List[Any] Ignore reappearing items from iterables, resulting in a list. once Python Library Documentation: function once in module __main__ once(x: Any, replacement: Any = None) -> Any Replace the first argument given after the first time this func has been given it: this is a deliberately stateful function, given its purpose. dive Python Library Documentation: function dive in module __main__ dive(into: Any, doing: Callable) -> Any Transform a nested value by calling a func via depth-first recursion. len Python Library Documentation: built-in function len in module builtins len(obj, /) Return the number of items in a container. cond Python Library Documentation: function cond in module __main__ cond(*args: Any) -> Any Simulate a chain of if-else statements, using condition/result pairs from the arguments given; when given an even number of args, None is used as a final fallback result; when given an odd number of args, the last argument is used as a final `else` value, if needed. log10 Python Library Documentation: built-in function log10 in module math log10(x, /) Return the base 10 logarithm of x. # should add many more examples...