Skip to content

Commit

Permalink
sometimes its good not to be able to sleep -> very nice projection!
Browse files Browse the repository at this point in the history
Schweppe will love it:
* a cool list komprehantion
* wich is used inside a higher oder function, that will filter the wanted
  things of any list
* not we just can run that filter on header and all rows,
its so beautifull!!!!1!!1!one!!
  • Loading branch information
wabu authored and Daniel Waeber ext:(web%3Cdot%3Ede%3E) committed Jul 18, 2007
1 parent 8a784a8 commit 9fc71e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
49 changes: 30 additions & 19 deletions src/SetTableOps.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,25 @@ more complex and less readable.

Projection

TODO: projectUnsafe?
FIXME: remove decleartion, default one is nicer?
TODO: optimize! this is insanley slow :-(

> project wantedNames tab@(SetTab header rows)
> | checkHeader = SetTab newHeader newRows
> where
> checkHeader = all (\n -> checkSize n (length [a | (a,_) <- header, n == a])) wantedNames
> checkSize :: String -> Int -> Bool
> checkSize n s
> | s == 0 = error ("project: could not project to "
> ++ show n ++ ": name unknown.")
> | s == 1 = True
> | otherwise = error ("project: could not project to "
> ++ show n ++ ": name is ambigious.")
> posList = [i | s <- wantedNames, (i,(n,_)) <- zip [1..] header, n==s ]
> newHeader = [h | p <- posList, (i,h) <- zip [1..] header, p==i ]
> newRows = Set.map
> (\r -> [ l | p <- posList, (i,l) <- zip [1..] r, p==i] )
> rows
> project wantedNames tab@(SetTab header rows)
> | checkHeader = SetTab newHeader newRows
> where
> checkHeader = all (\n -> checkSize n (length [a | (a,_) <- header, n == a])) wantedNames
> checkSize :: String -> Int -> Bool
> checkSize n s
> | s == 0 = error ("project: could not project to "
> ++ show n ++ ": name unknown.")
> | s == 1 = True
> | otherwise = error ("project: could not project to "
> ++ show n ++ ": name is ambigious.")
> posList = [i | s <- wantedNames, (i,(n,_)) <- zip [1..] header, n==s ]
> newHeader = [h | p <- posList, (i,h) <- zip [1..] header, p==i ]
> newRows = Set.map
> (\r -> [ l | p <- posList, (i,l) <- zip [1..] r, p==i] )
> rows

-- UnitTesting --
-----------------
Expand Down Expand Up @@ -217,8 +217,19 @@ projections on table23
> (["Name"], table123Empty, mkTable [("Name", String)] []),
> (["ID", "Name"], table123Empty, mkTable
> [("ID", Integer), ("Name", String)] []),
> (["Name", "ID"], table123Empty, mkTable
> [("Name", String), ("ID", Integer)] []),

> (["Name", "ID"], table123Empty, mkTable
> [("Name", String), ("ID", Integer)] []),

FIXME: RLY?
wikipedia sagt:
\pi_{a_1,...,a_n} R is defined as the set that is obtained when all tuples in R
are restricted to the set {a1,...,an}.

Dass heist dass {a1,...,an} ein set ist, also unsortiert und die sortierung
wird aus R uebernommen, imho. forallem is mir heut nacht die erleuchtng fuer ne
project implementierung gekommen, die so funktioniert wie wikipedia sagt

> (["ID", "Name"], table1, table1),
> (["ID", "Name"], table2, table2),
> (["ID", "Name"], table23, table23),
Expand Down
9 changes: 5 additions & 4 deletions src/TableOps.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ TODO: simplify class/type constraints

Note: The default implementation is clean and short, but not very effective.

FIXME: throw away project and only use projectUnsave

> projectUnsafe :: [ColumnName] -> tab -> tab
> project :: [ColumnName] -> tab -> tab
> projectUnsafe wanted tab = mkTable newHeader $ rows newTab
> where
> names = columnNames tab
> newHeader = [(n,t) | w <- wanted, (n,t) <- header tab, w==n]
> newRow row = [l | w <- wanted, (n,l) <- zip names row, w==n]
> newTab = mapRowsUnsafe newRow tab
> filterWanted = map snd . filter fst . zip [elem n wanted | n <- columnNames tab]
> newHeader = filterWanted (header tab)
> newTab = mapRowsUnsafe filterWanted tab
> project wanted tab
> | not checkExist = error(
> "invalid projection: a name can't be found inside the table in project" ++
Expand Down

0 comments on commit 9fc71e9

Please sign in to comment.