![]() |
|
[C] A Challenge for the bored C++'ers - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] A Challenge for the bored C++'ers (/showthread.php?tid=6073225) |
A Challenge for the bored C++'ers - saltycracker - Apr 30, 2020 Here's a good challenge for a keen C++'er. Create a dictionary in C++(keys are string's, and values are int's) but you can only use functions, function closures, and lambda functions as the dictionary. Good luck and have fun! A hint right way. Here's the header files I used in my solution: Code #include <iostream>Hint number 2. I don't expect anything fancy. Just create a data store that you can add string keys and int values that you can later retrieve. example -> you should have a add function to add key value pairs to your dictionary. You should have a find function to lookup keys and return values from your dictionary. RE: A Challenge for the bored C++'ers - jumprop - Apr 30, 2020 Hi, this seems like a fun challenge, but I'm a bit confused by the wording of the problem. Would you mind clarifying what you mean when you say "but you can only use functions, function closures, and lambda functions as the dictionary."? Thanks RE: A Challenge for the bored C++'ers - saltycracker - Apr 30, 2020 (Apr 30, 2020 03:15 PM)jumprop Wrote: Hi, this seems like a fun challenge, but I'm a bit confused by the wording of the problem. You will store the key and value pairs using function closures and lambda functions. Please note: The solution isn't complicated or large. My solution weights in at 34 lines of code. RE: A Challenge for the bored C++'ers - Dostoevsky - Apr 30, 2020 PHP Code This isn't a challenge if you've ever formalised heaps in Lean, Coq, or Agda, etc. where this is like the simplest way to do a purely functional map (the heap is defined as a mapping from keys to values and to "set a key to a value" is just to create a shallower if-statement in a recursive decision tree using higher order functions). RE: A Challenge for the bored C++'ers - saltycracker - Apr 30, 2020 (Apr 30, 2020 10:59 PM)Dostoevsky Wrote: I didn't use the optional type for my returned values.. I just used 0 as an invalid or end of search flag. Code #include <iostream>RE: A Challenge for the bored C++'ers - Dostoevsky - Apr 30, 2020 (Apr 30, 2020 11:25 PM)saltycracker Wrote: I didn't use the optional type for my returned values.. I just used 0 as an invalid or end of search flag. I used optional because, of course, the next step would be to generalise key and value types to produce a templated version. RE: A Challenge for the bored C++'ers - saltycracker - May 1, 2020 (Apr 30, 2020 11:45 PM)Dostoevsky Wrote: I used optional because, of course, the next step would be to generalise key and value types to produce a templated version. Yeah! Something like. Code type ('a, 'b) dict = 'a -> 'b optionRE: A Challenge for the bored C++'ers - Dostoevsky - May 1, 2020 (May 1, 2020 12:15 AM)saltycracker Wrote: Yeah! Something like. Perhaps a bit more like: Code Eq a => a -> Maybe bRE: A Challenge for the bored C++'ers - saltycracker - May 1, 2020 (May 1, 2020 12:26 AM)Dostoevsky Wrote: Perhaps a bit more like: Different language... I'm more comfortable with OCaml Code type ('a, 'b) dict = 'a -> 'b optionRE: A Challenge for the bored C++'ers - Dostoevsky - May 1, 2020 (May 1, 2020 12:50 AM)saltycracker Wrote: Different language... I'm more comfortable with OCaml Yeah, I like OCaml. Just the lack of typeclasses, curried constructors (at least without Jane street extensions), etc. is a deal-breaker for quite a lot of things for me (although, admittedly, it has some of the best tooling for other stuff; menhir). RE: A Challenge for the bored C++'ers - saltycracker - May 1, 2020 (May 1, 2020 12:54 AM)Dostoevsky Wrote: Yeah, I like OCaml. Just the lack of typeclasses, curried constructors (at least without Jane street extensions), etc. is a deal-breaker for quite a lot of things for me (although, admittedly, it has some of the best tooling for other stuff; menhir). I'm just glad that OCaml takes a practical approach to functional programming.. I don't think I'd like OCaml if it embraced monads. I like an easy escape hatch out of the functional paradigm to address the world. RE: A Challenge for the bored C++'ers - Dostoevsky - May 1, 2020 (May 1, 2020 01:03 AM)saltycracker Wrote: I'm just glad that OCaml takes a practical approach to functional programming.. I don't think I'd like OCaml if it embraced monads. I like an easy escape hatch out of the functional paradigm to address the world. Yeah, it's a "get sh*t done" functional language. My favourite OCaml project is Coq, the theorem prover (that's the workhorse theorem prover). |