Memoize

BasicWerk   EC Support   Technique   Facebook  

20140716101254_factor_hashtables

factor_hashtables

 
! hashtable
IN: scratchpad auto-use SYMBOL: h
IN: scratchpad auto-use H{ } h set
 
! sample: each-index
IN: scratchpad auto-use { "one" "two" } [ 1 + swap 2array . ] each-index
{ 1 "one" }
{ 2 "two" }
 
! set-at
IN: scratchpad auto-use { "one" "two" } [ 1 + h get set-at ] each-index
IN: scratchpad auto-use h get .
H{ { 1 "one" } { 2 "two" } }
IN: scratchpad auto-use "three" 3 h get set-at
IN: scratchpad auto-use h get .
H{ { 1 "one" } { 2 "two" } { 3 "three" } }
 
! keys
IN: scratchpad auto-use h get keys .
{ 1 2 3 }
 
! values
IN: scratchpad auto-use h get values .
{ "one" "two" "three" }
 
 
! value が "t" で始まる要素のみに filter
IN: scratchpad auto-use h get keys
 
--- Data stack:
{ 1 2 3 }
 
IN: scratchpad auto-use [ h get at R/ ^t/i re-contains? ] filter
 
--- Data stack:
{ 2 3 }
 
IN: scratchpad auto-use [ dup h get at 2array ] map >hashtable
 
--- Data stack:
H{ { 2 "two" } { 3 "three" } }
 

 

http://hyperpolyglot.org/stack

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

http://docs.factorcode.org/content/vocab-hashtables.html

 


© Shin Nakamura/BasicWerk 2014