Skip to content

Commit

Permalink
shortuuid: implementing "shortuuid" concept.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Apr 19, 2012
1 parent ed3906e commit f9fb12d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README
Expand Up @@ -160,6 +160,9 @@ robohash:
second-color:
implements a "colored" seconds hash

shortuuid
implements "shortuuid" concept

simple-rpg:
simple role-playing game example

Expand Down
21 changes: 21 additions & 0 deletions shortuuid/shortuuid.factor
@@ -0,0 +1,21 @@
! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license

USING: kernel math sequences ;

IN: shortuuid

CONSTANT: alphabet
"23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

: (encode-uuid) ( n -- n c )
alphabet [ length /mod ] [ nth ] bi ;

: encode-uuid ( uuid -- shortuuid )
[ dup 0 > ] [ (encode-uuid) ] "" produce-as nip ;

: (decode-uuid) ( n c -- n )
alphabet index [ alphabet length * ] dip + ;

: decode-uuid ( shortuuid -- uuid )
<reversed> 0 [ (decode-uuid) ] reduce ;

0 comments on commit f9fb12d

Please sign in to comment.