Posted to tcl by aspect at Wed Apr 15 00:16:42 GMT 2015view pretty

    # returns a script which composes the given list of commands:
    # this has elsewhere been called [pipe], [cmdpipe], [~>]
    proc cmdpipe args {
        set anonvar ~
        set args [lassign $args body]
        foreach cmd $args {
            if {[string first $anonvar $cmd] >= 0} {
                set body [string map [list $anonvar "\[$body\]"] $cmd]
            } else {
                set body "$cmd \[$body\]"
            }
        }
        set body
    }

    # example:
    -- {
        set s [cmdpipe {*}{
            {open /etc/passwd r}
            {read}
            {string trim}
            {split ~ \n}
            {lindex ~ end}
            {split ~ :}
            {lindex ~ 4}
            {puts}
        }]
    }

    # another example:
    proc lremove {_ls args} {
        set script [cmdpipe [list set $_ls] {*}[map {list lsearch -exact -not -all -inline ~} $args] [list set $_ls]]
        tailcall try $script
    }