Hi, all!
 
Little did I know when I asked about a solution to the "path-finding task", that we actually have a vocab named `path-finding`, which does exactly what I wanted!
This is some next-level API : )
 
Anyway, I was able to implement my `db.upgrade` vocab in just a few lines of code:
 
IN: db.upgrade

! If DB has no version, simply storing the version number will give us
! v0.9. The version is stored automatically, so this word is empty.
: 0>0.9 ( -- ) ;

: 0.9>1 ( -- )
   [
       persisted-tuple new [
           [ trim-head-separators ] change-path
       ] update-tuples
   ] ignore-table-missing ;

ALIAS: 0>1 0.9>1

CONSTANT: transitions {
   { "0" { { "0.9" 0>0.9 } { "1" 0>1 } } }
   { "0.9" { { "1" 0.9>1 } } }
}

: search-map ( -- assoc )
   transitions [ keys ] assoc-map ;

: words ( graph-path -- assoc )
   2 <clumps> [ first2 swap transitions at dupd at ] { } map>assoc ;

: (upgrade-db) ( word-assoc -- )
   [
       [ execute( -- ) db-version boa store* drop ] with-transaction
   ] assoc-each ;

: upgrade-db ( current-version required-version -- done? )
   2dup = [ 2drop t ] [
       search-map <bfs> find-path dup [ words (upgrade-db) ] when*
       >boolean
   ] if ;
 
 All I need going forward is to keep adding new upgrading words and registering them in the `transitions` map. Then one call to `upgrade-db`, and I have the latest version of the DB structure/contents. I guess at some point I'll have to move the upgrading words into separate vocab(s) when the code would get too big or would require extra scaffolding, tuples, etc.
 
11.12.2018, 05:49, "John Benediktsson" <mrj...@gmail.com>:
We have some path finding stuff in the ``path-finding`` vocabulary, as well as some directed graph stuff in ``graphs`` vocabulary that is used by the compiler.
 
On Mon, Dec 10, 2018 at 11:43 AM Alexander Ilin <ajs...@yandex.ru> wrote:
Hey, guys!

  I have a new task, and I want to know if there are existing vocabs that can be employed for solving it.

  Let's say there are versions of a DB format, and there are quotations that can be executed to convert DB from one version to another. A bunch of these quotations are edges of a directed graph, and DB versions are nodes. The task is to find the shortest path in the graph (meaning with the smallest number of edges), or to declare that the path does not exist.

  Do we have a vocab that could be employed for the path-finding task?
 
---=====---
Александр
 
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to