<commit_before>! Copyright (C) 2020 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays fry kernel make sequences ;

IN: rosetta-code.multisplit

: ?pair ( ? x -- {?,x}/f )
    over [ 2array ] [ 2drop f ] if ;

: best-separator ( seq -- pos index )
    dup [ first ] map infimum '[ first _ = ] filter first first2 ;

: first-subseq ( separators seq -- n separator )
    dupd [ swap [ subseq-start ] dip ?pair ] curry map-index sift
    [ drop f f ] [ best-separator rot nth ] if-empty ;

: multisplit ( string separators -- seq )
    '[
        [ _ over first-subseq dup ] [
            length -rot cut-slice swap , swap tail-slice
        ] while 2drop ,
    ] { } make ;<commit_msg>rosetta-code.multisplit: reduce memory usage<commit_after>! Copyright (C) 2020 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays fry kernel make sequences ;

IN: rosetta-code.multisplit

: ?pair ( ? x -- {?,x}/f )
    over [ 2array ] [ 2drop f ] if ;

: best-separator ( seq -- pos index )
    dup [ first ] map infimum '[ first _ = ] find nip first2 ;

: first-subseq ( separators seq -- n separator )
    dupd [ swap [ subseq-start ] dip ?pair ] curry map-index sift
    [ drop f f ] [ best-separator rot nth ] if-empty ;

: multisplit ( string separators -- seq )
    '[
        [ _ over first-subseq dup ] [
            length -rot cut-slice swap , swap tail-slice
        ] while 2drop ,
    ] { } make ;
{'fraction_non_alphanumeric': 0.13043478260869565, 'fraction_numerical': 0.011042097998619738, 'mean_word_length': 2.403755868544601, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4145216', 'n_tokens_mistral': 496, 'n_tokens_neox': 473, 'n_words': 151}