<commit_before>! Copyright (C) 2012 <NAME>
! See http://factorcode.org/license.txt for BSD license

USING: arrays assocs combinators.short-circuit command-line
environment io io.backend io.files io.files.info io.pathnames
kernel namespaces sequences sets splitting system unicode.case ;

IN: tools.which

<PRIVATE

: executable? ( path -- ? )
    {
        [ exists? ]
        [ file-executable? ]
        [ file-info directory? not ]
    } 1&& ;

: split-path ( paths -- seq )
    os windows? ";" ":" ? split harvest ;

: path-extensions ( command -- commands )
    "PATHEXT" os-env [
        split-path 2dup [ [ >lower ] bi@ tail? ] with any?
        [ drop 1array ] [ [ append ] with map ] if
    ] [ 1array ] if* ;

: ((which)) ( commands paths -- file/f )
    [ normalize-path ] map members
    cartesian-product flip concat
    [ prepend-path ] { } assoc>map
    [ executable? ] find nip ;

: (which) ( command path -- file/f )
    split-path os windows? [
        [ path-extensions ] [ "." prefix ] bi*
    ] [ [ 1array ] dip ] if ((which)) ;

PRIVATE>

: which ( command -- file/f )
    "PATH" os-env (which) ;

: run-which ( -- )
    command-line get [ which [ print ] when* ] each ;

MAIN: run-which<commit_msg>tools.which: add a convenience for "which or".<commit_after>! Copyright (C) 2012 <NAME>
! See http://factorcode.org/license.txt for BSD license

USING: arrays assocs combinators.short-circuit command-line
environment io io.backend io.files io.files.info io.pathnames
kernel namespaces sequences sets splitting system unicode.case ;

IN: tools.which

<PRIVATE

: executable? ( path -- ? )
    {
        [ exists? ]
        [ file-executable? ]
        [ file-info directory? not ]
    } 1&& ;

: split-path ( paths -- seq )
    os windows? ";" ":" ? split harvest ;

: path-extensions ( command -- commands )
    "PATHEXT" os-env [
        split-path 2dup [ [ >lower ] bi@ tail? ] with any?
        [ drop 1array ] [ [ append ] with map ] if
    ] [ 1array ] if* ;

: ((which)) ( commands paths -- file/f )
    [ normalize-path ] map members
    cartesian-product flip concat
    [ prepend-path ] { } assoc>map
    [ executable? ] find nip ;

: (which) ( command path -- file/f )
    split-path os windows? [
        [ path-extensions ] [ "." prefix ] bi*
    ] [ [ 1array ] dip ] if ((which)) ;

PRIVATE>

: which ( command -- file/f )
    "PATH" os-env (which) ;

: ?which ( command -- file/command )
    [ which ] [ or ] bi ;

: run-which ( -- )
    command-line get [ which [ print ] when* ] each ;

MAIN: run-which
{'fraction_non_alphanumeric': 0.14769594328475777, 'fraction_numerical': 0.0070894052776683735, 'mean_word_length': 2.413978494623656, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1530440', 'n_tokens_mistral': 858, 'n_tokens_neox': 823, 'n_words': 233}