Memoize

BasicWerk   EC Support   Technique   Facebook  

20140630191031_factor_split-by-one_indexOf

factor_split-by-one_indexOf

 
! Copyright (C) 2014 Shin Nakamura.
! See http://factorcode.org/license.txt for BSD license.
USING: 
    kernel sequences fry arrays
    assocs sets
    locals math math.ranges
;
IN: my-seq
 
! some other words ...
 
! 文字列を1文字ずつに切り分ける
! empty? だったらそのまま返す
: split-by-one ( str -- seq )
    [let :> str 
        str empty?
        [ str ]
        [ str length 1 - [0,b] 
          [ dup 1 + str subseq ] map
        ] if
    ]
;
 
! Like JavaScript
: indexOf ( elt str -- index )
    split-by-one index
;
 

 

 
IN: scratchpad USE: my-seq
 
IN: scratchpad "" split-by-one .
""
 
IN: scratchpad "a" split-by-one .
{ "a" }
 
IN: scratchpad "ssssss" split-by-one .
{ "s" "s" "s" "s" "s" "s" }
 
IN: scratchpad auto-use "y" "xyz" split-by-one index .
1
 
IN: scratchpad auto-use "y" "" split-by-one index .
f
 
IN: scratchpad auto-use "y" "xyz" indexOf .
1
 

 

http://docs.factorcode.org/content/word-subseq,sequences.html

http://docs.factorcode.org/content/word-index,sequences.html

http://hyperpolyglot.org/stack

 


© Shin Nakamura/BasicWerk 2014