Skip to content

Instantly share code, notes, and snippets.

@egisatoshi
Last active August 29, 2015 13:57
Show Gist options
  • Save egisatoshi/9481005 to your computer and use it in GitHub Desktop.
Save egisatoshi/9481005 to your computer and use it in GitHub Desktop.
user =
  { <User 1 "Egison_Lang">
    <User 2 "__Egi">
    ...
   }

follow =
  { <Follow 1 101 201>
    ...
   }
SELECT * FROM user WHERE name = '__Egi';
(match-all user user-table [<cons <user $id ,"__Egi"> _> <user id "__Egi">])
SELECT * FROM user WHERE id < 100;
(match-all user user-table [<cons <user (& ?(lt? $ 100) $id) $name> _> <user id name>])
(match-all [user follow user] [user-table follow-table user-table]
          [<cons <user $id ,"__Egi"> _>
           <cons <follow ,id $fid>
            <cons <follow ,fid ,id>
             _>>
           <cons <user ,fid $sn> _>]
  {<user fid sn>}
SELECT name
  FROM user
  WHERE id IN (SELECT to_id
                  FROM follow
                  WHERE from_id = (SELECT id FROM user WHERE name = '__Egi')
                    AND to_id NOT IN (SELECT follow1.from_id
                                         FROM user as user1, follow as follow1
                                         WHERE user1.name = '__Egi'
                                           AND user1.id = follow1.to_id)

or

SELECT DISTINCT ON (user4.name) user4.name
  FROM user AS user1,
       follow AS follow2,
       user AS user4
  WHERE user1.name = '__Egi'
    AND follow2.from_id = user1.id
    AND user4.id = follow2.to_id
    AND NOT EXISTS
      (SELECT '' 
         FROM follow AS follow3
         WHERE follow3.from_id = follow2.to_id
           AND follow3.to_id = user1.id)
  ORDER BY user4.name;
(match-all [user follow user] [user-table follow-table user-table]
          [<cons <user $id ,"__Egi"> _>
           <cons <follow ,id $fid>
            ^<cons <follow ,fid ,id>
              _>>
           <cons <user ,fid $sn> _>]
  {<user fid sn>}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment