News for hackers. Stuff that matters.
Re:When in doubt, turn to _why by shrughes on Wednesday January 21, 2009 @07:13PM (#444082)
I'd be happy to write you an example if you specified more clearly than the linked website what the problem is.
Re:When in doubt, turn to _why by bk on Wednesday January 21, 2009 @08:14PM (#444156)
Essentially, you get a hash consisting of key/value pairs, where the keys may have a format such that

    "key[subkey][subsubkey]" => "some_value"
is parsed into

    {"key" => {"subkey" => {"subsubkey" => "some_value"}}}
for any depth of nesting.

Valid keys would be:

    "key"
    "key[subkey]"
    "key[subkey][subsubkey]"
    etc.
I hope I got the spec right. Pretty tired right now, been coding all day. :)
Re:When in doubt, turn to _why by shrughes on Wednesday January 21, 2009 @10:39PM (#444311)
http://paste.lisp.org/display/74007

    let Right x = paramTree [("a[x]","b"),("a[b]","c"),
    ("a[b][c]","d"),("bc[de]","f")] in putStrLn (drawTree x)

    { "a" => { "b" => <conflict>, "x" => "b" }, "bc" => {
    "de" => "f" } }
Re:When in doubt, turn to _why by eru on Thursday January 22, 2009 @05:38AM (#444616)
Interesting. Could you have implemented drawTree with Language.Haskell.Pretty? Also I get the following error with ghc:

yc.hs:35:34: No instance for (Monad (Either String)) arising from a use of `mapM' at yc.hs:35:34-52 Possible fix: add an instance declaration for (Monad (Either String)) In a 'do' expression: keys <- mapM parseKey names In the expression: do keys <- mapM parseKey names return (toTree (zip keys values)) In the definition of `paramTree': paramTree nameValues = do keys <- mapM parseKey names return (toTree (zip keys values)) where (names, values) = unzip nameValues

(Edit: I'm sorry that the error message's whitespace gets messed up here.)

Also your groupings function is only used in conjunction with Data.Map.fromList. Did you consider using something like Data.Map.fromListWith (++) instead? Of course you'd have to wrap your would-be Map values in singleton lists first.

Re:When in doubt, turn to _why by shrughes on Thursday January 22, 2009 @09:47PM (#445835)
What version of GHC (or of the libraries) do you have? (Either String) should be a monad as specified through Control.Monad.Error:

    instance Error e => Monad (Either e) where
        ...
I didn't consider Data.Map.fromListWith; I didn't know about it. (Thanks.)
Re:When in doubt, turn to _why by shaunxcode on Wednesday January 21, 2009 @10:51PM (#444322)
This is how I would approach such an issue with php. This assumes it would be run through eval or written to file and included. Obviously to make this valid php I would replace { with Array( and } with ) -- but I decided to stick to the spec above for arguments sake.

$parsed = str_replace('{', '{"', '{'.str_replace(Array('[', ']'), Array('" => {', ''), $string)) .str_repeat('}', substr_count($string, '[')+1);

Re:When in doubt, turn to _why by eru on Thursday January 22, 2009 @05:23AM (#444607)
Keep away from small children and tainted data.
Re:When in doubt, turn to _why by shaunxcode on Thursday January 22, 2009 @10:11AM (#444850)
the truth is spoken here.